简体   繁体   English

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

[英]MySQL - Getting count of distinct values from 3 tables

I have created 3 tables named complete_shifts,incomplete_shifts and incomplete_shift_register. 我创建了3个表,分别名为complete_shifts,incomplete_shifts和incomplete_shift_register。 All three tables include 2 columns called employee_no and date. 这三个表均包含2列,名为employee_no和date。 I want to get count of distinct values in employee_no column from all three tables on a specified date. 我想在指定日期从所有三个表中获取employee_no列中不同值的计数。

Ex: if I specify " date = 2013-06-13 " then count shuld be 3 . 例如:如果我指定“ date = 2013-06-13 ”,则计数应为3 (employee no : 0008,0019,0035 ) (员工编号: 0008,0019,0035

complete_shifts 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 incomplete_shifts

 shift_id  |    date   | employee_no |  sign_in_at |sign_out_at

   1       | 2013-06-13|    0019     |   7:08      |    

incomplete_shift_register 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

i don't know how to write the SQL syntax for above situation... plezzz help me.. 我不知道如何为上述情况编写SQL语法... plezzz帮帮我..

thank you. 谢谢。

How about something like 怎么样

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'
    )

Using 使用

UNION Syntax UNION语法

UNION is used to combine the result from multiple SELECT statements into a single result set. UNION用于将来自多个SELECT语句的结果合并为一个结果集。

and

COUNT(DISTINCT expr,[expr...]) COUNT(DISTINCT expr,[expr ...])

Returns a count of the number of rows with different non-NULL expr values. 返回具有不同非NULL expr值的行数的计数。

Just UNION the tables together and COUNT . 只需将表UNIONCOUNT一起。 The UNION will remove duplicates, so a simple COUNT will do the trick - a COUNT DISTINCT won't be needed. UNION将删除重复项,因此简单的COUNT就可以解决问题-不需要COUNT DISTINCT This will work if you want the count only; 如果您只想计数,这将起作用。 if you want other values the query gets slightly more involved: 如果您需要其他值,查询将涉及更多:

SELECT COUNT(*) 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'
 ) AllShifts
SELECT count(*) from 
(
SELECT 1 from complete_shifts WHERE date="2013-06-13"
UNION ALL
SELECT 1 from incomplete_shifts WHERE date="2013-06-13"
UNION ALL
SELECT 1 from incomplete_shift_register WHERE date="2013-06-13"
)t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM