也许如何从选择中进行选择

3

请帮忙,我是mySQL新手。 我想知道如何编写查询以生成以下结果:

table1

+-----+------+-------+
| id1 |  id2 | col1  |
+-----+------+-------+
| 9   |  1   | foo 1 |
+-----+------+-------+
| 9   |  2   | foo 2 |
+-----+------+-------+
| 9   |  3   | foo 3 |
+-----+------+-------+
| 8   |  4   | foo 4 |
+-----+------+-------+
| 7   |  5   | foo 5 |
+-----+------+-------+

table2

+-----+------+-------+
| id2 | col2 | col3  |
+-----+------+-------+
|  1  | 2018 | bar 1 |
+-----+------+-------+
|  3  | 2018 | bar 2 |
+-----+------+-------+
|  1  | 2017 | bar 3 |
+-----+------+-------+
|  2  | 2017 | bar 4 |
+-----+------+-------+

想要得到id1 = 92018相关的表1表2结果。

+-----+-------+-------+------+--------+
| id1 |  id2  | col1  | col2 |  col3  |
+-----+-------+-------+------+--------+
|  9  |  1    | foo 1 | 2018 | bar 1  |
+-----+-------+-------+------+--------+
|  9  |  2    | foo 2 | 2018 | "NULL" |
+-----+-------+-------+------+--------+
|  9  |  3    | foo 3 | 2018 | bar 2  |
+-----+-------+-------+------+--------+

到目前为止,我已经尝试了以下方法,但是还没有按照我期望的格式返回数据:

SELECT 
    *
FROM
    table1 a
        LEFT JOIN
    table2 b ON a.id2 = b.id2
WHERE
    a.id1 = 9 AND b.col2 = 2018

感谢并非常感激。 涉及 IT 技术的相关内容。

根据您的样本数据集,其中id2 = 2且col2为2017,但在您的期望输出中,当id2 = 2时col2显示为2018? - M Khalid Junaid
你能澄清一下你所说的“但没有运气”是什么意思吗?查询是否返回了一个空集?table2.col2使用什么数据类型进行存储,它是作为整数还是作为varchar存储的? - John Stark
你能展示一下输出吗? - Krishanu
2个回答

4
你离正确答案很近了。将年份过滤器移至连接条件中:
create table table1 (id1 int, id2 int, col1 varchar(20));
insert into table1 (id1, id2, col1) values (9, 1, 'foo 1');
insert into table1 (id1, id2, col1) values (9, 2, 'foo 2');
insert into table1 (id1, id2, col1) values (9, 3, 'foo 3');
insert into table1 (id1, id2, col1) values (8, 4, 'foo 4');
insert into table1 (id1, id2, col1) values (7, 5, 'foo 5');

create table table2 (id2 int, col2 int, col3 varchar(20));
insert into table2 (id2, col2, col3) values (1, 2018, 'bar 1');
insert into table2 (id2, col2, col3) values (3, 2018, 'bar 2');
insert into table2 (id2, col2, col3) values (1, 2017, 'bar 3');
insert into table2 (id2, col2, col3) values (2, 2017, 'bar 4');

SELECT 
    a.id1, a.id2, a.col1, 2018 as col2, b.col3
FROM
    table1 a
    LEFT JOIN table2 b ON a.id2 = b.id2 and b.col2 = 2018
WHERE
    a.id1 = 9

结果:

id1        id2          col1              col2  col3                  
-------------------------------------------------------
9          1            foo 1             2018  bar 1                 
9          2            foo 2             2018  <null>                
9          3            foo 3             2018  bar 2    

好像我在某个地方记得这个答案 ;) - Barbaros Özhan
抱歉,伙计。没有剽窃的意图。是从头开始编写的。 - The Impaler
不,我没有考虑过这个...也许我们都想到了同样的事情,好的 +1。 - Barbaros Özhan

3

你的问题中似乎重点在于使用带有 t1.id2 = t2.id2 and t2.col2 = 2018 连接条件的 LEFT OUTER JOIN,并为年份列中的非匹配值分配 当前年份,如下所示的 SQL:

select t1.id1, t1.id2, t1.col1, 
       coalesce(t2.col2,year(now())) as col2, 
       t2.col3
  from table1 t1
  left outer join table2 t2 
  on ( t1.id2 = t2.id2 and t2.col2 = 2018 )
 where t1.id1 = 9 
 order by t1.id2;

 id1    id2  col1   col2    col3

  9      1   foo 1  2018    bar 1
  9      2   foo 2  2018    (null)
  9      3   foo 3  2018    bar 2

SQL Fiddle Demo


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接