帮助SELECT DISTINCT

时间:2011-06-20 12:04:54

标签: mysql sql database

我有一个plans表:

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

怎么办呢?

1 个答案:

答案 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'

肮脏但有效率。