Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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 来自2个表的数据_Mysql_Sql_Database - Fatal编程技术网

Mysql 来自2个表的数据

Mysql 来自2个表的数据,mysql,sql,database,Mysql,Sql,Database,有两个表格: 表汽车 id | date ---------- 1 | 2012-01-04 2 | 2012-01-04 3 | 2012-01-05 id_car | year | author ------------------------- 1 | 2005 | John 1 | 2001 | Carl 2 | 2003 | Carl 2 | 2001 | John 3 | 2004 | Carl 3 | 2003 | John 表版本 id | date ----------

有两个表格:

汽车

id | date
----------
1 | 2012-01-04
2 | 2012-01-04
3 | 2012-01-05
id_car | year | author
-------------------------
1 | 2005 | John
1 | 2001 | Carl

2 | 2003 | Carl
2 | 2001 | John

3 | 2004 | Carl
3 | 2003 | John
版本

id | date
----------
1 | 2012-01-04
2 | 2012-01-04
3 | 2012-01-05
id_car | year | author
-------------------------
1 | 2005 | John
1 | 2001 | Carl

2 | 2003 | Carl
2 | 2001 | John

3 | 2004 | Carl
3 | 2003 | John
如果作者是卡尔,我需要获得关于昨天日期(2012-01-04)的汽车的所有信息以及关于其最新版本的信息

所以在这个例子中,我需要得到:
2 | 2012-01-04 | 2003 |卡尔

您想要一个
内部联接

select
    c.id,
    c.date,
    v.year,
    v.author
from
   cars c
   inner join versions v on
       c.id = v.id_car
   inner join (
        select 
            id_car, 
            max(year) as latestYear 
        from 
            versions 
        group by 
            id_car
    ) vmax on
       c.id = vmax.id_car
       and v.year = vmax.latestYear
where
   v.author = 'Carl'
   and c.date = '2012-01-04'

在这个问题中,你是说,“从
cars
中抓取日期为
2012-01-04
的所有内容,然后在
versions
中查找所有内容,其中
id\u car
列等于
cars
中的我的
id
列。哦,只给我从
版本
中的任何内容,作者是
卡尔
,但只给汽车的年份等于可用汽车的最大年份。”

你想要
内部连接

select
    c.id,
    c.date,
    v.year,
    v.author
from
   cars c
   inner join versions v on
       c.id = v.id_car
   inner join (
        select 
            id_car, 
            max(year) as latestYear 
        from 
            versions 
        group by 
            id_car
    ) vmax on
       c.id = vmax.id_car
       and v.year = vmax.latestYear
where
   v.author = 'Carl'
   and c.date = '2012-01-04'

在这个查询中,您的意思是,“从
cars
中抓取日期为
2012-01-04
的所有内容,然后在
versions
中查找所有内容,其中
id\u car
列等于
cars
中的my
id
列。哦,只给我从
版本
中的任何内容,作者是
卡尔
,但只给汽车的年份等于可用汽车的最大年份的内容。“

我用它来处理联接->希望您觉得有用我用它来处理联接->希望您觉得有用这将从表版本返回两行:1 | 2001 | Carl和2 | 2003 | Carl。我只需要第二行。这将从表版本返回两行:1 | 2001 | Carl和2 | 2003 | Carl。我只需要第二个