2014-02-11 99 views
0

先進的謝謝!如何將這三個MYSQL查詢合併爲一個?

我有這個疑問:

SELECT da_clubs_categories.name AS cat_name, 
    da_clubs_categories.id AS cat_id, 
    da_deals.id, 
    da_brands.id AS brand_id, 
    da_brands.name AS brand_name 
    FROM da_clubs_categories, da_deals, da_brands 
    WHERE da_clubs_categories.fk_club_id = da_brands.fk_club_id 
    AND da_brands.fk_club_id = 6 
    AND da_deals.fk_brand_id = da_brands.id 
    AND FIND_IN_SET(da_clubs_categories.id, da_deals.categories) > 0 
    AND da_brands.active = 1 
    AND da_deals.active = 1 
    AND da_deals.date_start <= CURRENT_DATE() 
    AND CURRENT_DATE() <= da_deals.date_end 
    ORDER BY da_clubs_categories.name ASC, da_deals.date_start DESC 

結果:

+--------------+--------+----+----------+------------+ 
| cat_name  | cat_id | id | brand_id | brand_name | 
+--------------+--------+----+----------+------------+ 
| Deporte  |  19 | 22 |  57 | Marca1  | 
| Deporte  |  19 | 23 |  57 | Marca1  | 
| Deporte  |  19 | 24 |  57 | Marca1  | 
| Deporte  |  19 | 25 |  57 | Marca1  | 
| Deporte  |  19 | 26 |  57 | Marca1  | 
| Deporte  |  19 | 27 |  57 | Marca1  | 
| Deporte  |  19 | 28 |  57 | Marca1  | 
| Deporte  |  19 | 29 |  57 | Marca1  | 
| Gastronomía |  20 | 22 |  57 | Marca1  | 
| Salud  |  21 | 22 |  57 | Marca1  | 
+--------------+--------+----+----------+------------+ 

後來,另一個查詢:

SELECT value AS 'like' 
FROM da_logs 
WHERE fk_deal_id = 22 
AND type = 'deal_like' 

結果:

+------+ 
| like | 
+------+ 
| ::1 | 
+------+ 

後來,另一個查詢:

SELECT value AS 'rating' 
FROM da_logs 
WHERE fk_deal_id = 22 
AND type = 'deal_rating' 

結果:

+------------------------------------------------------------+ 
| rating              | 
+------------------------------------------------------------+ 
| a:2:{s:7:"user_ip";s:3:"::1";s:12:"rating_value";s:1:"3";} | 
+------------------------------------------------------------+ 

我不得不這樣做,使用PHP中的循環,但我想使只有一個單一的查詢,有兩個附加列專表A(我做了2個額外的查詢),你可以看到。

如果類似或評級沒有找到一個特定的交易,然後將其設置爲''。

謝謝你幫助我。

+0

使用'LEFT JOIN'加入這些表。如果沒有匹配,則「LEFT JOIN」將爲列返回「NULL」。 – Barmar

回答

1

使用LEFT JOIN加入da_logs表兩次,一次爲每個type你想要匹配。

SELECT da_clubs_categories.name AS cat_name, 
    da_clubs_categories.id AS cat_id, 
    da_deals.id, 
    da_brands.id AS brand_id, 
    da_brands.name AS brand_name, 
    IFNULL(l1.value, '') AS like, 
    IFNULL(l2.value, '') AS rating 
    FROM da_clubs_categories 
    INNER JOIN da_brands ON da_clubs_categories.fk_club_id = da_brands.fk_club_id 
    INNER JOIN da_deals ON da_deals.fk_brand_id = da_brands.id 
     AND FIND_IN_SET(da_clubs_categories.id, da_deals.categories) > 0 
    LEFT JOIN da_logs AS l1 ON l1.fk_deal_id = da_deals.id AND l1.type = 'deal_like' 
    LEFT JOIN da_logs AS l2 ON l2.fk_deal_id = da_deals.id AND l2.type = 'deal_rating' 
    WHERE da_brands.fk_club_id = 6 
    AND da_brands.active = 1 
    AND da_deals.active = 1 
    AND da_deals.date_start <= CURRENT_DATE() 
    AND CURRENT_DATE() <= da_deals.date_end 
    ORDER BY da_clubs_categories.name ASC, da_deals.date_start DESC 
+0

非常感謝你的男人!上帝以耶穌的名義祝福你! –