UNION的MySql CONCATENATE结果

时间:2017-01-03 12:15:02

标签: mysql sql union group-concat

我希望从查询联合的结果中得到一个结果...我的代码是这样的:

SELECT GROUP_CONCAT(rifDoc), idUser, user, dateDoc
FROM
(
SELECT GROUP_CONCAT(CAR.rifDoc) AS rifDoc, CAR.idUser, CAR.user, CARDETT.dateDoc
FROM car AS CAR, carDett AS CARDETT 
WHERE CAR.id>0 CAR.id=CARDETT.idDoc CARDETT.dateDoc<='2017-01-31' 
GROUP BY idUser, dateDoc

UNION ALL

SELECT GROUP_CONCAT(BK.rifDoc) AS rifDoc, BK.idUser, BK.user, GROUP_CONCAT(BK.inUso) AS inUso, GROUP_CONCAT(BK.inCarico) AS inCarico, BKDETT.dateDoc
FROM bike AS BK, bikeDett AS BKDETT 
WHERE BK.id>0 AND BK.id=BKDETT.idDoc AND BKDETT.dateDoc<='2017-01-31' 
GROUP BY idUser, dateDoc
 )

GROUP BY idUser, dateDoc

但我有这样的错误:

#1248 - Every derived table must have its own alias

有人有解决方案吗?

2 个答案:

答案 0 :(得分:3)

您缺少内联视图或子查询的别名,因为错误状态如

SELECT GROUP_CONCAT(rifDoc), idUser, `user`, dateDoc
FROM
(
SELECT GROUP_CONCAT(CAR.rifDoc) AS rifDoc, CAR.idUser, CAR.user, CARDETT.dateDoc
FROM car AS CAR, carDett AS CARDETT 
WHERE CAR.id>0 CAR.id=CARDETT.idDoc CARDETT.dateDoc<='2017-01-31' 
GROUP BY idUser, dateDoc

UNION ALL

SELECT GROUP_CONCAT(BK.rifDoc) AS rifDoc, BK.idUser, BK.user, GROUP_CONCAT(BK.inUso) AS inUso, GROUP_CONCAT(BK.inCarico) AS inCarico, BKDETT.dateDoc
FROM bike AS BK, bikeDett AS BKDETT 
WHERE BK.id>0 AND BK.id=BKDETT.idDoc AND BKDETT.dateDoc<='2017-01-31' 
GROUP BY idUser, dateDoc
 ) XXX     <--- here

答案 1 :(得分:0)

每个派生表(AKA子查询)必须确实具有别名。即括号中的每个查询都必须给出一个别名(AS无论如何),可以用来在外部查询的其余部分中引用它。 我不确定,但我认为你的代码应该是这样的

awk -F"|" '{count[$1]++}END{for (i in count) if (count[i] < 10){print count[i],i}}' file
3 96472

不确定代码,但解决方案是(As anythin)