复杂的sql查询:正确连接四个不同的表

发布于 2024-10-21 00:49:12 字数 759 浏览 1 评论 0原文

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

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

发布评论

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

评论(3

站稳脚跟 2024-10-28 00:49:12

它可能很简单,就像您在 where 子句中需要一些括号一样:

(usereducation.school like 'msu%' OR like 'minnesota state%')

否则,OR 的优先级将低于相邻的 AND。

编辑:2011-03-06

下面,我格式化了代码以使其更易于阅读,并且还移动了 userinformationusereducation 检查 exists() 子句。我这样做的原因是,如果用户有超过 1 个 userinformationuserreductionat 行与您的条件匹配,它将影响 count() 聚合。

select
    userpreference.preferencevalue as 'Music',
    COUNT(*) as 'SingleFemaleCount'

from users, userpreference
where users.Gender = 'female'
  and userpreference.Id = users.Id
  and userpreference.preferencetype = 'music'

  and exists
    (select *
    from userinformation
    where userinformation.Id = users.Id
      and userinformation.informationvalue = 'single')

  and exists
    (select *
    from usereducation
    where usereducation.Id = users.Id
      and (usereducation.school like 'msu%' OR like 'minnesota state%'))

GROUP BY userpreference.preferencevalue
ORDER BY COUNT(*) DESC limit 10

另一件需要检查的事情是 (usereducation.school like 'msu%' OR like 'minnesota state%') 确实找到了所有 MSU 记录。如果结果集不是太大,将运行 select different school from usereducation 来检查并确保您获取了所有记录。

最后,我更喜欢使用连接语法,如下所示:

select
    userpreference.preferencevalue as 'Music',
    COUNT(*) as 'SingleFemaleCount'

from users
inner join userpreference on userpreference.Id = users.Id
where users.Gender = 'female'
  and userpreference.preferencetype = 'music'

  and exists
    (select *
    from userinformation
    where userinformation.Id = users.Id
      and userinformation.informationvalue = 'single')

  and exists
    (select *
    from usereducation
    where usereducation.Id = users.Id
      and (usereducation.school like 'msu%' OR like 'minnesota state%'))

GROUP BY userpreference.preferencevalue
ORDER BY COUNT(*) DESC limit 10

我意识到我完全改变了你的查询,但是嘿,这是家庭作业,对吧:)

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 and usereducation checking into exists() clauses. The reason I am doing this is because if a user has more than 1 userinformation or usereductionat row matching your criteria, it would affect the count() aggregate.

select
    userpreference.preferencevalue as 'Music',
    COUNT(*) as 'SingleFemaleCount'

from users, userpreference
where users.Gender = 'female'
  and userpreference.Id = users.Id
  and userpreference.preferencetype = 'music'

  and exists
    (select *
    from userinformation
    where userinformation.Id = users.Id
      and userinformation.informationvalue = 'single')

  and exists
    (select *
    from usereducation
    where usereducation.Id = users.Id
      and (usereducation.school like 'msu%' OR like 'minnesota state%'))

GROUP BY userpreference.preferencevalue
ORDER BY COUNT(*) DESC limit 10

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 a select 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:

select
    userpreference.preferencevalue as 'Music',
    COUNT(*) as 'SingleFemaleCount'

from users
inner join userpreference on userpreference.Id = users.Id
where users.Gender = 'female'
  and userpreference.preferencetype = 'music'

  and exists
    (select *
    from userinformation
    where userinformation.Id = users.Id
      and userinformation.informationvalue = 'single')

  and exists
    (select *
    from usereducation
    where usereducation.Id = users.Id
      and (usereducation.school like 'msu%' OR like 'minnesota state%'))

GROUP BY userpreference.preferencevalue
ORDER BY COUNT(*) DESC limit 10

I realize I totally changed your query, but hey this is homework, right :)

以酷 2024-10-28 00:49:12

达纳是正确的。您还需要将 usereducation 添加到 from 子句:

from users, userpreference, userinformation, usereducation

并将其加入到 users 中。

Dana is correct. You also need to add usereducation to the from clause:

from users, userpreference, userinformation, usereducation

and join it to users.

羁客 2024-10-28 00:49:12

好的,一旦您在“FROM”中添加了 usereducation 并将其加入到 users 表中(您不需要加入其他表,但如果您愿意,可以加入)。尝试下面的方法。

在查询中,如果要查找与 WHERE 子句中的一个值或另一个值相等的列,则需要列出这些列两次:

AND (usereducation.school LIKE 'msu%' OR usereducation.school LIKE 'minnesota state%')

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:

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