Mysql select 并集错误
Mysql select union error
我正在尝试显示来自表 1 的所有记录,即使表 2 中不存在该 catid(如果表 2 中不存在,则表 2 中的所有员工都应该拥有来自表 1 的所有 catid,时间为 0 天),使用以下 sql查询但出现错误:
Error Code: 1054. Unknown column 'catid' in 'group statement'
SQL
select empid,days from table2
union
select catid from table1
group by empid, catid;
表 1
catid
1
2
3
table2
empid catid days (computed column count(*))
1000 1 1
1000 3 1
预期结果:
empid catid days
1000 1 1
1000 2 0 <---catid 2 and days 0 if catid is not existing in table2 for empid 1000
1000 3 1
你可以这样做:
表 1:
mysql> SELECT * FROM table1;
+-------+
| catid |
+-------+
| 1 |
| 2 |
| 3 |
+-------+
3 rows in set (0.00 sec)
表 2:
mysql> SELECT * FROM table2;
+-------+-------+
| empid | catid |
+-------+-------+
| 1000 | 1 |
| 1000 | 3 |
| 1001 | 1 |
| 1002 | 2 |
| 1002 | 3 |
+-------+-------+
5 rows in set (0.00 sec)
查询:
SELECT
t2.empid,
t1.catid
,COUNT(t3.empid) as days
FROM table1 AS t1
LEFT JOIN (SELECT
DISTINCT empid,t1.catid
FROM table2 AS t2
LEFT JOIN table1 AS t1 ON (t1.catid>0)
) AS t2 -- Getting all empids from table2 and catids from table1
ON (t1.catid=t2.catid)
LEFT JOIN table2 AS t3 ON (t2.empid=t3.empid AND t3.catid=t2.catid)
GROUP BY t2.empid,t2.catid
ORDER BY t2.empid,t2.catid;
测试:
mysql> SELECT
-> t2.empid,
-> t1.catid
-> ,COUNT(t3.empid) as days
-> FROM table1 AS t1
-> LEFT JOIN (SELECT DISTINCT empid,t1.catid FROM table2 AS t2 LEFT JOIN table1 AS t1 ON (t1.catid>0)) AS t2
-> ON (t1.catid=t2.catid)
-> LEFT JOIN table2 AS t3 ON (t2.empid=t3.empid AND t3.catid=t2.catid)
-> GROUP BY t2.empid,t2.catid
-> ORDER BY t2.empid,t2.catid;
+-------+-------+------+
| empid | catid | days |
+-------+-------+------+
| 1000 | 1 | 1 |
| 1000 | 2 | 0 |
| 1000 | 3 | 1 |
| 1001 | 1 | 1 |
| 1001 | 2 | 0 |
| 1001 | 3 | 0 |
| 1002 | 1 | 0 |
| 1002 | 2 | 1 |
| 1002 | 3 | 1 |
+-------+-------+------+
我正在尝试显示来自表 1 的所有记录,即使表 2 中不存在该 catid(如果表 2 中不存在,则表 2 中的所有员工都应该拥有来自表 1 的所有 catid,时间为 0 天),使用以下 sql查询但出现错误:
Error Code: 1054. Unknown column 'catid' in 'group statement'
SQL
select empid,days from table2
union
select catid from table1
group by empid, catid;
表 1
catid
1
2
3
table2
empid catid days (computed column count(*))
1000 1 1
1000 3 1
预期结果:
empid catid days
1000 1 1
1000 2 0 <---catid 2 and days 0 if catid is not existing in table2 for empid 1000
1000 3 1
你可以这样做:
表 1:
mysql> SELECT * FROM table1;
+-------+
| catid |
+-------+
| 1 |
| 2 |
| 3 |
+-------+
3 rows in set (0.00 sec)
表 2:
mysql> SELECT * FROM table2;
+-------+-------+
| empid | catid |
+-------+-------+
| 1000 | 1 |
| 1000 | 3 |
| 1001 | 1 |
| 1002 | 2 |
| 1002 | 3 |
+-------+-------+
5 rows in set (0.00 sec)
查询:
SELECT
t2.empid,
t1.catid
,COUNT(t3.empid) as days
FROM table1 AS t1
LEFT JOIN (SELECT
DISTINCT empid,t1.catid
FROM table2 AS t2
LEFT JOIN table1 AS t1 ON (t1.catid>0)
) AS t2 -- Getting all empids from table2 and catids from table1
ON (t1.catid=t2.catid)
LEFT JOIN table2 AS t3 ON (t2.empid=t3.empid AND t3.catid=t2.catid)
GROUP BY t2.empid,t2.catid
ORDER BY t2.empid,t2.catid;
测试:
mysql> SELECT
-> t2.empid,
-> t1.catid
-> ,COUNT(t3.empid) as days
-> FROM table1 AS t1
-> LEFT JOIN (SELECT DISTINCT empid,t1.catid FROM table2 AS t2 LEFT JOIN table1 AS t1 ON (t1.catid>0)) AS t2
-> ON (t1.catid=t2.catid)
-> LEFT JOIN table2 AS t3 ON (t2.empid=t3.empid AND t3.catid=t2.catid)
-> GROUP BY t2.empid,t2.catid
-> ORDER BY t2.empid,t2.catid;
+-------+-------+------+
| empid | catid | days |
+-------+-------+------+
| 1000 | 1 | 1 |
| 1000 | 2 | 0 |
| 1000 | 3 | 1 |
| 1001 | 1 | 1 |
| 1001 | 2 | 0 |
| 1001 | 3 | 0 |
| 1002 | 1 | 0 |
| 1002 | 2 | 1 |
| 1002 | 3 | 1 |
+-------+-------+------+