简体   繁体   English

MySQL where 搜索问题

[英]MySQL where search issue

I cannot seem to search when I add WHERE statement below.当我在下面添加 WHERE 语句时,我似乎无法搜索。 But it works without it....但是没有它它也可以工作......

Is there something wrong?有什么不对?

Below is my code: Thank you.以下是我的代码:谢谢。

SELECT DISTINCT (
                    `name`
                    ), users.id, `email`, `state_id`,
                    MAX(`total_time_driven_at_this_trip`) AS trip
                    FROM `users`
                    LEFT JOIN trip_vics ON users.id = trip_vics.user_id
                    GROUP BY `user_id`
                    WHERE name LIKE '%dan%' 

You need to place your where right before group by您需要在group by之前放置where

The WHERE keyword can't be used with aggregate functions. WHERE 关键字不能与聚合函数一起使用。 Use HAVING instead http://www.w3schools.com/sql/sql_having.asp使用 HAVING 代替http://www.w3schools.com/sql/sql_having.asp

Put WHERE before the group by if you want to apply the filter before MAX is calculated.如果要在计算 MAX 之前应用过滤器,请将 WHERE 放在 group by 之前。

having goes with group by . having group by一起使用。

having name like '%dan%'

Or if you are actually trying to have a where clause independent of the having clause, it should go between the join and the group by statement.或者,如果您实际上试图让where子句独立于having子句,则它应该在joingroup by语句之间为 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM