mysql php 查询 HAVING 子句
我试图让这个查询工作,但我收到这个错误: “having 子句”中的未知列“zips.city”
`$query = "SELECT zips.* FROM zips HAVING zips.city LIKE '%$city%' AND zips.stateabbr LIKE '%$state%' LIMIT 1"; $result = mysql_query($query) or die (mysql_error());`
我的 zips 表有一个城市列,所以我不确定问题是什么,我知道我正在访问数据库,因为我可以毫无错误地运行此查询:
$zip1query = "SELECT zips.* FROM zips WHERE zips.zip = '$zip' ";
任何建议都会很多赞赏!谢谢!
Im trying to get this query to work but i get this error:
Unknown column 'zips.city' in 'having clause'
`$query = "SELECT zips.* FROM zips HAVING zips.city LIKE '%$city%' AND zips.stateabbr LIKE '%$state%' LIMIT 1"; $result = mysql_query($query) or die (mysql_error());`
my zips table has a city column, so im not sure what the problem is, i know im accessing the database because i can run this query with no errors:
$zip1query = "SELECT zips.* FROM zips WHERE zips.zip = '$zip' ";
any advice would be much appreciated! thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
having
子句与where
子句的含义不同:运行简单查询时,您应该使用where
—— 这是您在第二个查询中所做的事情是有效的。当必须将条件应用于
group by
子句的结果时,使用having
。这意味着,在这里,您的查询应该以这种方式构建:
这样,如果您仍然遇到有关不存在或未找到的列的错误(至少对于
city
和/或stateabbr
) ,这是因为您的表中不存在该列。在这种情况下,我们无能为力:您必须检查表的结构,以确定它包含哪些列。
您可以使用基于 Web 的工具(例如 phpMyAdmin)或使用 SQL 指令(例如:
) 检查该结构
作为参考,引用 MySQL 的
select
手册页:The
having
clause doesn't mean the same thing as thewhere
clause : when running a simple query, you should usewhere
-- which is what you did in your second query, that works.having
is used when the condition has to be applied on the result of agroup by
clause.Which means that, here, your query should be build this way :
With that, if you still have an error about a non-existing or not-found column (at least for
city
and/orstateabbr
), it'll be because that column doesn't exist in your table.In this case, there is not much we can do : you'll have to check the structure of your table, to determine which columns it contains.
You can check that structure using a web-based tool like phpMyAdmin, or using an SQL instruction such as :
For reference, quoting MySQL's manual page for
select
:尝试使用
WHERE
而不是HAVING
。Try using
WHERE
instead ofHAVING
.正确的方法是使用 WHERE 子句。
HAVING 是在分组时使用的,请参阅此处 获取解释
The proper way to do it is by using a WHERE clause.
HAVING is to be used when you are GROUPing, see here for an explanation
天啊,对不起,大家,我解决了这个问题,显然,当我命名表中的列时,我在城市之前加了一个空格。所以我重命名了该专栏,它可以正常工作,无论如何,谢谢大家!但是使用 where 函数而不是 had 一定会加快速度,谢谢大家!
o jeez sorry guys i figured out the problem, apparently i put a space before city when i named the columns in my table. so i renamed the column and it works thanks anyway chaps! but using the where function instead of having must speed things up alot, thanks guys!