多个 MYSQL 内连接
您好,我正在尝试创建一个搜索功能,但无法使其正常工作。
到目前为止,这就是我所得到的:
SELECT DISTINCT users.ID, users.name
FROM users
INNER JOIN usersSkills
ON users.ID = usersSkills.userID
INNER JOIN usersLanguages
ON users.ID = usersLanguages.userID
WHERE activated = "1"
AND type = "GRADUATE"
AND usersSkills.skillID IN(2)
GROUP BY usersSkills.userID HAVING COUNT(*) = 1
AND usersLanguages.languageID IN(2)
GROUP BY usersLanguages.userID HAVING COUNT(*) = 1
但我不断收到此错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 14 行的“GROUP BY usersLanguages.userID HAVING COUNT(*) = 1”附近使用的正确语法
如果我删除内部联接和分组依据之一,它是否有效,所以不确定我可以使用两个分组依据吗?
Hi I am trying to create a search function but having trouble on getting it to work.
This is what I have so far:
SELECT DISTINCT users.ID, users.name
FROM users
INNER JOIN usersSkills
ON users.ID = usersSkills.userID
INNER JOIN usersLanguages
ON users.ID = usersLanguages.userID
WHERE activated = "1"
AND type = "GRADUATE"
AND usersSkills.skillID IN(2)
GROUP BY usersSkills.userID HAVING COUNT(*) = 1
AND usersLanguages.languageID IN(2)
GROUP BY usersLanguages.userID HAVING COUNT(*) = 1
But I keep getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY usersLanguages.userID HAVING COUNT(*) = 1' at line 14
If i remove one of the inner joins and group bys it works so not sure what it is can i use two group bys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
语句中有两个 group by 子句;那是无效的 SQL 。进行第一个分组依据,然后是逗号,然后是第二个分组依据。你的having子句应该在使用OR和AND将它们连接成一个逻辑语句
来回答你的语法错误问题之后..(不想在这里说出来)但是查询的结构表明你并不真正知道你想要什么不过,以查询的形式执行...我强烈建议您获得一本 SQL 书籍或阅读 SQL 教程,以便您了解您尝试编写的语句实际上是做什么的。
You have two group by clauses in the statement; that is invalid SQL . Do your first group by, then a comma, then your second group by. Your Having clauses should come after using ORs and ANDs to join them into a logical statement
that answers your syntax error question.. (not trying to speak down here) but the structure of the query shows you don't really know what you want to do in the form of the query though... I would strongly suggest you get a SQL book or go through a sql tutorial so you understand what the statements you are trying to write actually do..
Tr this:
删除了多个错误的
GROUP BY
并合并了查询。还将
IN(x)
更改为= x
Tr this:
Removed the multiple erroneous
GROUP BY
's and merged the query.Also changed the
IN(x)
to= x