MySQL - 从 3 个表中获取不同值的计数

标签 mysql

我创建了 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 Syntax

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/

相关文章:

mysql - 嵌套多个 select 语句的性能影响

mysql - 如何在数据库上存储带有订购项目的 list

mysql - 将重复值移动到另一列

mysql - 如何在更新之前将一列的值复制到另一列

php - phpMyAdmin 数据输入表单可以在链接表中输入数据吗?

mysql - 使用 join 进行 SQL 聚合

mysql - 如何从数据库中提取图像 URL 并填充元标记

mysql - 通过扩展字段更新MySql表的高效方法

php - 使用两个 order by 的 codeigniter 事件记录查询

php - 在MySQL中,如何同时编辑具有相同值的多条记录?