mysql php 查询 HAVING 子句

发布于 2024-08-23 01:11:30 字数 505 浏览 9 评论 0原文

我试图让这个查询工作,但我收到这个错误: “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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

将军与妓 2024-08-30 01:11:30

having 子句与 where 子句的含义不同:运行简单查询时,您应该使用 where —— 这是您在第二个查询中所做的事情是有效的。

当必须将条件应用于 group by 子句的结果时,使用 having

这意味着,在这里,您的查询应该以这种方式构建:

$query = "SELECT zips.*
    FROM zips
    where zips.city LIKE '%$city%' 
        AND zips.stateabbr LIKE '%$state%' 
    LIMIT 1";

这样,如果您仍然遇到有关不存在或未找到的列的错误(至少对于city和/或stateabbr ,这是因为您的表中不存在该列。

在这种情况下,我们无能为力:您必须检查表的结构,以确定它包含哪些列。

您可以使用基于 Web 的工具(例如 phpMyAdmin)或使用 SQL 指令(例如:

desc zips;

) 检查该结构
作为参考,引用 MySQL 的 select 手册页

SQL 标准要求 HAVING
必须仅引用中的列
中使用的 GROUP BY 子句或列
聚合函数。
...

请勿对以下项目使用 HAVING
应位于 WHERE 子句中。

例如,不要写
以下:

从 tbl_name 中选择 col_name HAVING col_name > 0;

改为这样写:

SELECT col_name FROM tbl_name WHERE col_name > 0;

...

HAVING子句可以参考
聚合函数,其中 WHERE
条款不能

The having clause doesn't mean the same thing as the where clause : when running a simple query, you should use where -- 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 a group by clause.

Which means that, here, your query should be build this way :

$query = "SELECT zips.*
    FROM zips
    where zips.city LIKE '%$city%' 
        AND zips.stateabbr LIKE '%$state%' 
    LIMIT 1";

With that, if you still have an error about a non-existing or not-found column (at least for city and/or stateabbr), 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 :

desc zips;

For reference, quoting MySQL's manual page for select :

The SQL standard requires that HAVING
must reference only columns in the
GROUP BY clause or columns used in
aggregate functions.
...

Do not use HAVING for items that
should be in the WHERE clause.

For example, do not write the
following:

SELECT col_name FROM tbl_name HAVING col_name > 0;

Write this instead:

SELECT col_name FROM tbl_name WHERE col_name > 0;

...

The HAVING clause can refer to
aggregate functions, which the WHERE
clause cannot

葮薆情 2024-08-30 01:11:30

尝试使用 WHERE 而不是 HAVING

Try using WHERE instead of HAVING.

苏大泽ㄣ 2024-08-30 01:11:30

正确的方法是使用 WHERE 子句。

$query = "SELECT
zips.*
FROM
zips
WHERE
zips.city LIKE '%$city%' 
AND
zips.stateabbr LIKE '%$state%' 
LIMIT 1";

HAVING 是在分组时使用的,请参阅此处 获取解释

The proper way to do it is by using a WHERE clause.

$query = "SELECT
zips.*
FROM
zips
WHERE
zips.city LIKE '%$city%' 
AND
zips.stateabbr LIKE '%$state%' 
LIMIT 1";

HAVING is to be used when you are GROUPing, see here for an explanation

小糖芽 2024-08-30 01:11:30

天啊,对不起,大家,我解决了这个问题,显然,当我命名表中的列时,我在城市之前加了一个空格。所以我重命名了该专栏,它可以正常工作,无论如何,谢谢大家!但是使用 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文