我有一张“价目表”表。我正在尝试从另一个名为“rate_cycles”的表中加入一列 new_rate,它应该基于特定条件。我在创建这样的查询时遇到了麻烦。 价目表包含前缀,这里是两个表中的示例。如果价目表中的一对国家 + 地区匹配,我将从循环中加入“new_rate”。
如果(在循环表中)前缀为空并通知 0,我需要从该行加入“new_rate”。如果它被通知(通知= 1)我需要找到,如果存在,通知的下一行是0。如果在该行中前缀为空,那么我从该行加入 new_rate,但如果前缀不为空,那么对于 new_rate 值我需要前缀为空且通知为 1 的最后一行(如果存在,如果不存在则为空)。
我怎样才能做到这一点?谢谢。
价目表
+----+--------------+---------+--------------+---------+-----------+----------+-----------+----------+--------+---------------------+----------------+ | 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 | +----+------------+---------+--------------+----------+---------------------+---------------+---------+----------+---------+
最佳答案
这是一个艰难的查询,并设法让它做我需要的。如果能简化一点就好了。我正在分享我的解决方案。
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
关于mysql在不同条件下从另一个表连接列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26096152/