mysql – ‘having子句’中的未知列
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql – ‘having子句’中的未知列,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1318字,纯文字阅读大概需要2分钟。
内容图文
![mysql – ‘having子句’中的未知列](/upload/InfoBanner/zyjiaocheng/889/e693fca84d7f4598a7e460f947d12b23.jpg)
我需要在sakila数据库中找到电影的最长租期.
我试过这个:
SELECT DISTINCT
customer.first_name
FROM
rental,
customer
WHERE
rental.customer_id = customer.customer_id
GROUP BY
rental.rental_id
HAVING
(
rental.return_date - rental.rental_date
) =(
SELECT
MAX(countRental)
FROM
(
SELECT
(
rental.return_date - rental.rental_date
) AS countRental
FROM
rental,
customer
GROUP BY
rental.rental_id
) AS t1
)
但我收到错误:
1054 – ‘having子句’中的未知列’rental.return_date’
有谁知道为什么?我使用了一个应该是聚合数据的列..我缺少什么
解决方法:
如文档中所述
The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well.
您必须在select子句中指定return_date和rental_date.
有两种选择:
SELECT DISTINCT
customer.first_name,
rental.return_date,
rental.rental_date
FROM
rental,
customer
WHERE
rental.customer_id = customer.customer_id
GROUP BY
rental.rental_id
HAVING
(
rental.return_date - rental.rental_date
) =(
...
要么
SELECT DISTINCT
customer.first_name,
(rental.return_date - rental.rental_date) as rental_duration
FROM
rental,
customer
WHERE
rental.customer_id = customer.customer_id
GROUP BY
rental.rental_id
HAVING
rental_duration =(
...
两者都应该工作得很好.
内容总结
以上是互联网集市为您收集整理的mysql – ‘having子句’中的未知列全部内容,希望文章能够帮你解决mysql – ‘having子句’中的未知列所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。