我在下面使用此查询加入了4个表。鉴于我的数据如下,它实际上不会返回任何数据,因为TRANSACTION_LINE中没有行对象类型值为6的记录。
我希望此查询从EX_WORK返回默认值key_1,null line_Id,默认值为' OFF'如果查询没有返回任何记录,则在TENDER_CODE上。
我已经尝试过coalesce和ifnull,但似乎它没有用。可以通过左连接而不是内连接来解决这个问题吗?
谢谢!
SELECT w.key_1 as if_entry_no,
l.line_id as line_id,
o.object_export_code as tender_code
FROM EX_WORK w,
transaction_header h,
transaction_line l,
line_object o
WHERE w.key_1 = h.if_entry_no
AND h.transaction_void_flag in (0,8)
AND h.if_entry_no = l.if_entry_no
AND l.line_object_type = 6
AND l.line_action <> 55
AND (l.line_action <> 72 OR h.tender_total = 0)
AND l.line_object = o.line_object
EX_WORK
serial_no key_1
111 2879051
TRANSACTION_HEADER
if_entry_no store_no transaction_void_flag tender_total
2879051 9500 0 0
TRANSACTION_LINE
if_entry_no line_Id line_object_type line_object line_action
2879051 1 14 9109 38
2879051 2 1 9105 99
2879051 3 5 9501 98
2879051 4 11 9111 46
LINE_OBJECT
line_object line_object_type resource_id object_export_code
9105 1 5529 null
9109 1 5533 null
9111 1 5535 null
9501 1 5709 null
答案 0 :(得分:0)
是的,您想要左联接。像这样:
SELECT if_entry_no, line_id, ifnull(o.object_export_code, "OFF") AS tender_code
FROM (SELECT w.key_1 AS if_entry_no, l.line_id AS line_id, l.line_object
FROM EX_WORK w,
transaction_header h,
transaction_line l,
line_object o
WHERE w.key_1 = h.if_entry_no
AND h.transaction_void_flag IN (0, 8)
AND h.if_entry_no = l.if_entry_no
AND l.line_object_type = 6
AND l.line_action <> 55
AND (l.line_action <> 72 OR h.tender_total = 0)) s
LEFT JOIN line_object o ON s.line_object = o.line_object;