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
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
userinformationandusereducationchecking intoexists()clauses. The reason I am doing this is because if a user has more than 1userinformationorusereductionatrow 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 usereducationto 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 🙂