Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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 SQL搜索城市id中的用户和用户状态_Php_Mysql_Join - Fatal编程技术网

Php SQL搜索城市id中的用户和用户状态

Php SQL搜索城市id中的用户和用户状态,php,mysql,join,Php,Mysql,Join,我不知道这个标题是否有意义,但情况如下:见下表4 用户表(有200多个用户) 城市表 +---------+-------+----------+ | idcity | city | idstate | +---------+-------+----------+ | 123 | city1 | 1 | | 542 | city2 | 2 | | 623 | city3 | 3 | +---------+-------+--

我不知道这个标题是否有意义,但情况如下:见下表4

用户表(有200多个用户)

城市表

+---------+-------+----------+
| idcity  | city  | idstate  |
+---------+-------+----------+
|    123  | city1 |     1    |
|    542  | city2 |     2    |
|    623  | city3 |     3    |
+---------+-------+----------+
+---------+--------+------------+
| idstate | state  | idcountry  |
+---------+--------+------------+
|     1   | state1 |      4     |
|     2   | state2 |      5     |
|     3   | state3 |      6     |
+---------+--------+------------+
    +-----------+----------+
    | idcountry |  country |
    +-----------+----------+
    |     4     | country1 |
    |     5     | country2 |
    |     6     | country3 |
    +-----------+----------+
状态表

+---------+-------+----------+
| idcity  | city  | idstate  |
+---------+-------+----------+
|    123  | city1 |     1    |
|    542  | city2 |     2    |
|    623  | city3 |     3    |
+---------+-------+----------+
+---------+--------+------------+
| idstate | state  | idcountry  |
+---------+--------+------------+
|     1   | state1 |      4     |
|     2   | state2 |      5     |
|     3   | state3 |      6     |
+---------+--------+------------+
    +-----------+----------+
    | idcountry |  country |
    +-----------+----------+
    |     4     | country1 |
    |     5     | country2 |
    |     6     | country3 |
    +-----------+----------+
国家/地区表

+---------+-------+----------+
| idcity  | city  | idstate  |
+---------+-------+----------+
|    123  | city1 |     1    |
|    542  | city2 |     2    |
|    623  | city3 |     3    |
+---------+-------+----------+
+---------+--------+------------+
| idstate | state  | idcountry  |
+---------+--------+------------+
|     1   | state1 |      4     |
|     2   | state2 |      5     |
|     3   | state3 |      6     |
+---------+--------+------------+
    +-----------+----------+
    | idcountry |  country |
    +-----------+----------+
    |     4     | country1 |
    |     5     | country2 |
    |     6     | country3 |
    +-----------+----------+
注意=我需要找出属于
州的所有人
并通过
idcity

输出

+--------+----------+-------------------+---------+---------+
| iduser |   user   |      address      |  idcity |  state  |
+--------+----------+-------------------+---------+---------+
|   11   | person11 | some address      |     123 |  state1 |
|    2   | person2  | another address   |     123 |  state1 |
|   13   | person13 | different address |     123 |  state1 |
+--------+----------+-------------------+---------+---------+

如何通过一个连接实现这一点?我尝试在一个连接(在查询中)中组合这两个表,但感觉不太对劲。请帮助我如何修复此pblm。谢谢

像这样试试

select u.iduser,u.user,u.address,u.idcity,s.state 
From usertable u
inner join city c on c.idcity=u.idcity
inner join state s on s.idstate=c.idstate
获取城市身份证

select 
 c.idcity 
from city c 
where c.idcity = <someidcity>
选择
c、 城市
来自c市
其中c.idcity=
加入州名

select 
  c.idcity, 
  s.state
from city c
join state s on s.idstate = c.idstate
where c.idcity = <someidcity>
选择
c、 艾德城,
s、 陈述
来自c市
在s.idstate=c.idstate上连接状态s
其中c.idcity=
然后加入用户

select 
  u.iduser,
  u.user,
  u.address,
  c.idcity, 
  s.state
from city c
join state s on s.idstate = c.idstate
join user u on u.idcity = c.idcity
where c.idcity = <someidcity>
选择
u、 iduser,
u、 用户,
u、 地址:,
c、 艾德城,
s、 陈述
来自c市
在s.idstate=c.idstate上连接状态s
在u.idcity=c.idcity上加入用户u
其中c.idcity=
您不能在单个联接中执行此操作,因为您需要来自表用户和表状态的信息(ID除外),它们之间没有直接关系。

试试这个

SELECT
    a.iduser,
    a.user,
    a.address,
    b.idcity,
    c.state
FROM
    users a
INNER JOIN
    cities b
ON
    a.idcity=b.idcity
INNER JOIN
    states c
ON
    b.idstate=c.idstate
WHERE
    c.state='myState'
AND b.city='myCity'

您必须将城市和州表与用户表连接,使用单个连接这是不可能的,但与按idcity搜索类似,例如:-从用户表u内部连接中选择u.iduser、u.user、u.address、u.idcity、s.state。。。。。。。。。。。。。。。。其中,idcity=“542”操作请求结果为单个joinOP请求结果为单个joinOP请求join@SandipPatel是的,但他也显示了期望的输出。。。