Search code examples
mysqldatabaseselectfetch

MySQL many 'and' statements


I have this statement which is the wrong way to do it but how would I get the statement to work (the result of the statement needs to hold all the AND in it)

SELECT a.username, a.first_name, a.last_name
     , b.tx_time, b.account_id
     , a.id 
     , b.table_id, b.tx_type, b.amount 
FROM punter a
   , account_transaction b 
WHERE b.tx_time >= '2011-07-01' 
  AND b.tx_time < '2011-09-30' 
  AND b.account_id = a.id 
  AND b.tx_type = 4 
  AND b.tx_type = 14

Solution

  • AND b.tx_type = 4 AND b.tx_type = 14 will give you empty result set. One column can only have one value at the same time.

    SELECT a.username, a.first_name, a.last_name, 
           b.tx_time, b.account_id, a.id,
           b.table_id, b.tx_type, b.amount 
    FROM punter a 
    INNER JOIN account_transaction b ON b.account_id = a.id
    WHERE b.tx_time BETWEEN '2011-07-01' AND '2011-09-30' AND b.tx_type IN (4,14)