复杂的sql查询:正确连接四个不同的表
我有 4 个表:users、userpreference、userinfo、useredu
最后三个表使用“id”作为引用表“users”的外键:
要制定的查询:
我需要找到“所有去密歇根州立大学的单身女性中的顶级音乐”
指出密歇根州立大学也可能是“明尼苏达州立大学”
到目前为止我有这个疑问,但它没有产生正确的结果?
select userpreference.preferencevalue as 'Music', COUNT(*) as 'SingleFemaleCount'from users, userpreference, userinformation
where users.Id = userinformation.Id
and users.Id = userpreference.Id
and userpreference.Id = userinformation.Id
and users.Gender = 'female'
and userinformation.informationvalue = 'single'
and usereducation.school like 'msu%' OR like 'minnesota state%'
and userpreference.preferencetype = 'music' GROUP BY preferencevalue ORDER BY COUNT(distinct users.Id) DESC limit 10
I have 4 tables: users, userpreference, userinfo, useredu
the last three tables use "id" as a foreign key referencing the table 'users' :
Query to be formulated:
i need to find the "top music among all single females who go to MSU"
noting that MSU could also be 'Minnesota State University"
i have this query so far but it is not producing the correct results?
select userpreference.preferencevalue as 'Music', COUNT(*) as 'SingleFemaleCount'from users, userpreference, userinformation
where users.Id = userinformation.Id
and users.Id = userpreference.Id
and userpreference.Id = userinformation.Id
and users.Gender = 'female'
and userinformation.informationvalue = 'single'
and usereducation.school like 'msu%' OR like 'minnesota state%'
and userpreference.preferencetype = 'music' GROUP BY preferencevalue ORDER BY COUNT(distinct users.Id) DESC limit 10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它可能很简单,就像您在 where 子句中需要一些括号一样:
(usereducation.school like 'msu%' OR like 'minnesota state%')
否则,OR 的优先级将低于相邻的 AND。
编辑:2011-03-06
下面,我格式化了代码以使其更易于阅读,并且还移动了
userinformation
和usereducation
检查exists()
子句。我这样做的原因是,如果用户有超过 1 个userinformation
或userreductionat
行与您的条件匹配,它将影响count()
聚合。另一件需要检查的事情是
(usereducation.school like 'msu%' OR like 'minnesota state%')
确实找到了所有 MSU 记录。如果结果集不是太大,将运行select different school from usereducation
来检查并确保您获取了所有记录。最后,我更喜欢使用连接语法,如下所示:
我意识到我完全改变了你的查询,但是嘿,这是家庭作业,对吧:)
It might be as simple as you need some parenthesis in your where clause:
(usereducation.school like 'msu%' OR like 'minnesota state%')
Otherwise, the OR will be of lower precedence than the adjacent ANDs.
EDIT: 2011-03-06
Below, I have formatted the code to make it a little easier to read, and also moved the
userinformation
andusereducation
checking intoexists()
clauses. The reason I am doing this is because if a user has more than 1userinformation
orusereductionat
row matching your criteria, it would affect thecount()
aggregate.Another thing to check is that
(usereducation.school like 'msu%' OR like 'minnesota state%')
indeed finds all MSU records. If the result set is not too huge, would run aselect distinct school from usereducation
to check and see that sure you are getting all records.Lastly, I sort of prefer to use the join syntax as follows:
I realize I totally changed your query, but hey this is homework, right :)
达纳是正确的。您还需要将 usereducation 添加到 from 子句:
并将其加入到 users 中。
Dana is correct. You also need to add usereducation to the from clause:
and join it to users.
好的,一旦您在“FROM”中添加了 usereducation 并将其加入到 users 表中(您不需要加入其他表,但如果您愿意,可以加入)。尝试下面的方法。
在查询中,如果要查找与 WHERE 子句中的一个值或另一个值相等的列,则需要列出这些列两次:
Ok, once you have usereducation added in the "FROM" and it's joined to the users table (you shouldn't need to join the other tables, but if you want, have at it). Try the below.
In a query, if you want to look for a column to equal one value or another in the WHERE clause, you'll need to list the columns twice: