relational-division

SQL how to search a many to many relationship

こ雲淡風輕ζ 提交于 2019-11-28 04:29:34
问题 I have a database with two main tables notes and labels . They have a many-to-many relationship (similar to how stackoverflow.com has questions with labels). What I am wondering is how can I search for a note using multiple labels using SQL? For example if I have a note "test" with three labels "one", "two", and "three" and I have a second note "test2" with labels "one" and "two" what is the SQL query that will find all the notes that are associated with labels "one" and "two"? 回答1: To obtain

Using same column multiple times in WHERE clause

廉价感情. 提交于 2019-11-27 14:56:04
问题 I have a following table structure. USERS PROPERTY_VALUE PROPERTY_NAME USER_PROPERTY_MAP I am trying to retrieve user/s from the users table who have matching properties in property_value table. A single user can have multiple properties. The example data here has 2 properties for user '1', but there can be more than 2. I want to use all those user properties in the WHERE clause. This query works if user has a single property but it fails for more than 1 properties: SELECT * FROM users u

PostgreSQL where all in array

旧巷老猫 提交于 2019-11-27 08:36:40
What is the easiest and fastest way to achieve a clause where all elements in an array must be matched - not only one when using IN ? After all it should behave like mongodb's $all . Thinking about group conversations where conversation_users is a join table between conversation_id and user_id I have something like this in mind: WHERE (conversations_users.user_id ALL IN (1,2)) UPDATE 16.07.12 Adding more info about schema and case: The join-table is rather simple: Table "public.conversations_users" Column | Type | Modifiers | Storage | Description -----------------+---------+-----------+------

SQL: Get Products from a category but also must be in another set of categories

北慕城南 提交于 2019-11-27 07:51:01
问题 I am currently stuck in a situation. The scenario is this. I have products who may be associated with multiple categories. The data structure is shown below: Products Table: product_id name 1 Lemon 2 Kiwis 3 Cheese Product to Categories Table product_id category_id 1 1 1 2 1 3 2 1 2 3 3 2 3 4 Category Table (not required in query however adding it here to help visualize what is happening) category_id name 1 Fruit 2 Yellow 3 Round 4 Dairy What I'm struggling with here is that originally I want

SQL where joined set must contain all values but may contain more

℡╲_俬逩灬. 提交于 2019-11-27 05:33:00
I have three tables offers , sports and the join table offers_sports . class Offer < ActiveRecord::Base has_and_belongs_to_many :sports end class Sport < ActiveRecord::Base has_and_belongs_to_many :offers end I want to select offers that include a given array of sport names. They must contain all of the sports but may have more. Lets say I have these three offers: light: - "Yoga" - "Bodyboarding" medium: - "Yoga" - "Bodyboarding" - "Surfing" all: - "Yoga" - "Bodyboarding" - "Surfing" - "Parasailing" - "Skydiving" Given the array ["Bodyboarding", "Surfing"] I would want to get medium and all

jsonb query with nested objects in an array

百般思念 提交于 2019-11-27 02:20:44
问题 I'm using PostgreSQL 9.4 with a table teams containing a jsonb column named json . I am looking for a query where I can get all teams which have the Players 3 , 4 and 7 in their array of players. The table contains two rows with the following json data: First row: { "id": 1, "name": "foobar", "members": { "coach": { "id": 1, "name": "A dude" }, "players": [ { "id": 2, "name": "B dude" }, { "id": 3, "name": "C dude" }, { "id": 4, "name": "D dude" }, { "id": 6, "name": "F dude" }, { "id": 7,

PostgreSQL where all in array

会有一股神秘感。 提交于 2019-11-26 12:44:37
问题 What is the easiest and fastest way to achieve a clause where all elements in an array must be matched - not only one when using IN ? After all it should behave like mongodb\'s $all. Thinking about group conversations where conversation_users is a join table between conversation_id and user_id I have something like this in mind: WHERE (conversations_users.user_id ALL IN (1,2)) UPDATE 16.07.12 Adding more info about schema and case: The join-table is rather simple: Table \"public.conversations

Select group of rows that match all items in a list

▼魔方 西西 提交于 2019-11-26 12:22:20
Assume I have two tables: cars – list of cars carname | modelnumber | ... passedtest – contains every test that a car passed: id | carname | testtype | date | ... 1 | carA | A | 2000 | 2 | carB | C | 2000 | 3 | carC | D | 2001 | 4 | carA | C | 2002 | Now, how can I select a car from the passedtest table that passed all tests (A, B, C, D)? I tried the IN statement but it also matches cars that pass even one test. I am looking for a statement to match all values in a list across all rows. MarcinJuraszek How about this? SELECT carname FROM PassedTest GROUP BY carname HAVING COUNT(DISTINCT

SQL where joined set must contain all values but may contain more

本秂侑毒 提交于 2019-11-26 11:37:26
问题 I have three tables offers , sports and the join table offers_sports . class Offer < ActiveRecord::Base has_and_belongs_to_many :sports end class Sport < ActiveRecord::Base has_and_belongs_to_many :offers end I want to select offers that include a given array of sport names. They must contain all of the sports but may have more. Lets say I have these three offers: light: - \"Yoga\" - \"Bodyboarding\" medium: - \"Yoga\" - \"Bodyboarding\" - \"Surfing\" all: - \"Yoga\" - \"Bodyboarding\" - \

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,