relational-division

Complicated SQL Query--finding items matching multiple different foreign keys

只愿长相守 提交于 2020-01-10 03:20:07
问题 So imagine that you have a table of Products (ID int, Name nvarchar(200)) , and two other tables, ProductsCategories (ProductID int, CategoryID int) and InvoiceProducts (InvoiceID int, ProductID int) . I need to write a query to produce a set of products that match a given set of invoice ids and category ids such that the list of products match all the specified categories and all the specified invoices, without falling back to dynamic SQL. Imagine I need to find a list of products that are

MySQL Select ID's which occur on different rows with multiple specific values for a column

跟風遠走 提交于 2020-01-10 01:48:08
问题 I'm trying to select items from an associative table that have satisfy two or more values of the same field, sounds confusing, let me explain. +-----------------------+ | item_id | category_id | +-----------------------+ | 1 | 200 | | 1 | 201 | | 1 | 202 | | 2 | 201 | | 2 | 202 | | 3 | 202 | | 3 | 203 | | 4 | 201 | | 4 | 207 | +-----------------------+ In the table, I want to be able to select only items which are in the categories that I pass. So for example, if I pass category IDs of 201

MySQL Select ID's which occur on different rows with multiple specific values for a column

让人想犯罪 __ 提交于 2020-01-10 01:46:15
问题 I'm trying to select items from an associative table that have satisfy two or more values of the same field, sounds confusing, let me explain. +-----------------------+ | item_id | category_id | +-----------------------+ | 1 | 200 | | 1 | 201 | | 1 | 202 | | 2 | 201 | | 2 | 202 | | 3 | 202 | | 3 | 203 | | 4 | 201 | | 4 | 207 | +-----------------------+ In the table, I want to be able to select only items which are in the categories that I pass. So for example, if I pass category IDs of 201

How to count MySQL results in a has-many-through relation

て烟熏妆下的殇ゞ 提交于 2020-01-05 03:47:06
问题 There is pretty good article how to filter results in a has-many relation: How to filter SQL results in a has-many-through relation I'm just seeking a solution for COUNT result, not show them all. student { id name } club { id name } student_club { student_id club_id } How many students are in both CLUB1 && CLUB2? EDIT: It would be great to use "Martin 2" method from a link below: SELECT s.stud_id, s.name FROM student s JOIN student_club sc USING (stud_id) WHERE sc.club_id IN (30, 50) GROUP

SQL query to find a row with a specific number of associations

江枫思渺然 提交于 2020-01-02 23:13:15
问题 Using Postgres I have a schema that has conversations and conversationUsers . Each conversation has many conversationUsers . I want to be able to find the conversation that has the exactly specified number of conversationUsers . In other words, provided an array of userIds (say, [1, 4, 6] ) I want to be able to find the conversation that contains only those users, and no more. So far I've tried this: SELECT c."conversationId" FROM "conversationUsers" c WHERE c."userId" IN (1, 4) GROUP BY c.

How to find all pizzerias that serve every pizza eaten by people over 30?

限于喜欢 提交于 2019-12-31 14:36:35
问题 I'm following the Stanford Database course and there's a question where we have Find all pizzerias that serve every pizza eaten by people over 30 using Relational Algebra only. The problem consist of a small database with four relations: Person(name, age, gender) // name is a key Frequents(name, pizzeria) // [name,pizzeria] is a key Eats(name, pizza) // [name,pizza] is a key Serves(pizzeria, pizza, price) // [pizzeria,pizza] is a key I know how to find which pizza's people over 30 eat and

Finding combinations of specific values

只谈情不闲聊 提交于 2019-12-31 05:47:11
问题 I don't know how to write the query for below. My table is col1 col2 5 1 5 5 5 6 5 7 4 5 4 8 4 9 4 3 3 3 3 5 I need to select distinct col1 id where both parameters exists in col2. eg. if i send 6,7 it should send me 5 回答1: Try: SELECT col1 FROM mytable WHERE col2 IN (6, 7) GROUP BY col1 HAVING COUNT(DISTINCT col2) = 2 回答2: This is probably among the fastest solutions: SELECT col1 -- already DISTINCT? FROM tbl t1 JOIN tbl t2 USING (col1) WHERE t1.col2 = 6 AND t2.col2 = 7; Assuming a PRIMARY

SQL - Return rows matching all values from a joined table

人盡茶涼 提交于 2019-12-25 02:16:26
问题 Title might not be massively helpful on this one so I'll try and explain it as best I can. Assume two tables. Product (ProductID, Name) ProductCategory (ID, ProductID, CategoryID) and a third temporary table containing a list of CategoryIDs to match. MatchTable(CategoryID) I want to use the contents of MatchTable to return the products that have all of the associated categories. IE. Product 1: Associated with categories 1 and 2. Product 2: Associated with categories 2 and 3. Product 3:

Get the count of rows count after GROUP BY

眉间皱痕 提交于 2019-12-24 13:34:09
问题 Here is the code I use in a Ruby on Rails project to find residences which have amenities with the ids 48, 49 and 50. They are connected with a has_many through connection. id_list = [48, 49, 50] Residence.joins(:listed_amenities). where(listed_amenities: {amenity_id: id_list}). group('residences.id'). having("count(listed_amenities.*) = ?", id_list.size) The resulting SQL: SELECT "residences".* FROM "residences" INNER JOIN "listed_amenities" ON "listed_amenities"."residence_id" = "residences

Select courses that are completely satisfied by a given list of prerequisites

吃可爱长大的小学妹 提交于 2019-12-22 08:52:00
问题 I'm trying to write an SQL query that will return a list of courses that a person is eligible for given a list of their completed subjects (to be used as prerequisites). I have my database laid out as such. Prerequisite: +---------------+---------------+ | Id | Name | (Junction table) |---------------|---------------| CoursePrerequisites: | 1 | Maths | +---------------+---------------+ | 2 | English | | Course_FK | Prerequisite_FK | 3 | Art | |---------------|---------------| | 4 | Physics |