Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在mysql中检索具有不同ID的多行_Php_Mysql - Fatal编程技术网

Php 在mysql中检索具有不同ID的多行

Php 在mysql中检索具有不同ID的多行,php,mysql,Php,Mysql,我试图在mysql中编写一个查询,下面给出了我的表结构 place_id state_id city_id place_name 1 0 0 United States 2 1 0 Alabama 3 0 2 Auburn 32

我试图在mysql中编写一个查询,下面给出了我的表结构

place_id    state_id    city_id     place_name  
    1           0           0           United States   
    2           1           0           Alabama     
    3           0           2           Auburn  
    32          0           0           Canada  
    33          32          0           Alberta     
    34          0           33          Calgary 
这里我有美国id,它是1,使用这个id我想检索美国所有的州和每个州的城市。 这里,

  • 如果state\u id和city\u id为0,则它是一个国家
  • 如果city_id为0,则它是一个状态
  • 如果state_id为0,则它是一个城市
  • 位置id是自动递增的

  • 请有人帮帮我,谢谢。

    对于美国,您可以使用自动加入来加入同一个表3次

    select t1.place_name, t2.place_name, t3.
    from my_table t1 
    inner join my_table t2 on  t1.place_id = t2.state_id 
    inner join my_table t3 on t3place_id =   t3.city_id
    where t1.place_name  ='United States';
    

    发布您的预期输出。我不羡慕您的数据模型:-(谢谢你的回复,自我加入解决了我的问题