Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 也许如何从选择中选择_Mysql_Sql - Fatal编程技术网

Mysql 也许如何从选择中选择

Mysql 也许如何从选择中选择,mysql,sql,Mysql,Sql,请帮忙,我是mySQL新手。我想知道如何编写查询以生成以下结果: 表1 +-----+------+-------+ | id1 | id2 | col1 | +-----+------+-------+ | 9 | 1 | foo 1 | +-----+------+-------+ | 9 | 2 | foo 2 | +-----+------+-------+ | 9 | 3 | foo 3 | +-----+------+-------+ | 8 |

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

表1

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

+-----+------+-------+
| id2 | col2 | col3  |
+-----+------+-------+
|  1  | 2018 | bar 1 |
+-----+------+-------+
|  3  | 2018 | bar 2 |
+-----+------+-------+
|  1  | 2017 | bar 3 |
+-----+------+-------+
|  2  | 2017 | bar 4 |
+-----+------+-------+
想要得到id1=9和2018的结果表1、表2

到目前为止,我已经尝试了以下方法,但没有以我正在寻找的格式返回数据:

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

谢谢,非常感谢。

看来你问题的重点是

使用具有t1.id2=t2.id2和t2.col2=2018连接条件的左外连接 并为年份列的不匹配值指定当前年份,如以下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

你很接近。将年份筛选器移至联接条件:

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    

根据您的样本数据集,其中id2=2,col2为2017年,但在您的预期输出中,它显示col2为2018年,其中id2=2?您能否澄清您所说的“但没有运气”是什么意思?查询是否返回一个空集?什么数据类型用于存储table2.col2,它存储为整数还是varchar?您能显示输出吗?好像我从某处记得这个答案;对不起,伙计。没有抄袭的意图。从零开始。不,不,我没有考虑过…也许我们俩的想法是一样的OK+1
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