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

标签 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

如何在同一查询中使用创建的别名添加按计数 ASC 排序

谢谢。

最佳答案

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

关于mysql - 在同一个 sql 查询中使用别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23394682/

相关文章:

php - MySQL 合并多个表的查询结果

mysql - 使用 phpMyAdmin 备份 MySQL

c# - 如何从类中的函数使用 C# 访问 MySQL 数据库?

php - 数据库查询未按预期返回 php pdo

mysql - 从子集查询

php - MySQL - 得到两个不同的结果(PHP 和 phpMyAdmin)

mysql - 分隔符在 MySQL 5.1 上不起作用

Mysql 按两列进行分组,其中条件为第三列的最大日期

mysql - 如何在旧版 MySQL(例如 5.0 或 5.1)中强制使用外键(ID1、ID2)?

sql - 使用主键更改列数据类型