Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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
Sql 内部连接的行为类似于交叉连接?_Sql_Sql Server_Join - Fatal编程技术网

Sql 内部连接的行为类似于交叉连接?

Sql 内部连接的行为类似于交叉连接?,sql,sql-server,join,Sql,Sql Server,Join,我试图创建一个查询,在多个表之间创建连接。 以下是一个例子: select b.name as state, c.name as city, sum(qnt) as total_quantity from qtn_table a, state_table b, city_table c where a.state_id = b.id and a.city_id = c.id and a.year = 2011 and a.product = 1 group by b.name, c.name

我试图创建一个查询,在多个表之间创建连接。 以下是一个例子:

select b.name as state, c.name as city, sum(qnt) as total_quantity 
from qtn_table a, state_table b, city_table c
where a.state_id = b.id and a.city_id = c.id
and a.year = 2011 and a.product = 1 group by b.name, c.name
还尝试了内部联接:

select b.name as state, c.name as city, sum(qnt) as total_quantity 
from qtn_table a
inner join state_table b ON a.state_id = b.id
inner join city_table c ON a.city_id = c.id
where a.year = 2011 and a.product = 1 group by b.name, c.name
结果是一样的

它应该只返回一个拥有自己国家的城市:

state    city    total_quantity
NY       A
NY       B
Texas    C
Texas    D
Cali     E
Cali     F
但它正在返回奇怪的结果,例如:

state    city     total_quantity
NY       A
Texas    A
Cali     A
NY       B
...
...
在一个典型的交叉连接上,我认为它应该出现在所有州的一个城市,但它只影响了其中的一些州,而不是所有州,这是一个更奇怪的情况


我做错了什么?

您缺少一个从
state\u table
city\u table
的连接,并且它为该表中的每个州或具有相同名称的每个州返回一行(看起来至少是)。我在查询中添加了
和state\u table.state=city\u table.state

select b.name as state, c.name as city, sum(qnt) as total_quantity 
from qtn_table a
 inner join state_table b ON a.state_id = b.id
 inner join city_table c ON a.city_id = c.id AND state_table.state = city_table.state
where a.year = 2011 
and a.product = 1 
group by b.name, c.name

ypu能提供样品记录和你想要的结果吗?