在同一个sql查询中使用别名

时间:2014-04-30 17:40:57

标签: mysql sql phpmyadmin

我有这个SQL查询:

select GEN_source, count(*) as count, sum(100) / total as percentage
from tics
cross join (select count(*) as total from t_cs) x
group by 1

如何在同一查询中使用创建的别名添加order by count ASC

感谢。

1 个答案:

答案 0 :(得分:0)

Count是一个保留字,因此需要将其封装在背景上或更改名称

select GEN_source, count(*) as `count`, sum(100) / total as percentage
from tics
cross join (select count(*) as total from t_cs) x
group by 1
ORDER By `count`

select GEN_source, count(*) as cnt, sum(100) / total as percentage
from tics
cross join (select count(*) as total from t_cs) x
group by 1
ORDER By cnt