将2个SELECT的结果合二为一

时间:2019-05-27 09:48:45

标签: mysql join select

我有2个选择:

SELECT * 
FROM driverdetails 
WHERE driverdetails.driverid = '32bbdbe8-eda0-32a5-92c0-f9215e24edcf'

SELECT * 
FROM location 
WHERE location.utc = (  SELECT MAX(location.utc) 
                        FROM location 
                        WHERE location.driverid = '32bbdbe8-eda0-32a5-92c0-f9215e24edcf'
                    );

我正在尝试得到一个类似的结果: select1中的列+ select 2中的列

如何通过保留示例中的所有条件来逐列获得列?

2 个答案:

答案 0 :(得分:0)

您应该尝试使用子选择,然后:

SELECT
     (SELECT * 
     FROM driverdetails 
     WHERE driverdetails.driverid = '32bbdbe8-eda0-32a5-92c0-f9215e24edcf'
     ),
     (SELECT * 
     FROM location 
     WHERE location.utc = (  SELECT MAX(location.utc) 
                        FROM location 
                        WHERE location.driverid = '32bbdbe8-eda0-32a5-92c0-f9215e24edcf'
     );

UPD:编辑表格以按某列进行匹配后,我建议您使用上面的答案

答案 1 :(得分:0)

SELECT * 
FROM driverdetails 
INNER JOIN location ON location.driverid = driverdetails.driverid
WHERE driverdetails.driverid = '32bbdbe8-eda0-32a5-92c0-f9215e24edcf'

在上面,您将加入其中。但是根据您的情况,它需要一个MAX(location.utc)

为此,您需要在查询上创建一个临时表

SELECT * 
FROM driverdetails 
INNER JOIN (
             SELECT *,MAX(location.utc) as maxUTC 
             FROM location 
             GROUP BY location.driverid 
           ) as location_temp ON location_temp.driverid = driverdetails.driverid
WHERE driverdetails.driverid = '32bbdbe8-eda0-32a5-92c0-f9215e24edcf'

希望这对您有用。