从MySQL DB中选择数据

时间:2013-08-06 05:07:06

标签: mysql sql select

在选择查询中,如果事务类型1存在,我想获得结果,它不检查事务类型3,否则应选择事务类型3.

基本上,我必须选择那些在一段时间内没有重新填充帐户并且必须使其余额失效的客户。但是在注册时,我正在分配交易类型为3的初始余额。如果客户在该期限之前没有重新填写账户并且具有我在注册时分配的初始余额,那么我也需要使该余额到期。

SELECT customer.* FROM `customer` 
LEFT JOIN transaction ON (customer.ID=transaction.CUSTOMER_ID) 
WHERE transaction.CREATED_AT NOT Between '2013-02-06 00:00:00' AND '2013-08-05 23:59:59' 
AND transaction.TRANSACTION_TYPE_ID=1
AND transaction.transaction_status_id=3 AND customer.CUSTOMER_STATUS_ID=3 
GROUP BY customer.ID

此查询工作正常,并向我提供未重新填充其帐户的客户的记录,但是当我添加OR Transaction_type_id = 3时,它不会提供所需的结果。

1 个答案:

答案 0 :(得分:0)

使用OR时是否包括括号?

SELECT customer.* FROM `customer` 
LEFT JOIN transaction ON (customer.ID=transaction.CUSTOMER_ID) 
WHERE transaction.CREATED_AT NOT Between '2013-02-06 00:00:00' AND '2013-08-05 23:59:59' 
AND (transaction.TRANSACTION_TYPE_ID=1 OR transaction.TRANSACTION_TYPE_ID=3)
AND transaction.transaction_status_id=3 AND customer.CUSTOMER_STATUS_ID=3 
GROUP BY customer.ID