更多子查询

时间:2016-10-27 12:43:54

标签: mysql

请问你能解决问题的所在。

MYSQL

Select NameList_name from NameList where NameList.id In  
(Select NameList.id from NameList where id_company = 
(Select company.id From company where company_company Like "XY"))
And
(Select id_NameList from NameListGoods where id_NameListGoodsKind = 
(Select NameListGoodsKind.id from NameListGoodsKind where NameListGoodsKind_NameListGoodsKind LIKE "XX"))
And
(Select id_NameList from NameListStateFrom where id_StateFrom = 
(Select StateFrom.id from StateFrom where StateFrom_state Like "XXX"))
And
(Select id_NameList from NameListStateTo where id_StateTo = 
( Select StateTo.id from StateTo where StateTo_state Like "XYX")); 

这让我回复了"" SQL Server子查询返回的值超过1个"" 强文 我想搜索: 从NameList中选择NameList_name,NameList.id 正确的NameList.id外显率 什么从其他子查询返回。

示例:

表名单:

ID   Name    
1    a   
2    b    
3    c    
4    d    
5    e

子查询1:

(Select NameList.id from NameList where id_company = 
(Select company.id From company where company_company Like "XY"))

回复我:数字:1,4,5

子查询2:

(Select id_NameList from NameListGoods where id_NameListGoodsKind = 
(Select NameListGoodsKind.id from NameListGoodsKind where NameListGoodsKind_NameListGoodsKind LIKE "XX"))

回复我:数字2,4,3

子查询3:

(Select id_NameList from NameListStateFrom where id_StateFrom = 
(Select StateFrom.id from StateFrom where StateFrom_state Like "XXX"))

回复我:数字3,4,1

子查询4:

(Select id_NameList from NameListStateTo where id_StateTo = 
(Select StateTo.id from StateTo where StateTo_state Like "XYX"));

回复我:数字5,4,2,1

抬头查询

此结果的穿透数为4.然后我需要显示姓名列表中的名称,其中Namelist.id = 4;

寻求帮助,

1 个答案:

答案 0 :(得分:1)

SELECT a.id
  FROM (SUBQUERY 1) a
  JOIN (SUBQUERY 2) b
    ON b.id = a.id
  JOIN (SUBQUERY 3) c
    ON c.id = a.id
  JOIN (SUBQUERY 4) d
    ON d.id = a.id;