MySQL在第二个表的同一日期连接两个表,多个条目

时间:2012-04-07 12:14:05

标签: mysql

我有这两张桌子:

table1:
---------

UserID:     Date:       Day_status:
-----------------------------------
3004     2010-01-01     Normal
3004     2010-01-12     Normal
3004     2010-01-15     Ignore
3004     2010-01-18     Abnormal
4001     2010-01-09     Normal
4001     2010-01-11     Ignore
4001     2010-02-10     Normal
4001     2010-02-12     Abnormal
------------------------------------

table:2
-------
UserID:     Date:       Time          Height
--------------------------------------------
3004     2010-01-01     08:01:02       35
3004     2010-01-01     09:01:12       32
3004     2010-01-01     18:01:02       40

3004     2010-01-02     07:01:02       20
3004     2010-01-02     08:01:02       30
3004     2010-01-02     10:01:02       10

3004     2010-01-08     07:01:02       20
3004     2010-01-08     08:01:02       30
3004     2010-01-08     10:01:02       10

3004     2010-01-12     07:01:02       30
3004     2010-01-12     09:01:02       50
3004     2010-01-12     18:01:02       30

3004     2010-01-15     07:01:02       30
3004     2010-01-15     09:01:02       50
3004     2010-01-15     18:01:02       30

3004     2010-01-18     07:01:02       30
3004     2010-01-18     09:01:02       50
3004     2010-01-18     18:01:02       30

4001     2010-01-08     07:01:02       30
4001     2010-01-08     08:01:02       30
4001     2010-01-08     09:01:02       40
4001     2010-01-08     13:01:02       30

4001     2010-01-09     07:01:02       30
4001     2010-01-09     08:01:02       30
4001     2010-01-09     09:01:02       40

4001     2010-01-11     08:01:02       30
4001     2010-01-11     09:01:02       30
4001     2010-01-11     18:01:02       30

4001     2010-01-12     08:01:02       30
4001     2010-01-12     09:01:02       30
4001     2010-01-12     18:01:02       30

4001     2010-01-13     08:01:02       30
4001     2010-01-13     09:01:02       30
4001     2010-01-13     18:01:02       30

4001     2010-02-12     08:01:02       30
4001     2010-02-12     09:01:02       30
4001     2010-02-12     09:01:02       30
-----------------------------------------------

请记住,在表1中:用户可以有多个'日期'(用户3004有多个日期)。并且,在表2中,

用户可以拥有多个“日期”,每个日期可以有多个“时间”。在表2中,用户3004有3个不同的“日期时间”2011-01-01'等等。

我想加入这两个表,以便

1)来自table2的所有数据和来自table1的Day_status数据都包含在结果中。

2)如果table1中table2的日期没有'Day_status',结果中该日期的Day_status将显示为'Normal'(注意:表2的日期条目多于table1)

3)来自table1的Day_status ='Ignore'的条目不会出现在最终结果中

输出将如下:

UserID:     Date:       Time          Height     Day_status_val
---------------------------------------------------------------
3004     2010-01-01     08:01:02       35        Normal
3004     2010-01-01     09:01:12       32        Normal
3004     2010-01-01     18:01:02       40        Normal

3004     2010-01-02     07:01:02       20        Normal
3004     2010-01-02     08:01:02       30        Normal
3004     2010-01-02     10:01:02       10        Normal

3004     2010-01-08     07:01:02       20        Normal
3004     2010-01-08     08:01:02       30        Normal
3004     2010-01-08     10:01:02       10        Normal

3004     2010-01-12     07:01:02       30        Normal
3004     2010-01-12     09:01:02       50        Normal
3004     2010-01-12     18:01:02       30        Normal

3004     2010-01-18     07:01:02       30        Abnormal
3004     2010-01-18     09:01:02       50        Abnormal
3004     2010-01-18     18:01:02       30        Abnormal

4001     2010-01-08     07:01:02       30        Normal
4001     2010-01-08     08:01:02       30        Normal
4001     2010-01-08     09:01:02       40        Normal
4001     2010-01-08     13:01:02       30        Normal 

4001     2010-01-09     07:01:02       30        Normal
4001     2010-01-09     08:01:02       30        Normal
4001     2010-01-09     09:01:02       40        Normal

4001     2010-01-12     08:01:02       30        Normal
4001     2010-01-12     09:01:02       30        Normal
4001     2010-01-12     18:01:02       30        Normal

4001     2010-01-13     08:01:02       30        Normal
4001     2010-01-13     09:01:02       30        Normal
4001     2010-01-13     18:01:02       30        Normal

4001     2010-02-12     08:01:02       30        Abnormal
4001     2010-02-12     09:01:02       30        Abnormal
4001     2010-02-12     09:01:02       30        Abnormal
-----------------------------------------------

我使用以下查询来获取结果:

SELECT table2.UserID, table2.Date, table2.Time, table2.Height,

CASE
   when table1.Day_status='Abnormal Day' then 'Abnormal Day'
   when table1.Day_status='Normal Day' then 'Normal Day'
   else 'Normal Day'
END as Day_status_val

FROM table2 LEFT JOIN table1
ON table2.UserID = table1.UserID and table1.Day_status !='Ignore'

但是,它给出了错误的结果。有人可以帮我这个。请查看table1和table2中的数据以及我想要的输出。

1 个答案:

答案 0 :(得分:0)

我觉得你快到了。你需要添加一个额外的ON子句来匹配Date字段,你可以使用IFNULL来获得正确的Day_status。

SELECT t2.UserID,t2.Date,t2.Time,t2.Height,IFNULL(t1.Day_status,'Normal')
FROM table2 t2 
    LEFT JOIN table1 t1
    ON  t2.UserID = t1.UserID 
    AND t1.Day_status !='Ignore' 
    AND t2.`Date`=t1.`Date`