there are three tables country-> region-> city where in each subsequent there is the id of the previous one, to which it refers:

tbl.strana

id name 1 Албания 2 Уругвай 3 Вьетнам 

tbl.region

 id name countryid 1 Албанский регион 1 2 Уругвайский регион 2 3 Вьетнамский регион 3 

tbl.gorod

 id name regionid 1 Албанская столица 1 2 Уругвайская столица 2 3 Вьетнамская столица 3 

Please tell me how in one request for a city Id to get the region and country id.

  • 3
    See about join - Mike
  • one
    select t1.*,t2.*,t3.* from table1 t1 left join table2 t2 on t2.id = t1.id, left join table3 t3 on t3.id = t1.id get all data from 3 tables with the same id - InDevX
  • @InDevX request text does not match the question asked. - Akina
  • @Akina while writing - it was not really clear what information is available ... - InDevX
  • select r.id, cn.id from city c left join region r on r.id = c.id, left join country cn on cn.id = c.id something like this, it turns out - InDevX

1 answer 1

Personally, I like the following requests, everything is clear where it comes from:

 SELECT тбл.страна.name as `countryname`, тбл.регион.name as `regionname`, тбл.город.name as `cityname` FROM тбл.страна, тбл.регион, тбл.город WHERE тбл.страна.id = тбл.регион.countrid AND тбл.регион.id = тбл.город.regionid AND тбл.город.id = какой-то город ид 
  • and personally, I like it when the request can be read in its entirety without having to crawl for half an hour with the mouse) - Ipatyev
  • one
    @ Ipatiev, still new) - FrozDark
  • yes you are just a bitchara)) - Olympic Mishka