我有一张'价格表'。我试图从另一个名为'rate_cycles'的表中加入一个列new_rate,它应该基于某些条件。我在创建这样的查询时遇到了麻烦。 价目表包含前缀,以下是两个表的样本。如果来自价格表的一对国家+地区匹配,我将从周期加入'new_rate'。
如果(在循环表中)前缀为空并通知0我需要从该行加入“new_rate”。如果通知(通知= 1),我需要查找(如果存在)下一行通知为0.如果在该行中前缀为null,则我从该行加入new_rate,但如果前缀不为null则为new_rate值我需要前缀为null并且通知为1的最后一行(如果存在,则为否则为null)。
我怎么能实现这个目标?感谢。
价目表
+----+--------------+---------+--------------+---------+-----------+----------+-----------+----------+--------+---------------------+----------------+ | 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 | +----+------------+---------+--------------+----------+---------------------+---------------+---------+----------+---------+
答案 0 :(得分: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