在子查询中获得价值

时间:2014-03-19 08:19:16

标签: mysql sql subquery mysql-error-1064

我不希望在结果中 r.reguser_id作为测试列,如果我从Select语句中删除此列,那么我在 WHERE bu.business_id = r.reguser_id 并且错误超过1行返回。

SELECT r.reguser_name ,  r.reguser_id as test,

(Select r.reguser_id as c, GROUP_CONCAT(i.industry_name) as Industry

From reguser r inner JOIN business_industry bu ON r.reguser_id = bu.business_id  join industry i ON bu.industry_id = i.industry_id

WHERE  bu.business_id = test

GROUP BY bu.business_id
) as indus

FROM reguser r

请告诉我如何解决此问题? 谢谢。

2 个答案:

答案 0 :(得分:1)

SELECT r.reguser_name ,  r.reguser_id,

(Select r1.reguser_id as c, GROUP_CONCAT(i.industry_name) as Industry

From reguser r1 inner JOIN business_industry bu ON r1.reguser_id = bu.business_id  join industry i ON bu.industry_id = i.industry_id

WHERE  bu.business_id = r.reguser_id

GROUP BY bu.business_id
) as indus

FROM reguser r

不要在子查询中对主表和表使用相同的alliases。这真的很糟糕

答案 1 :(得分:0)

SELECT t1.user_name FROM
(SELECT r.reguser_name as user_name,  r.reguser_id as test,

(Select r.reguser_id as c, GROUP_CONCAT(i.industry_name) as Industry

From reguser r inner JOIN business_industry bu ON r.reguser_id = bu.business_id  join     industry i ON bu.industry_id = i.industry_id

WHERE  bu.business_id = test

GROUP BY bu.business_id
) as indus

FROM reguser r)t1
WHERE t1.user_name IS NOT NULL

我认为如果你在查询这种方式时使用sql developer,你需要有一些where子句,即为select查询提供一个别名。希望这会有所帮助..