多个 MYSQL 内连接

发布于 2024-12-06 08:24:05 字数 660 浏览 0 评论 0原文

您好,我正在尝试创建一个搜索功能,但无法使其正常工作。

到目前为止,这就是我所得到的:

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 技术交流群。

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

发布评论

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

评论(2

少女净妖师 2024-12-13 08:24:06

语句中有两个 group by 子句;那是无效的 SQL 。进行第一个分组依据,然后是逗号,然后是第二个分组依据。你的having子句应该在使用OR和AND将它们连接成一个逻辑语句

GROUP BY usersSkills.userID, usersLanguages.userID
HAVING COUNT(*) = 1 
AND usersLanguages.languageID IN(2) 

来回答你的语法错误问题之后..(不想在这里说出来)但是查询的结构表明你并不真正知道你想要什么不过,以查询的形式执行...我强烈建议您获得一本 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

GROUP BY usersSkills.userID, usersLanguages.userID
HAVING COUNT(*) = 1 
AND usersLanguages.languageID IN(2) 

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..

雨后彩虹 2024-12-13 08:24:06

Tr this:

SELECT DISTINCT users.ID, users.name, COUNT(*)
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 = 2 
      AND usersLanguages.languageID = 2 
GROUP BY usersSkills.userID,
         usersLanguages.userID 
HAVING COUNT(*) = 1 

删除了多个错误的 GROUP BY 并合并了查询。
还将 IN(x) 更改为 = x

Tr this:

SELECT DISTINCT users.ID, users.name, COUNT(*)
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 = 2 
      AND usersLanguages.languageID = 2 
GROUP BY usersSkills.userID,
         usersLanguages.userID 
HAVING COUNT(*) = 1 

Removed the multiple erroneous GROUP BY's and merged the query.
Also changed the IN(x) to = x

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