我有这样的查询:
SELECT a.ID_ITEM_INSPECTION, a.NOMOR_ITEM_INSPECTION, a.NAMA_ITEM_INSPECTION,
(SELECT d.NO_INSPECTION FROM tb_master_inspection_report d WHERE d.NO_INSPECTION = 66) AS NO_INSPECTION,
b.ID_ITEM_INSPECTION, b.CONDITION, b.PCS, b.`ALIAS`
FROM `db_depo`.`tb_item_inspection_report` a
LEFT JOIN (SELECT b.CONDITION, b.ID_ITEM_INSPECTION, b.PCS, c.ALIAS
FROM tb_detail_inspection_report b
LEFT JOIN tb_item_condition_detail_inspection_report c
ON b.CONDITION = c.ID_ITEM
WHERE b.NO_INSPECTION = 66) b
ON b.ID_ITEM_INSPECTION = a.ID_ITEM_INSPECTION
WHERE a.TIPE = 'T'
ORDER BY a.NOMOR_ITEM_INSPECTION ASC LIMIT 1000;
它给了我一张这样的表格:
+--------------------+-----------------------+---------------------------------+---------------+--------------------+-----------+------+-------+
| ID_ITEM_INSPECTION | NOMOR_ITEM_INSPECTION | NAMA_ITEM_INSPECTION | NO_INSPECTION | ID_ITEM_INSPECTION | CONDITION | PCS | ALIAS |
+--------------------+-----------------------+---------------------------------+---------------+--------------------+-----------+------+-------+
| 1 | 1 | Protection Box Cover | 66 | NULL | NULL | NULL | NULL |
| 2 | 2a | Manlid | 66 | NULL | NULL | NULL | NULL |
| 3 | 2b | Swing bolts | 66 | NULL | NULL | NULL | NULL |
| 28 | 2c | Manlid Gasket | 66 | NULL | NULL | NULL | NULL |
| 4 | 3a | PV Valve / Flame Trap / Gauge | 66 | NULL | NULL | NULL | NULL |
| 5 | 3b | Rupture Disc | 66 | 5 | 6 | 1 | M |
| 6 | 4a | Top Outlet (Flange/Bolt/Nut) | 66 | NULL | NULL | NULL | NULL |
| 12 | 4b | Syphone Tube/Butterfly | 66 | 12 | 6 | 1 | 0 |
| 7 | 5 | Top Operated Valve | 66 | 7 | 6 | 1 | 0 |
| 8 | 6 | Dipstick | 66 | 8 | 3 | 1 | C |
| 9 | 7 | Air Line Valve (Ball Butterfly) | 66 | NULL | NULL | NULL | NULL |
| 10 | 8 | Calibration Chart | 66 | NULL | NULL | NULL | NULL |
| 11 | 9 | Walkway | 66 | NULL | NULL | NULL | NULL |
+--------------------+-----------------------+---------------------------------+---------------+--------------------+-----------+------+-------+
13 rows in set (0.00 sec)
请注意,有一条值为0的记录,
但是,如果我将左连接命令分开:
SELECT b.CONDITION, b.ID_ITEM_INSPECTION, b.PCS, c.ALIAS
FROM tb_detail_inspection_report b
LEFT JOIN tb_item_condition_detail_inspection_report c
ON b.CONDITION = c.ID_ITEM
WHERE b.NO_INSPECTION = 66
+-----------+--------------------+------+-------+
| CONDITION | ID_ITEM_INSPECTION | PCS | ALIAS |
+-----------+--------------------+------+-------+
| 6 | 5 | 1 | M |
| 6 | 12 | 1 | M |
| 6 | 7 | 1 | M |
| 3 | 8 | 1 | C |
| 6 | 23 | 1 | M |
+-----------+--------------------+------+-------+
5 rows in set (0.00 sec)
刚刚发生了什么? 任何帮助或建议都非常感激。
答案 0 :(得分:0)
如果你使用这样的正确连接:
SELECT a.ID_ITEM_INSPECTION, a.NOMOR_ITEM_INSPECTION, a.NAMA_ITEM_INSPECTION,
(SELECT d.NO_INSPECTION FROM tb_master_inspection_report d WHERE d.NO_INSPECTION = 66) AS NO_INSPECTION,
b.ID_ITEM_INSPECTION, b.CONDITION, b.PCS, b.`ALIAS`
FROM `db_depo`.`tb_item_inspection_report` a
right JOIN (SELECT b.CONDITION, b.ID_ITEM_INSPECTION, b.PCS, c.ALIAS
FROM tb_detail_inspection_report b
leftJOIN tb_item_condition_detail_inspection_report c
ON b.CONDITION = c.ID_ITEM
WHERE b.NO_INSPECTION = 66) b
ON b.ID_ITEM_INSPECTION = a.ID_ITEM_INSPECTION
WHERE a.TIPE = 'T'
ORDER BY a.NOMOR_ITEM_INSPECTION ASC LIMIT 1000;