2012-03-26 82 views
0

以下内容按预期工作。它显示该国家的默认每千次展示费用和每次点击费用,如果原始表格中缺失。 我正在使用临时表,我想知道它是否可以在不使用临时表的情况下完成。SQL语句的重构

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不在主表中。所以我不能使用自我左连接。我想知道是否有更好的方法。

回答

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 = '' 
+0

PS:几乎未经检验的,所以你可能需要在这里解决一个错字,有 – deroby 2012-03-26 12:12:14

+0

修正了几件事情。 SQL-Server的'ISNULL()'在MySQL中具有等效的'IFNULL()'。或者ANSI'COALESCE()'在两者中都起作用。 – 2012-03-26 12:28:27

+0

啊,thx,再次学到了一些东西......我会尽量记住它。 – deroby 2012-03-26 12:38:40