relational-division

How to find if a list/set is contained within another list

喜欢而已 提交于 2021-01-27 12:44:43
问题 I have a list of product IDs and I want to find out which orders contain all those products. Orders table is structured like this: order_id | product_id ---------------------- 1 | 222 1 | 555 2 | 333 Obviously I can do it with some looping in PHP but I was wondering if there is an elegant way to do it purely in mysql. My ideal fantasy query would be something like: SELECT order_id FROM orders WHERE (222,555) IN GROUP_CONCAT(product_id) GROUP BY order_id Is there any hope or should I go read

What is a SQL statement to select an item that has several attributes in an item/attribute list?

烂漫一生 提交于 2020-02-01 04:40:32
问题 Say I have a table that has items and attributes listed like, frog green cat furry frog nice cat 4 legs frog 4 legs From the items column I want to select unique objects that have both the green and 4 legs attribute. I would expect to get back just the frog object in this case. What is the most efficient query to do this? 回答1: select item.name from item where item.attribute in ('4 legs', 'green') group by item.name having count(distinct item.attribute) = 2 回答2: The most efficient way to do

What is a SQL statement to select an item that has several attributes in an item/attribute list?

≡放荡痞女 提交于 2020-02-01 04:40:25
问题 Say I have a table that has items and attributes listed like, frog green cat furry frog nice cat 4 legs frog 4 legs From the items column I want to select unique objects that have both the green and 4 legs attribute. I would expect to get back just the frog object in this case. What is the most efficient query to do this? 回答1: select item.name from item where item.attribute in ('4 legs', 'green') group by item.name having count(distinct item.attribute) = 2 回答2: The most efficient way to do

Database Relational Algebra: How to find actors who have played in ALL movies produced by “Universal Studios”?

梦想与她 提交于 2020-01-22 03:49:05
问题 Given the following relational schemas, where the primary keys are in bold: movie( movieName , whenMade); actor( actorName , age); studio( studioName , location, movieName); actsIn( actorName , movieName ); How do you find the list of actors who have played in EVERY movie produced by "Universal Studios"? My attempt: π actorName ∩ (σ studioName=“Universal Studios” studio) |><| actsIn, where |><| is the natural join Are you supposed to use cartesian product and/or division? :\ 回答1: Here are the

Rails has_and_belongs_to_many find unique objects in common

依然范特西╮ 提交于 2020-01-16 03:44:08
问题 I have two models, Conversation and Phones, both of which has_and_belongs_to_many each other. Phones can have a lot of conversations, and conversations can have a lot of phones (two or more). class Conversation < ActiveRecord::Base has_and_belongs_to_many :phones end class Phone < ActiveRecord::Base has_and_belongs_to_many :conversations end Of course, there's a conversations_phones join table as well. If I have two or more phone objects, how do I find a list of all the conversations they

Sql: choose all baskets containing a set of particular items

自古美人都是妖i 提交于 2020-01-16 01:36:09
问题 Eddy has baskets with items. Each item can belong to arbitrary number of baskets or can belong to none of them. Sql schema to represent it is as following: tbl_basket - basketId tbl_item - itemId tbl_basket_item - pkId - basketId - itemId Question: how to select all baskets containing a particular set of items? UPDATE. Baskets with all the items are needed. Otherwise it would have been easy task to solve. UPDATE B. Have implemented following solution, including SQL generation in PHP: SELECT

Retrieve multiple rows with query using AND and OR

不问归期 提交于 2020-01-16 00:19:26
问题 I want to retrieve multiple rows using same id's. Therefore having this table "component_property", I would like to have as results 2 records, id's: 8 and 9 according to my SQL query (check below), but of course and retrieving nothing since I'm checking if cp.property_id = 9102 and later and checking if cp.property_id = 8801 which at the same time is impossible. ID;type_id;name;desc;property_id,value -------------------------------------- 8;3832;"amplifier1";"";8801;"3" 8;3832;"amplifier1";""

Matching algorithm in SQL

北城余情 提交于 2020-01-10 04:38:00
问题 I have the following table in my database. # select * FROM matches; name | prop | rank ------+------+------- carl | 1 | 4 carl | 2 | 3 carl | 3 | 9 alex | 1 | 8 alex | 2 | 5 alex | 3 | 6 alex | 3 | 8 alex | 2 | 11 anna | 3 | 8 anna | 3 | 13 anna | 2 | 14 (11 rows) Each person is ranked at work by different properties/criterias called 'prop' and the performance is called 'rank'. The table contains multiple values of (name, prop) as the example shows. I want to get the best candidate following

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

偶尔善良 提交于 2020-01-10 03:20:10
问题 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

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

此生再无相见时 提交于 2020-01-10 03:20:08
问题 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