联合选择和组出现

时间:2018-09-30 16:46:54

标签: mysql sql

我在设置查询时遇到问题

(SELECT * FROM attendance WHERE am_slip="absent" AND pm_slip="none")
 UNION
(SELECT * FROM attendance WHERE am_slip="none" AND pm_slip="absent")
 UNION
(SELECT * FROM attendance WHERE am_slip="absent" AND pm_slip IS NULL)
 UNION
(SELECT * FROM attendance WHERE am_slip="absent" AND pm_slip="late")
 UNION
(SELECT * FROM attendance WHERE am_slip="late" AND pm_slip="absent")
 UNION
(SELECT * FROM attendance WHERE am_slip="absent" AND pm_slip="absent")

我想合并/合并数据表(ID号)中的所有事件,但我做不到。那么,你能帮我吗?

这是使用上面的查询的输出:

这是我想要的输出:

这是表格,我想合并它们并显示单个输出:

实验查询但不起作用:

  

选择stu_id,从中选择COUNT(*)AS id_count(选择*从出勤地点am_slip =“ late”和pm_slip =“ none”)                                         联盟                                         (选择*从出勤地点,其中am_slip =“ none”和pm_slip =“ late”)                                         联盟                                         (选择*从出勤地点,其中am_slip =“ late”和pm_slip为NULL)                                         联盟                                         (选择*从出勤地点,其中am_slip =“ late”和pm_slip =“ absent”)                                         联盟                                         (选择*从出勤地点,其中am_slip =“ absent”和pm_slip =“ late”)                                         联盟                                         (选择*从出勤地点,其中am_slip =“ late”和pm_slip =“ late”)                 GROUP BY stu_id           ORDER BY id_count

3 个答案:

答案 0 :(得分:0)

如果要获得独特的结果,则可以正确使用UNION,但在这种情况下,应避免选择*(选择所有列),应明确选择涉及的列

SELECT name, ID_No, Num_Absence, Handle 
FROM attendance WHERE am_slip="absent" AND pm_slip="none")
  UNION
SELECT name, ID_No, Num_Absence, Handle 
FROM attendance WHERE am_slip="none" AND pm_slip="absent"
  UNION
SELECT name, ID_No, Num_Absence, Handle 
FROM attendance WHERE am_slip="absent" AND pm_slip IS NULL
  UNION
SELECT name, ID_No, Num_Absence, Handle 
FROM attendance WHERE am_slip="absent" AND pm_slip="late"
  UNION
SELECT name, ID_No, Num_Absence, Handle 
FROM attendance WHERE am_slip="late" AND pm_slip="absent"
  UNION
SELECT name, ID_No, Num_Absence, Handle 
FROM attendance WHERE am_slip="absent" AND pm_slip="absent"

答案 1 :(得分:0)

下面将通过DISTINCT给出示例,并使用WHERE子句上的ORs操作数替换所有UNION子句:

SELECT
    DISTINCT name, ID_No, Num_Absence, Handle 
FROM
    attendance
WHERE
    (am_slip="absent" AND (pm_slip="none" OR pm_slip IS NULL OR pm_slip="late" OR pm_slip="absent"))
OR
    (am_slip="none" AND pm_slip="absent")
OR
    (am_slip="late" AND pm_slip="absent")

答案 2 :(得分:0)

完成谢谢你们对我的帮助

  

选择idnum.stu_id,从(选择*来自出席者的COUNT(*)中,am_slip =“ absent”和pm_slip =“ none”                                         全联盟                                         选择*从出勤地点,其中am_slip =“ none”和pm_slip =“ absent”                                         全联盟                                         SELECT * FROM出勤,其中am_slip =“ absent”和pm_slip为NULL                                         全联盟                                         选择*从出勤地点,在am_slip =“ absent”和pm_slip =“ late”                                         全联盟                                         选择*从出勤地点,在am_slip =“ late”和pm_slip =“ absent”                                         全联盟                                         选择*从出席者中,将am_slip =“ absent”和pm_slip =“ absent”)作为idnum       GROUP BY stu_id           排序stu_id