三桌加入2桌的限制

时间:2012-08-21 03:35:30

标签: sql sqlite join

我在这里有点棘手,在SQLite中有以下3个表:

汽车表

.------.------.--------------.---------.
| vin  | year |     make     |  model  |
'------+------+--------------+---------'
| 1234 | 1965 | Aston Martin | DB5     |
| 5678 | 1965 | Ford         | Mustang |
| 9012 | 1994 | Ford         | Tarus   |
`--------------------------------------'

价格表

.------.-------.---------------------.
| vin  | price |    last_modified    |
'------+-------+---------------------'
| 1234 | 60000 | 2012-08-01 12:00:00 |
| 1234 | 58000 | 2012-08-02 12:00:00 |
| 1234 | 56000 | 2012-08-10 12:00:00 |
| 5678 | 30000 | 2012-08-02 12:00:00 |
| 9012 |  1000 | 2012-08-01 12:00:00 |
`------------------------------------'

里程表

.------.---------.---------------------.
| vin  | mileage |    last_modified    |
'------+---------+---------------------'
| 1234 |   35000 | 2012-08-01 12:00:00 |
| 1234 |   35030 | 2012-08-02 12:00:00 |
| 1234 |   35100 | 2012-08-10 12:00:00 |
| 5678 |   60000 | 2012-08-02 12:00:00 |
| 9012 |  245000 | 2012-08-01 12:00:00 |
`--------------------------------------'

我想加入所有三个表格来显示“汽车”表格中的所有行,但只显示价格表中最近的“last_modified”价格和里程表中的单个最高里程数。 / p>

最后我的结果如下:

.------.------.--------------.---------.-------.---------.
| vin  | year |     make     |  model  | price | mileage |
'------+------+--------------+---------+-------+---------+
| 1234 | 1965 | Aston Martin | DB5     | 56000 |   35100 |
| 5678 | 1965 | Ford         | Mustang | 30000 |   60000 |
| 9012 | 1994 | Ford         | Tarus   |  1000 |  245000 |
`--------------------------------------------------------'

如果我选择特定的VIN(即“1234”),我会这样做:

SELECT
    c.year, c.make, c.model, c.vin, p.price, m.mileage, p.last_modified 
FROM 
    cars c
LEFT JOIN (
            SELECT 
                price, vin, last_modified
            FROM 
                price
            WHERE 
                (vin = '1234')
            ORDER BY
                last_modified DESC LIMIT 1
          ) p
LEFT JOIN (
            SELECT 
                mileage, vin, last_modified 
            FROM 
                mileage 
            WHERE 
                (vin = '1234') 
            ORDER BY 
                mileage DESC LIMIT 1
           ) m

WHERE
    (c.vin = '1234') AND (c.vin = p.vin) AND (c.vin = m.vin)
GROUP BY
    p.vin, m.vin
ORDER BY 
    c.vin, p.last_modified DESC, m.mileage ASC;

...但我想从“汽车”表中获取所有行。

有人有任何想法吗?

3 个答案:

答案 0 :(得分:3)

您需要在SubQuery中获取其个人的最大修改日期。尝试,

SELECT  a.*, b.price, c.mileage
FROM    Cars a
            INNER JOIN Price b
                on a.vin = b.vin
            INNER JOIN Mileage c
                on a.vin = c.vin
            INNER JOIN
            (
                SELECT vin, MAX(last_modified) lastMod
                FROM    Price
                GROUP BY vin
            ) d ON  b.vin = d.vin AND
                    b.last_modified = d.lastMod
            INNER JOIN
            (
                SELECT vin, MAX(last_modified) lastMod
                FROM    Mileage
                GROUP BY vin
            ) e ON  c.vin = e.vin AND
                    c.last_modified = e.lastMod

SQLFiddle Demo

答案 1 :(得分:1)

试试这个:http://www.sqlfiddle.com/#!5/71705/14

select c.vin, c.year, c.make, c.model, rpci.price, rmci.mileage 
from car c
join 
(  
  select * from mileage m
  join
  (
    select vin, max(last_modified) recent_modified
    from mileage
    group by vin
  ) rm on rm.vin = m.vin and rm.recent_modified = m.last_modified
) rmci on rmci.vin = c.vin
-- rmci: recent mileage complete info ;-)
join
(
  select * from price p
  join
  (
    select vin, max(last_modified) recent_modified
    from price
    group by vin
  ) rp on rp.vin = p.vin and rp.recent_modified = p.last_modified
) rpci on rpci.vin = c.vin
-- rpci: receint price complete info

输出:

| c.vin | c.year |       c.make | c.model | rpci.price | rmci.mileage |
-----------------------------------------------------------------------
|  1234 |   1965 | Aston Martin |     DB5 |      56000 |        35100 |
|  5678 |   1965 |         Ford | Mustang |      30000 |        60000 |
|  9012 |   1994 |         Ford |   Tarus |       1000 |       245000 |

在此处查询进度:http://www.sqlfiddle.com/#!5/71705/14

答案 2 :(得分:0)

您可以在选择列表中使用相关查询。它很丑,但它会起作用。 http://en.wikipedia.org/wiki/Correlated_subquery