2014-09-29 81 views
1

我有一個「價格表」。我試圖從另一個名爲'rate_cycles'的表中加入一列new_rate,它應該基於某些條件。我有麻煩創建這樣的查詢。 價格表包含前綴,這裏是兩個表中的樣品。如果來自價格表匹配的國家+地區對,我將從週期加入'new_rate'。mysql在不同條件下從另一個表加入列

如果(在循環表中)前綴爲空並通知0我需要從該行加入'new_rate'。如果它被通知(通知= 1),我需要找到,如果存在,通知爲0的下一行。如果在該行中前綴爲空,則我從該行加入new_rate,但是如果前綴不爲null,則對於new_rate值我需要最後一行前綴爲空,通知爲1(如果存在,如果不是則爲空)。

我怎麼能做到這一點?謝謝。

表Pricelist

 
+----+--------------+---------+--------------+---------+-----------+----------+-----------+----------+--------+---------------------+----------------+ 
| id | pricelist_id | country | region  | prefix | is_mobile | is_fixed | is_custom | currency | rate | last_updated  | has_rate_cycle | 
+----+--------------+---------+--------------+---------+-----------+----------+-----------+----------+--------+---------------------+----------------+ 
| 2 |   1 | Albania | Fixed ALBTEL | 3554249 |   0 |  0 |   0 | USD  | 0.0000 | 2014-09-23 09:48:00 |    1 | 
| 3 |   1 | Albania | Fixed ALBTEL | 3554250 |   0 |  0 |   0 | USD  | 0.0000 | 2014-09-23 09:48:00 |    1 | 
| 4 |   1 | Albania | Fixed ALBTEL | 3554251 |   0 |  0 |   0 | USD  | 0.0000 | 2014-09-23 09:48:00 |    1 | 
| 5 |   1 | Albania | Fixed ALBTEL | 3554252 |   0 |  0 |   0 | USD  | 0.0000 | 2014-09-23 09:48:00 |    1 | 
+----+--------------+---------+--------------+---------+-----------+----------+-----------+----------+--------+---------------------+----------------+ 

週期表

 
+----+------------+---------+--------------+----------+---------------------+---------------+---------+----------+---------+ 
| id | carrier_id | country | region  | new_rate | activation_date  | update_status | prefix | notified | grouped | 
+----+------------+---------+--------------+----------+---------------------+---------------+---------+----------+---------+ 
| 1 |   15 | Albania | Fixed ALBTEL | 1.0000 | 2014-09-30 03:48:00 | NEW   | NULL |  0 |  0 | 
| 2 |   15 | Albania | Fixed ALBTEL | 2.0000 | 2014-10-01 03:48:00 | BLOCKED  | 3554250 |  0 |  0 | 
| 3 |   15 | Albania | Fixed ALBTEL | 3.0000 | 2014-10-02 03:48:00 | DECREASE  | NULL |  0 |  0 | 
| 4 |   15 | Albania | Fixed ALBTEL | 4.0000 | 2014-10-03 03:48:00 | NEW   | 3554250 |  0 |  0 | 
+----+------------+---------+--------------+----------+---------------------+---------------+---------+----------+---------+ 
+2

如果你喜歡,可以考慮下列行爲這個簡單的兩步過程:1.如果您還沒有這樣做,提供適當的DDL(和/或sqlfiddle),這樣我們就可以更方便地複製問題。 2.如果您尚未這樣做,請提供與步驟1中提供的信息相符的所需結果集。 – Strawberry 2014-09-29 09:08:30

+1

@Strawberry,謝謝,我會的。我將在sqlfiddle中創建一個模式。 – infinite 2014-09-29 09:18:02

+0

不要忘記,這個過程有兩個步驟! – Strawberry 2014-09-29 09:24:49

回答

0

這是一個艱難的查詢和管理,使之做什麼,我需要的。如果它可以簡化一點,它會很棒。我正在分享我的解決方案。

 
SELECT p.id,p.country, p.region, p.prefix, p.currency, p.rate, 

    (SELECT CASE WHEN notified = 0 

    THEN (SELECT CASE WHEN prefix IS NULL 
     THEN new_rate 
     ELSE (SELECT CASE WHEN COUNT(new_rate)>0 
       THEN (SELECT new_rate FROM pricelist_rates_cycle WHERE country=p.country and region=p.region and notified=1 and prefix is null order by DATE(activation_date) DESC LIMIT 1) 
       ELSE NULL 
       END 
       FROM pricelist_rates_cycle WHERE country=p.country and region=p.region and notified=1 and prefix is null 
       ) 
     END 
     FROM pricelist_rates_cycle WHERE country=p.country and region=p.region and notified=0 LIMIT 1) 

    ELSE (
      SELECT CASE WHEN COUNT(new_rate)>0 
      THEN IF(prefix is null, new_rate, 
        (SELECT new_rate FROM pricelist_rates_cycle WHERE country=p.country and region=p.region and notified=1 and prefix is null order by DATE(activation_date) DESC LIMIT 1) 
        ) 
      ELSE (SELECT new_rate FROM pricelist_rates_cycle WHERE country=p.country and region=p.region and notified=1 and prefix is null order by DATE(activation_date) DESC LIMIT 1) 
      END 
      FROM pricelist_rates_cycle WHERE country=p.country and region=p.region and notified=0 
     ) 
    END 
    FROM pricelist_rates_cycle WHERE country=p.country and region=p.region LIMIT 1) AS new_rate 

FROM pricelist_15 AS p ORDER BY country, region ASC 
相關問題