如何在SQLite select语句中放入两个WHERE条件?

时间:2015-12-23 12:34:46

标签: sql sqlite

我的问题是在一个获取Table1数据库的SELECT语句中。

假设我有以下Table2:ref_no,name,start_date

我也假设table2.ref_no:ref_no,付款

我想选择table2.paymenttable1.start_datetable1.nametable2.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

但没有成功..

1 个答案:

答案 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