2011-06-20 38 views
0

我有一个plans表:帮助SELECT DISTINCT

tariff  | monthly_cost 
-------------------------- 
    Lion  , 15.00 
    Lion  , 20.00 
    Cat  , 15.00 
    Cat  , 20.00 
    Cat Extra , 20.00 
    Cat Extra , 30.00 

运行该SQL查询:

SELECT DISTINCT monthly_cost FROM plans; 

,这将是:

monthly_cost 
------------ 
15.00 
20.00 
30.00 

我想会出现的结果像这样:

tariff  | monthly_cost 
-------------------------- 
      , 15.00 
      , 20.00 
    Cat Extra , 20.00 
    Cat Extra , 30.00 

这怎么能做到?

+6

这不是很清楚,这种查询的物理意义是什么? – heximal

+0

你能解释一下你的想法吗?为什么应该对某些记录进行不同处理,以及如何确定哪些记录应该是? – Guffa

+0

你能更准确地知道你想要对数据做什么,为什么你想要这个特定的输出? –

回答

1
select distinct null, monthly_cost 
from plans 
where tariff not like 'Cat Extra' 
union 
select tariff, monthly_cost 
from plans 
where tariff like 'Cat Extra' 

肮脏的,但有效率。

+0

只要'选择不同的null,monthly_cost'?咦? – MatBailie

+0

哦,你输入答案的第一行,提交它,然后编辑它?没有+1的偷偷摸摸... – MatBailie

+0

是的,它逃脱了我。 –