mysql - 为什么我不能在 WHERE 中使用我的列,而我可以在 GROUP BY 中使用?

标签 mysql sql group-by where-clause

MySQL 不允许在 WHERE 中使用 date_sent_at 而在 GROUP BY 中允许使用它有什么特别的原因吗?

SELECT
    CONCAT(type, '_', IF(om.is_auto, 'auto', 'user')) AS subtype,
    DATE(om.sent_at) AS date_sent_at,
    COUNT(om.id) AS sends
FROM outgoing_message om INNER JOIN outgoing_message_customers AS omc
    ON om.id = omc.outgoing_message_id
WHERE
    omc.customer_id = 2
    AND
    om.status <> 'pending'
    AND
    date_sent_at >= '2012-01-01'
GROUP BY subtype, date_sent_at

编辑:我知道(当然)我可以替换为 DATE(om.sent_at) >= '2012-01-01'

最佳答案

你不能在where中使用别名但是可以在GROUP BY中使用

写在mysql文档在这里:

A select_expr can be given an alias using AS alias_name. The alias is used as the expression's column name and can be used in GROUP BY, ORDER BY, or HAVING clauses. For example:

希望这对您有所帮助。

关于mysql - 为什么我不能在 WHERE 中使用我的列,而我可以在 GROUP BY 中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11559137/

相关文章:

sql - 如何限制内部查询中特定列值返回的记录数?

Mysql语法错误

c++ - 如何对列表执行 GroupBy Sum 查询?

mysql - 从 MySQL 中选择第 n 个百分位数

mysql - 给定 3 个表,我必须找到这个特定的查询,我该怎么做?

Python PyQt5 : Can I also insert a QPushButton into a database via SQL?

PHP CSV 上传到 MySQL 不执行任何操作

sql - 如何在 Postgres 中加入 jsonb 数组元素?

mysql - mysql修改日期格式的方法

python - SQLAlchemy:组内不同