从表中评估来自其中一个的结果

时间:2018-01-12 22:28:20

标签: mysql

我有三个表要加入,其中一个表有一到多个值。

SQLFIDDLE

CREATE TABLE Table1  (`id` int, `name` varchar(3));

INSERT INTO Table1   (`id`, `name`)
VALUES   (1, 'A'),  (2, 'B'),   (3, 'C');


CREATE TABLE Table2   (`id` int, `status` int, `date` varchar(9));

INSERT INTO Table2  (`id`, `status`, `date`)
VALUES   (1, 1, '''.11..'''),  (1, 2, '''.12..'''),   (1, 3, '''.13..'''),
         (2, 3, '''.23..'''),  (3, 1, '''.31..'''),   (3, 3, '''.33..''')
;


CREATE TABLE Table3  (`id` int, `value` int);

INSERT INTO Table3   (`id`, `value`)
VALUES     (1, 34),  (2, 22),  (3, 17);

查询1

select * from table1

| id | name |
|----|------|
|  1 |    A |
|  2 |    B |
|  3 |    C |

查询2

select * from table2;

| id | status |    date |
|----|--------|---------|
|  1 |      1 | '.11..' |
|  1 |      2 | '.12..' |
|  1 |      3 | '.13..' |
|  2 |      3 | '.23..' |
|  3 |      1 | '.31..' |
|  3 |      3 | '.33..' |

查询3

select * from table3

| id | value |
|----|-------|
|  1 |    34 |
|  2 |    22 |
|  3 |    17 |

我需要为每个id返回的查询:

   TABLE1.name, TABLE2.status, TABLE2.date, TABLE3.value

有这个条件:

  • 如果TABLE2.status = 1存在,则仅返回TABLE2的那一行
  • 否则,如果TABLE2.status = 1不存在,则查找status = 2并仅返回TABLE2的那一行
  • 如果TABLE2中没有其中任何一个值,则从结果中跳过该ID

编辑:TABLE2有一个用于id,状态的UNIQUE键,因此只能有一个id = 1 status = 1

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

这样的事情可能是:

select table1.id, table1.name,
    coalesce(table2_status1.status, table2_status2.status) as status,
    coalesce(table2_status1.date, table2_status2.date) as date,
    table3.value
from table1
left join table2 table2_status1 on table2_status1.id = table1.id and table2_status1.status = 1
left join table2 table2_status2 on table2_status2.id = table1.id and table2_status2.status = 2
join table3 on table3.id = table1.id
where (table2_status1.id is not null or table2_status2.id is not null);

答案 1 :(得分:0)

使用子选择不起作用(但rlanvins https://stackoverflow.com/a/48235077/7505395更好):

A,B,C而不是First,Second,......

select 
    TABLE1.name,

    case 
      when exists( select 1 from table2 where id = table1.id and status = 1)
        then 1
      when exists( select 1 from table2 where id = table1.id and status = 2)
        then 2 
    end as T2status,

    case 
      when exists( select 1 from table2 where id = table1.id and status = 1)
        then ( select date from table2 where id = table1.id and status = 1)
      when exists( select 1 from table2 where id = table1.id and status = 2)
        then ( select date from table2 where id = table1.id and status = 2)
    end as T2date,

    TABLE3.value
from table1
join table3 on table1.id = table3.id
where     
    exists( select 1 from table2 where id = table1.id and status = 1)
    or exists( select 1 from table2 where id = table1.id and status = 2)

<强>输出

Name    T2status    T2date  value
A       1           '.11..' 34 
C       1           '.31..' 17

<强> DDL

CREATE TABLE Table1  (`id` int, `name` varchar(3));

INSERT INTO Table1   (`id`, `name`)
VALUES   (1, 'A'),  (2, 'B'),   (3, 'C');


CREATE TABLE Table2   (`id` int, `status` int, `date` varchar(9));

INSERT INTO Table2  (`id`, `status`, `date`)
VALUES   (1, 1, '''.11..'''),  (1, 2, '''.12..'''),   (1, 3, '''.13..'''),
         (2, 3, '''.23..'''),  (3, 1, '''.31..'''),   (3, 3, '''.33..''')
;


CREATE TABLE Table3  (`id` int, `value` int);

INSERT INTO Table3   (`id`, `value`)
VALUES     (1, 34),  (2, 22),  (3, 17);