Please see link to get table in better format
在MySQL数据库中,我有两个表'VEHICLE'和'VEHICLE_CLASS',如下所示:
VEHICLE
-------------------------------------------
VEHICLE_ID | VEHICLE_CLASS_ID | STATUS
-------------------------------------------
vehicle_001 | vehicle_class_001 | 0
vehicle_002 | vehicle_class_002 | 1
vehicle_003 | vehicle_class_003 | 2
vehicle_004 | vehicle_class_001 | 0
vehicle_005 | vehicle_class_002 | 2
vehicle_006 | vehicle_class_001 | 0
vehicle_007 | NULL | 1
----------------------------------------------
VEHICLE_CLASS
------------------------------------------
VEHICLE_CLASS_ID | VEHICLE_CLASS_NAME
-----------------------------------------
vehicle_class_001 | ABC
vehicle_class_002 | BCD
vehicle_class_003 | EFG
vehicle_class_004 | XYZ
vehicle_class_005 | PQR
vehicle_class_006 | STU
---------------------------------------
我需要一个查询来获取结果行,如下所示:
-----------------------------------------------------------------------
VEHICLE_CLASS_NAME | COMPLETED_ VEHICLES | NOT_COMPLETED_ VEHICLES
---------------------------------------------------------------------------
ABC | |vehicle_001, vehicle_004, vehicle_006
BCD | vehicle_005 |vehicle_002
EFG | vehicle_003 |
这是我写的查询,但它没有给我正确的行。
SELECT
veh.VEHICLE_CLASS_ID,
vehclass.VEHICLE_CLASS_NAME,
IF(STATUS=2, GROUP_CONCAT(`VEHICLE_ID`),'') COMPLETED_ VEHICLES,
IF(STATUS<2,GROUP_CONCAT(`VEHICLE_ID`),'') NOT_COMPLETED_ VEHICLES
FROM
VEHICLE veh
LEFT JOIN VEHICLE_CLASS vehclass
on veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID
GROUP BY
veh.VEHICLE_CLASS_ID
having
veh.VEHICLE_CLASS_ID is not null;
请帮帮我,我怎样才能获得结果行。 谢谢。
答案 0 :(得分:1)
SELECT
vehclass.VEHICLE_CLASS_NAME,
GROUP_CONCAT(CASE veh.STATUS WHEN 2 THEN `VEHICLE_ID` ELSE NULL END) COMPLETED_VEHICLES,
GROUP_CONCAT(CASE veh.STATUS WHEN 2 THEN NULL ELSE `VEHICLE_ID` END) NOT_COMPLETED_VEHICLES
FROM VEHICLE veh
LEFT JOIN VEHICLE_CLASS vehclass
ON veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID
GROUP BY veh.VEHICLE_CLASS_ID
HAVING veh.VEHICLE_CLASS_ID is not null;
SELECT
vehclass.VEHICLE_CLASS_NAME,
STUFF((SELECT CASE v.STATUS WHEN 2 THEN ',' + v.VEHICLE_ID ELSE '' END
FROM Vehicle v
WHERE v.VEHICLE_CLASS_ID = veh.VEHICLE_CLASS_ID
FOR XML PATH (''))
, 1, 1, '') as COMPLETED_VEHICLES,
STUFF((SELECT CASE v.STATUS WHEN 2 THEN '' ELSE ',' + v.VEHICLE_ID END
FROM Vehicle v
WHERE v.VEHICLE_CLASS_ID = veh.VEHICLE_CLASS_ID
FOR XML PATH (''))
, 1, 1, '') as NOT_COMPLETED_VEHICLES
FROM VEHICLE veh
LEFT JOIN VEHICLE_CLASS vehclass
ON veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID
GROUP BY veh.VEHICLE_CLASS_ID, vehclass.VEHICLE_CLASS_NAME
HAVING veh.VEHICLE_CLASS_ID is not null;
为了在oracle中实现这一点,请参阅this link以了解使用什么代替GROUP_CONCAT
答案 1 :(得分:1)
SELECT
vechile_class.vechile_class_name,
GROUP_CONCAT(v1.vechile_id) AS COMPLETED_VEHICLES,
GROUP_CONCAT(v2.vechile_id) AS NOT_COMPLETED_VEHICLES
FROM vechile_class
LEFT JOIN vechile v1 ON
v1.vechile_class_id = vechile_class.vechile_class_id AND v1.status = 2
LEFT JOIN vechile v2 ON
v2.vechile_class_id = vechile_class.vechile_class_id AND v2.status <> 2
GROUP BY vechile.vechile_class_id