JOIN同一个表时为什么数据不匹配?

时间:2017-12-24 10:27:08

标签: mysql sql

我试图像这样显示表格

期望的结果: Desired result

条件:

  1. tgl1,kondisi_1,kondisi_2,kondisi_3,kondisi_4是kd_item的最新条件组
  2. tgl2,kond1,kond2,kond3,kond4是最新数据,其中日期< = CURDATE() - INTERVAL 7 DAY group by kd_item
  3. SQL: http://sqlfiddle.com/#!9/661f7/2

    我尝试写这样的查询,

    查询:

    SELECT a.kd_item, MAX(a.tanggal) as tgl1, a.kondisi_1, a.kondisi_2, a.kondisi_3, a.kondisi_4,
    -- I know min is not the right query 
    MIN(b.tanggal) as tgl2, b.kondisi_1 as kond1, b.kondisi_2 as kond2, b.kondisi_3 as kond3, b.kondisi_4 as kond4
    FROM status_item as a
    LEFT JOIN status_item as b ON a.kd_item = b.kd_item
    GROUP BY a.kd_item LIMIT 10
    
    1. 当我加入同一张桌子时,根据上述查询,为什么显示的数据与日期无关?
    2. 如何显示条件最新日期,其中日期小于或等于7天前?
    3. 如何在表格的左侧显示第2点?

2 个答案:

答案 0 :(得分:0)

你应该选择最大日期,加入包含非参与组的数据,然后加入两个不同的查询toghter

select * from (
  select a.kd_item, a.tanggal as tgl1, a.kondisi_1, a.kondisi_2, a.kondisi_3, a.kondisi_4
  from status_item a 
  inner join (
    SELECT kd_item, MAX(tanggal) as max_tanggal
    FROM status_item 
    group by kd_item
    ) t1 on t1.max_tanggal = a.tanggal and t1.kd_item = a.kd_item
) t3 
inner join (
  select b.kd_item, b.tanggal as b_tgl1, b.kondisi_1 as b_kondisi_1, b.kondisi_2 as b_kondisi_2 , b.kondisi_3 as b_kondisi_3, b.kondisi_4 as b_kondisi_4
  from status_item b 
  inner join (
    SELECT kd_item, MAX(tanggal) as max_tanggal
    FROM status_item 
    where tanggal <= date_sub(CURDATE(), INTERVAL 7 DAY )
    group by kd_item
  ) t2 on t2.max_tanggal = b.tanggal and t2.kd_item = b.kd_item
) t4 on t3.kd_item= t4.kd_item

答案 1 :(得分:0)

这是你在找什么?

select
    a.kd_item, 
    a.tanggal  as tgl1, 
    a.kondisi_1, 
    a.kondisi_2, 
    a.kondisi_3, 
    a.kondisi_4,
    b.tanggal   as tgl2, 
    b.kondisi_1 as kond1, 
    b.kondisi_2 as kond2, 
    b.kondisi_3 as kond3, 
    b.kondisi_4 as kond4
from
    status_item as a
    left join 
    status_item as b 
    on
        a.kd_item = b.kd_item and
        b.tanggal <= date_sub(a.tanggal, interval 7 day);

结果是:

+---------+------------+-----------+-----------+-----------+-----------+------------+-------+-------+-------+-------+
| kd_item | tgl1       | kondisi_1 | kondisi_2 | kondisi_3 | kondisi_4 | tgl2       | kond1 | kond2 | kond3 | kond4 |
+---------+------------+-----------+-----------+-----------+-----------+------------+-------+-------+-------+-------+
|       1 | 2017-10-22 |         4 |         0 |         0 |         0 | 2017-10-07 |     3 |     0 |     1 |     0 |
|       1 | 2017-10-17 |         3 |         1 |         0 |         0 | 2017-10-07 |     3 |     0 |     1 |     0 |
|       2 | 2017-12-22 |         3 |         0 |         1 |         0 | 2017-12-12 |     2 |     1 |     0 |     1 |
|       2 | 2017-12-22 |         3 |         0 |         1 |         0 | 2017-10-22 |     4 |     0 |     0 |     0 |
|       2 | 2017-12-12 |         2 |         1 |         0 |         1 | 2017-10-22 |     4 |     0 |     0 |     0 |
|       3 | 2017-12-12 |         4 |         0 |         0 |         0 | 2017-10-22 |     1 |     1 |     1 |     1 |
|       8 | 2017-12-06 |         4 |         0 |         0 |         0 | 2017-11-28 |     0 |     0 |     4 |     0 |
|       1 | 2017-10-07 |         3 |         0 |         1 |         0 | null       |  null |  null |  null |  null |
|       2 | 2017-10-22 |         4 |         0 |         0 |         0 | null       |  null |  null |  null |  null |
|       3 | 2017-10-22 |         1 |         1 |         1 |         1 | null       |  null |  null |  null |  null |
|       4 | 2017-10-22 |         4 |         0 |         0 |         0 | null       |  null |  null |  null |  null |
|       5 | 2017-10-27 |         4 |         0 |         0 |         0 | null       |  null |  null |  null |  null |
|       5 | 2017-10-22 |         3 |         0 |         1 |         0 | null       |  null |  null |  null |  null |
|       6 | 2017-10-22 |         4 |         0 |         0 |         0 | null       |  null |  null |  null |  null |
|       7 | 2017-10-22 |         4 |         0 |         0 |         0 | null       |  null |  null |  null |  null |
|       8 | 2017-11-28 |         0 |         0 |         4 |         0 | null       |  null |  null |  null |  null |
+---------+------------+-----------+-----------+-----------+-----------+------------+-------+-------+-------+-------+
16 rows in set (0.00 sec)

对于问题中的第3点,您可以添加

date_sub(a.tanggal, interval 7 day)

作为第一个结果列:

select
    date_sub(a.tanggal, interval 7 day) as week_ago,
    a.kd_item, 
    a.tanggal  as tgl1, 
    ...