with basic_data as ( select * from (select c.wha_container_code wha_container_code,c.ins_carrier_code ins_carrier_code,'换装' type from ship_dock_supervision_record_detail c left join ship_dock_supervision_record a on c.dock_supervision_record_id = a.id left join ship_situation b on a.ship_situation_id = b.id where b.document_id = 1661945853862719489 group by c.id order by cast(SUBSTRING_INDEX(c.wha_number,"-",1) as decimal)) aa union all select * from (select a.transportation_tool_no wha_container_code,a.seal_no ins_carrier_code,'原柜' type from `storage`.sto_outbound_loading a left join ship_situation b on a.outbound_id = b.ship_transport_notice_id where b.document_id = 1661945853862719489 and a.transportation_tool_type = 2) bb ) select (@i:=@i+1) AS 'xh',ifnull(max(basic_data.wha_container_code),'TBA') wha_container_code,ifnull(max(basic_data.ins_carrier_code),'TBA') ins_carrier_code from basic_data,(SELECT @i:=0) AS itable where FIND_IN_SET(type,'换装') mysql如何让这句sql,查询不到数据时所有列显示为tba
时间: 2023-11-12 07:06:06 浏览: 107
可以使用IFNULL函数和LEFT JOIN语句来实现。具体语句如下:
```
SELECT (@i:=@i+1) AS 'xh', IFNULL(basic_data.wha_container_code, 'TBA') AS wha_container_code, IFNULL(basic_data.ins_carrier_code, 'TBA') AS ins_carrier_code
FROM (SELECT * FROM
(SELECT c.wha_container_code wha_container_code, c.ins_carrier_code ins_carrier_code, '换装' type
FROM ship_dock_supervision_record_detail c
LEFT JOIN ship_dock_supervision_record a ON c.dock_supervision_record_id = a.id
LEFT JOIN ship_situation b ON a.ship_situation_id = b.id
WHERE b.document_id = 1661945853862719489
GROUP BY c.id
ORDER BY CAST(SUBSTRING_INDEX(c.wha_number,"-",1) AS DECIMAL)) aa
UNION ALL
SELECT * FROM
(SELECT a.transportation_tool_no wha_container_code, a.seal_no ins_carrier_code, '原柜' type
FROM `storage`.sto_outbound_loading a
LEFT JOIN ship_situation b ON a.outbound_id = b.ship_transport_notice_id
WHERE b.document_id = 1661945853862719489 AND a.transportation_tool_type = 2) bb ) basic_data
LEFT JOIN (SELECT @i:=0) AS itable
WHERE FIND_IN_SET(type, '换装')
```
这里使用了LEFT JOIN将basic_data表连接到一个虚拟表,虚拟表中只有一个变量@i,用于生成行号。IFNULL函数用于在查询结果为空时将列值替换为'TBA'。
阅读全文