当条件在哪里

时间:2015-03-09 11:50:15

标签: sql oracle11g

我有两张桌子。一个产品有2个价格,我需要比较max_count和数量,应该只选择一个记录 我的查询不起作用..

   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

1 个答案:

答案 0 :(得分:0)

只需使用基本逻辑。查询的子句也是错误的顺序:

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的值,则逻辑稍微复杂一些。