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不在主表中。所以我不能使用自我左连接。我想知道是否有更好的方法。
PS:几乎未经检验的,所以你可能需要在这里解决一个错字,有 – deroby 2012-03-26 12:12:14
修正了几件事情。 SQL-Server的'ISNULL()'在MySQL中具有等效的'IFNULL()'。或者ANSI'COALESCE()'在两者中都起作用。 – 2012-03-26 12:28:27
啊,thx,再次学到了一些东西......我会尽量记住它。 – deroby 2012-03-26 12:38:40