我创建了 3 个名为complete_shifts、incomplete_shifts 和incomplete_shift_register 的表。所有三个表都包含 2 列,分别称为“employee_no”和“日期”。我想从指定日期的所有三个表中获取employee_no 列中不同值的计数。
例如:如果我指定“日期 = 2013-06-13”,则计数应为 3。 (员工编号:0008,0019,0035)
complete_shifts
shift_id | date | employee_no | sign_in_at | sign_out_at
<小时/>
1 |2013-06-13| 0008 | 7:05 | 16:05
2 |2013-06-13| 0008 | 7:10 | 16:05
3 |2013-06-14| 0025 | 7:11 | 16:10
incomplete_shifts
shift_id | date | employee_no | sign_in_at |sign_out_at
<小时/>
1 | 2013-06-13| 0019 | 7:08 |
incomplete_shift_register
shift_id | date |employee_no | sign_in_at
<小时/>
1 | 2013-06-13| 0008 | 7:08
2 | 2013-06-13| 0035 | 7:09
3 | 2013-06-14| 0060 | 7:11
我不知道如何为上述情况编写 SQL 语法...请帮助我..
谢谢。
最佳答案
像这样的东西怎么样
SELECT COUNT(DISTINCT employee_no) cnt_employee_no
FROM (
SELECT employee_no
FROM complete_shifts
WHERE date = '2013-06-13'
UNION
SELECT employee_no
FROM incomplete_shifts
WHERE date = '2013-06-13'
UNION
SELECT employee_no
FROM incomplete_shift_register
WHERE date = '2013-06-13'
)
使用
UNION is used to combine the result from multiple SELECT statements into a single result set.
和
COUNT(DISTINCT expr,[expr...])
Returns a count of the number of rows with different non-NULL expr values.
关于MySQL - 从 3 个表中获取不同值的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17112908/