与任何其他sql语法结合使用

时间:2016-09-19 06:01:12

标签: mysql sql

我想在sql语法中将查询与其他查询结合起来,我想用

显示数据
SELECT * 
FROM tx_log 
WHERE (tx_command = 'prepaid-response' AND tx_status = 'OK') 
OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
OR (tx_command = 'cek-response' AND tx_status = 'NOK') 
  AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01';

上面的语法清除不显示错误消息,但显示的数据与指定的日期不匹配。在上面我将显示2016-09-19至2016-09-20的数据,但实际出现的日期不是。

谢谢。

3 个答案:

答案 0 :(得分:1)

试试这个

SELECT * 
FROM tx_log 
WHERE ((tx_command = 'prepaid-response' AND tx_status = 'OK') 
OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
OR (tx_command = 'cek-response' AND tx_status = 'NOK')) 
AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01';

你错过了外括号()。当所有OR条件结束时。

答案 1 :(得分:1)

SELECT * 
FROM tx_log 
WHERE ( (tx_command = 'prepaid-response' AND tx_status = 'OK') 
OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
OR (tx_command = 'cek-response' AND tx_status = 'NOK') )
  AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01';

如果OR加入的任何条件返回true,则无论其他条件是返回true还是false,该行都将包含在结果中。将OR加入的所有条件包装到上面查询中给出的单个条件中。

答案 2 :(得分:1)

"和"优先于" OR" 所以你的查询是:

SELECT * 
FROM tx_log 
WHERE (tx_command = 'prepaid-response' AND tx_status = 'OK') 
OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
OR ( (tx_command = 'cek-response' AND tx_status = 'NOK') 
      AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01'
   )

所以时间戳过滤器仅适用于" last"或条件

试试这个:

SELECT * 
FROM tx_log 
WHERE (
       (tx_command = 'prepaid-response' AND tx_status = 'OK') 
    OR (tx_command = 'postpaid-response' AND tx_status = 'OK') 
    OR (tx_command = 'cek-response' AND tx_status = 'NOK')
    ) 
    AND tx_timestamp between '2016-09-19 00:00:01' and '2016-09-20 00:00:01'