使用if else条件从4个表中检索数据

时间:2014-08-12 07:09:03

标签: mysql

我有4张桌子。

td_product

|---------------------------------------------------------------------------------|
|  product_id |  product_title  |  collection_id  |  has_item   |  product_price  |
|---------------------------------------------------------------------------------|
|     1       |    Denim Pant   |                 |     1       |  400            |
|---------------------------------------------------------------------------------|
|     2       |    Cool Shirt   |                 |     0       |  500            |
|---------------------------------------------------------------------------------|

td_item

|---------------------------------------------------|
|   item_id  |   product_id  |   color  |   price   |
|---------------------------------------------------|
|     1      |       1       |    RED   |    450    |
|---------------------------------------------------|
|     2      |       1       |    GREEN |    500    |
|---------------------------------------------------|

td_collection

|------------------------------------------------------------|-----------|
|  collection_id  |   collection_type  |   collection_title  |           |
|------------------------------------------------------------------------|
|       1         |       1            |    Summer           |           |
|------------------------------------------------------------------------|
|       2         |       2            |    DENIM            |           |
|------------------------------------------------------------------------|

td_collectioncondition

|---------------------------------------------------------------------------------------------|
|   condition_id  |   collection_id  |   key             |   condition     |    constraint    |
|--------------------------------------------------------------------------|------------------|
|       1         |          2       |   product_title   |   contains      |     Denim        |
|--------------------------------------------------------------------------|------------------|
|       2         |          2       |   product_price   |  is_less_than   |      500         |
|---------------------------------------------------------------------------------------------|

现在让我解释一下:

  • 如果td_product.has_item = 1,那么td_item表中会有项目

  • 如果td_collection.collection_type = 2,那么它将有条件地自动获取产品,如果是1那么它将是手动的(产品需要手动添加到集合中。

现在,如果条件是价格应该小于500,那么产品(其has_item = 0)将以低于0的价格列出。

但是如果has_item = 1,那么任何商品的定价低于500的产品,那么该产品将被列出。

所以我有两个问题

第一个;

for product with has_item = 0

SELECT * FROM td_product WHERE
            ( ( product_title LIKE '%Denim%' ) 
              AND (product_price < '2000' ) ) 
           AND product_id > 0 AND active = '1' 

适用于has_item = 1的产品

SELECT * FROM td_product WHERE
        ( ( product_title LIKE '%Denim%' ) 
          AND ((SELECT max(item_price) FROM td_item WHERE td_item.product_id = td_product.product_id) < '2000' ) ) 
       AND product_id > 0 AND active = '1' 

现在我想进行查询以检查两个条件

我的意思是如果has_item = 0,那么

product_price < 200

如果has_item = 1

(SELECT max(item_price) FROM td_item WHERE td_item.product_id = td_product.product_id) < '2000' )

说出在查询中使用某种if else的东西。

2 个答案:

答案 0 :(得分:1)

我希望这有帮助:

SELECT 
* 
FROM td_product 
WHERE
     product_title LIKE '%Denim%' ) 
     AND (IF(has_item=0,product_price,(SELECT max(item_price) FROM td_item WHERE td_item.product_id = td_product.product_id)) < 2000)
     AND product_id > 0 
     AND active = 1

答案 1 :(得分:0)

使用左连接:

SELECT p.* FROM td_product p
LEFT JOIN td_item i ON i.product_id = p.product_id
WHERE
    p.product_title LIKE '%Denim%' ) 

   AND p.product_id > 0 AND p.active = '1' 
GROUP BY p.product_id
HAVING (p.has_item = 0 and p.product_price < 2000) or  (p.has_item = 1 and max(i.item_price) < 2000)

这允许您在查询中不使用if else的情况下测试这两个条件。