sql-match-all

How do I avoid dynamic SQL when using an undetermined number of parameters?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 15:13:54
问题 I have a StackOverflow-like tagging system for a database I'm working on. And I'm writing a stored procedure that looks for results based on an undetermined number of tags in a WHERE clause. There could be anywhere between 0 and 10 tags to filter results. So for example the user could be searching for items tagged with 'apple', 'orange', and 'banana' and each result must include all 3 tags. My query is made even more complicated because I'm also dealing with a cross reference table for the

How to filter SQL results in a has-many-through relation

守給你的承諾、 提交于 2019-11-25 23:56:36
问题 Assuming I have the tables student , club , and student_club : student { id name } club { id name } student_club { student_id club_id } I want to know how to find all students in both the soccer (30) and baseball (50) club. While this query doesn\'t work, it\'s the closest thing I have so far: SELECT student.* FROM student INNER JOIN student_club sc ON student.id = sc.student_id LEFT JOIN club c ON c.id = sc.club_id WHERE c.id = 30 AND c.id = 50 回答1: I was curious. And as we all know,

Select values that meet different conditions on different rows?

不羁的心 提交于 2019-11-25 23:44:23
问题 This is a very basic query I can\'t figure out.... Let\'s say I have a two column table like this: userid | roleid --------|-------- 1 | 1 1 | 2 1 | 3 2 | 1 I want to get all distinct userids that have roleids 1, 2 AND 3. Using the above example, the only result I want returned is userid 1. How do I do this? 回答1: SELECT userid FROM UserRole WHERE roleid IN (1, 2, 3) GROUP BY userid HAVING COUNT(DISTINCT roleid) = 3; To anyone reading this: my answer is simple and straightforward, and got the