Example Usage of or Statement in MySQL

  • 2021-09-11 21:39:02
  • OfStack

1. The use of or syntax in mysql, and the use of or in mysql syntax. The project encounters a pit, and the query error of traversing and issuing reward data! ! !


$sql = 'SELECT 
        * 
      FROM 
        `vvt_spread_doubleegg_exchange_award` AS p
      WHERE
        p.`act_type` = 4 or p.`act_type` = 5
      AND
        p.`user_id` = ' .$user_id
      ;

The or syntax 1 in sql is generally used for queries with multiple conditions, and the above syntax query is equivalent to two data sets queried by sql.


 $sql = 'SELECT * FROM `vvt_spread_doubleegg_exchange_award` AS p WHERE p.`act_type` = 4;
 $sql = 'SELECT * FROM `vvt_spread_doubleegg_exchange_award` AS p WHERE p.`act_type` = 5 AND p.`user_id` = ' .$user_id;

2. To query act_type = 4 and user_id = 11123 or equal to p. ` act_type ` = 5 and user_id = 11123, condition 1 on both sides of or must be added ().


$sql = 'SELECT 
        * 
      FROM 
        `vvt_spread_doubleegg_exchange_award` AS p
      WHERE
         ( p.`act_type` = 4 or p.`act_type` = 5 ) 
      AND
        p.`user_id` = ' .$user_id
      ;

Summarize


Related articles: