何时何地的条件

when condition in where

我有两张桌子。一个产品有 2 个价格,我需要比较 max_count 和数量,应该 select 只有一个记录 我的查询无效..

   SELECT product_id, quantity, max_count 
    FROM table1 WHERE WHEN max_count > quantity 
    THEN price_type = 1 ELSE price_type = 2 
    LEFT JOIN table2 ON table1.product_id = table2.product_id

只需使用基本逻辑即可。您查询的子句顺序也有误:

SELECT product_id, quantity, max_count 
FROM table1 LEFT JOIN
     table2
     ON table1.product_id = table2.product_id
WHERE (max_count > quantity AND price_type = 1) OR
      (max_count <= quantity AND price_type = 2);

如果max_count可以取NULL的值,逻辑稍微复杂一些。