我有用户表(简化):
id | username | url
我正在尝试查找具有相同域名的用户数量,因此我通过 replace
从 url
列中删除 http
文本mysql 函数调用和 group by
剥离的值,但它不起作用。
我正在尝试运行以下查询:
SELECT replace(url, "http://", "") u, count(u) c
FROM users
GROUP BY u
ORDER BY c DESC
我收到错误:
Unknown column 'u' in 'field list'
有什么想法如何在列上运行replace
并仍然按替换值进行分组吗?
最佳答案
您不能在 select
或 where
子句中使用别名。
It is not allowable to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed.
使用
select replace(url, "http://", "") u, count(*) c
关于mysql - 如何将 MySQL 的 `group by` 与已在 select 调用中用 `replace` 修改的列一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26799025/