重构SQL语句

时间:2012-03-26 11:56:46

标签: mysql sql

以下是按预期工作的。如果原始表中缺少该国家/地区,则会显示该国家/地区的默认CPM和CPC。 我正在使用临时表,我想知道是否可以在不使用临时表的情况下完成。

mysql> create temporary table country_list (country_name varchar(100));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into country_list values ('IN'), ('SA');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

select dt.country_name as country, dt.operator,
if (dt.cpm is null, (SELECT cpm FROM ox_bidding_configuration where country = '' and operator = ''), dt.cpm) as updated_cpm,
if (dt.cpc is null, (SELECT cpc FROM ox_bidding_configuration where country = '' and operator = ''), dt.cpc) as updated_cpc,
if (dt.cpa is null, (SELECT cpa FROM ox_bidding_configuration where country = '' and operator = ''), dt.cpa) as updated_cpa
from (
select a.country_name, b.operator, cpm, cpc, cpa from country_list as a left join ox_bidding_configuration as b on a.country_name = b.country group by b.country, b.operator, cpm, cpc, cpa) as dt;
+---------+----------+-------------+-------------+-------------+
| country | operator | updated_cpm | updated_cpc | updated_cpa |
+---------+----------+-------------+-------------+-------------+
| SA      | NULL     |      1.0000 |      1.0000 |      1.0000 |
| IN      |          |      2.0000 |      2.0000 |      2.0000 |
| IN      | abcd     |     11.0000 |     23.0000 |      4.0000 |
+---------+----------+-------------+-------------+-------------+

国家/地区SA不在主表中。所以我不能使用自我左连接。我想知道是否有更好的方法。

1 个答案:

答案 0 :(得分:2)

对于初学者来说,我站在那里你正在使用(临时)country_list表作为PRIMARY表,并尽可能匹配ox_bidding_configuration(左外连接);因此,您的结果集只包含SA& IN。

虽然我不是100%确定这个语法适用于MYSQL,但是用于TSQL我会将你的查询编写为:

SELECT dt.country_name as country, 
       dt.operator,
       COALESCE(dt.cpm, def.cpm) as updated_cpm,
       COALESCE(dt.cpc, def.cpc) as updated_cpd,
       COALESCE(dt.cpa, def.cpa) as updated_cpa
  FROM (SELECT DISTINCT a.country_name, 
                        b.operator, 
                        b.cpm, 
                        b.cpc, 
                        b.cpa 
          FROM country_list a
          LEFT OUTER JOIN ox_bidding_configuration as b
                       ON b.country = a.country_name ) dt
  LEFT OUTER JOIN ox_bidding_configuration def
               ON def.country = '' 
              AND def.operator = ''

恕我直言,这一点更加清晰,而且可能会稍快一些,因为默认配置只需要提取一次。

要删除country_list表,您可以将其转换为内联查询,但坦率地说,我没有看到它的好处。

下面的内容:

SELECT dt.country_name as country, 
       dt.operator,
       COALESCE(dt.cpm, def.cpm) as updated_cpm,
       COALESCE(dt.cpc, def.cpc) as updated_cpd,
       COALESCE(dt.cpa, def.cpa) as updated_cpa
  FROM (SELECT DISTINCT a.country_name, 
                        b.operator, 
                        b.cpm, 
                        b.cpc, 
                        b.cpa 
          FROM (SELECT 'SA'  AS country_name
                 UNION ALL 
                SELECT 'IN') a
          LEFT OUTER JOIN ox_bidding_configuration as b
                       ON b.country = a.country_name ) dt
  LEFT OUTER JOIN ox_bidding_configuration def
               ON def.country = '' 
              AND def.operator = ''