依赖于第一个子查询的MySQL JOIN 2子查询

时间:2019-06-21 15:17:22

标签: mysql sql subquery left-join

我一天中大部分时间都在工作,为了“今天能到达某个地方”,我开始将查询分解为多个部分,以便可以将它们组合在一起以进行发言。我有第一个查询,可以根据需要运行(使用联接和子查询),我只需要弄清楚如何做类似的事情即可将第二个查询的结果包含在查询1的列中

查询1:

SELECT
u.user_id,
u.firstname,
u.surname,
d.vehicleID,
vehicle.REG
FROM
users AS u
LEFT JOIN vehicle_drivers AS d ON u.user_id = d.driverID AND d.`timestamp` = (SELECT MAX(d2.`timestamp`)
                         FROM vehicle_drivers d2
                         WHERE d2.driverID = u.user_id
                        )
LEFT JOIN vehicle ON d.vehicleID = vehicle.ID
WHERE
    u.hide <> 1
AND remote_logon = 1

然后我需要查询2的结果:

SELECT
    a.referance AS storeID,
FROM
    qrTag_linktable a
INNER JOIN qrTag_linktable AS b ON b.tagID = a.tagID WHERE a.module = 1 and  b.referance = ****d.vehicleID****

这是qrTag_linktable

+----+-------+--------+-----------+
| ID | tagID | module | referance |
+----+-------+--------+-----------+
|  3 |     1 |      1 |         1 |
|  8 |     1 |      2 |        67 |
|  9 |    11 |      1 |         4 |
| 10 |    11 |      2 |        77 |
+----+-------+--------+-----------+

来自Query2的结果(storeID)必须位于查询1的一列中。查询2需要d.vehicleID的值才能起作用。我知道我不可能走得那么远,而且我一直在玩子查询,但是我似乎可以将它们组合在一起!我还应该提到查询一个可能返回NULL结果,这是可以的,因此需要为LEFT Join(?)

编辑2: 这是一个示例,如果需要的结果:

+---------+-----------+---------+-----------+---------+----------+
| user_id | firstname | surname | vehicleID | storeID |   REG    |
+---------+-----------+---------+-----------+---------+----------+
|       2 | Steve     | Jobs    |        67 |       1 | VN64 *** |
|     653 | James     | Smith   |        77 |       4 | *** EUF  |
|     592 | Harry     | Potter  |        72 |         | YM64 *** |
+---------+-----------+---------+-----------+---------+----------+

编辑: 到目前为止,这是我的工作:S

SELECT
    u.user_id,
    u.firstname,
    u.surname,
    d.vehicleID,
    s.storeID,
    vehicle.REG
FROM
    users AS u
LEFT JOIN vehicle_drivers AS d ON u.user_id = d.driverID
AND d.`timestamp` = (
    SELECT
        MAX(d2.`timestamp`)
    FROM
        vehicle_drivers d2
    WHERE
        d2.driverID = u.user_id
)
LEFT JOIN vehicle ON d.vehicleID = vehicle.ID
LEFT JOIN (
    SELECT
        a.referance AS storeID,
        b.referance AS vehicleID
    FROM
        qrTag_linktable a
    INNER JOIN qrTag_linktable AS b ON b.tagID = a.tagID
    WHERE
        a.module = 1
    AND b.referance = d.vehicleID
) AS s ON s.vehicleID = d.vehicleID
WHERE
    u.hide <> 1
AND remote_logon = 1
[Err] 1054 - Unknown column 'd.vehicleID' in 'where clause'

2 个答案:

答案 0 :(得分:0)

只需删除WHERE条件AND b.referance = d.vehicleID,这是多余的,因为在JOIN子句ON s.vehicleID = d.vehicleID中对其进行了处理。注意,您将 b.reference 别名为 s.vechicleID


顺便说一句,如果使用MySQL 8+,请考虑使用CTEs以提高可读性,从而将您的第一个子查询转换为聚合查询的联接。 WITH这种方法,将所有内容都引用为顶级命名源,而没有嵌套的SELECT语句。

WITH max_time AS (
    SELECT driverID,
           MAX(d2.`timestamp`) AS max_time
    FROM vehicle_drivers
    GROUP BY driverID
), 
    tag_link AS (
    SELECT a.referance AS storeID,
           b.referance AS vehicleID
    FROM qrTag_linktable a
    INNER JOIN qrTag_linktable AS b 
        ON b.tagID = a.tagID
    WHERE a.module = 1
)

SELECT
    u.user_id,
    u.firstname,
    u.surname,
    d.vehicleID,
    s.storeID,
    v.REG
FROM users AS u
LEFT JOIN vehicle_drivers AS d 
    ON u.user_id = d.driverID
LEFT JOIN max_time AS m 
    ON u.user_id = m.driverID 
    AND d.`timestamp` = m.max_time
LEFT JOIN vehicle v 
    ON d.vehicleID = v.ID
LEFT JOIN tag_link s 
    ON s.vehicleID = d.vehicleID
WHERE u.hide <> 1
AND remote_logon = 1

答案 1 :(得分:0)

您的问题在这里:

 LEFT JOIN (
        SELECT
            a.referance AS storeID,
            b.referance AS vehicleID
        FROM
            qrTag_linktable a
        INNER JOIN qrTag_linktable AS b ON b.tagID = a.tagID
        WHERE
            a.module = 1
        AND b.referance = d.vehicleID
    ) AS s ON s.vehicleID = d.vehicleID

您在子查询中使用别名“ d.vehicleID”:

在哪里         a。模块= 1     AND b.referance = d.vehicleID