我的问题是在一个获取Table1
数据库的SELECT语句中。
假设我有以下Table2
:ref_no,name,start_date
我也假设table2.ref_no
:ref_no,付款
我想选择table2.payment
,table1.start_date
,table1.name
和table2.payment
table2.ref_no
为空,table1.ref_no
等于{{1}按table2.ref_no
我试过这个:
SELECT ref_no.Table1, name.Table2, tel.Table2
FROM Table1, Table2
WHERE payment.Table1 IS NULL OR
payment.Table1 = '
ORDER BY ref_no.Table2
WHERE table1.ref_no = table2.ref_no
但没有成功..
答案 0 :(得分:3)
我想选择table2.ref_no,table2.payment,table1.start_date和 table1.name其中table2.payment为null,table2.ref_no等于 table1.ref_no按table2.ref_no排序
使用JOIN
代替逗号语法并更改table_name.column_name
的顺序:
SELECT Table2.ref_no, Table1.name, Table2.payment, table1.start_date, Table2.tel
FROM Table1
JOIN Table2
ON Table1.ref_no = Table2.ref_no
WHERE Table2.payment IS NULL
OR Table2.payment = ' '
-- explicit search for space maybe you want '' (empty string)
ORDER BY Table2.ref_no
<小时/> 为了便于阅读,添加别名是一种很好的做法:
SELECT t2.ref_no, t1.name, t2.payment, t1.start_date, t2.tel
FROM Table1 AS t1
JOIN Table2 AS t2
ON t1.ref_no = t2.ref_no
WHERE t2.payment IS NULL
OR t2.payment = ' '
ORDER BY t2.ref_no