relational-division

Find a value which contains in ALL rows of a table

[亡魂溺海] 提交于 2019-12-13 20:17:57
问题 I need to select all values which are contained in ALL rows of a table. I have table “Ingredient” and ProductIngredient(there I have a recipe of a product). Ingredient | ingredient_id | name | price | | 1 | Bla | 100 | 2 | foo | 50 ProductIngredient. | Product_id | ingredient_id | 1 | 1 | 1 | 2 | 2 | 1 The output should be | 1 | Bla | as it is in all rows of ProductIngredient. SELECT DISTINCT Ingredient_Id FROM Ingredients I WHERE Ingredient_Id = ALL (SELECT Ingredient_id FROM

How to do intersection on a composition table

不羁岁月 提交于 2019-12-13 03:48:50
问题 I have a simple SQL relational model with a many to many relation. Here is the composition table ___________________________ | object1_id | object2_id | |---------------------------| I would like to know all the object1 that are common to a set of object2 . My basic feeling is to do a request like this SELECT c.object1_id FROM composition c WHERE c.object2_id = <given_id_1> INTERSECT SELECT c.object1_id FROM composition c WHERE c.object2_id = <given_id_2> And if I have N object2 in the set, I

SQL to return a merged set of results

本秂侑毒 提交于 2019-12-12 18:28:50
问题 I have the following SQL: SELECT `table1`.`value`, `table2`.* FROM `table2` INNER JOIN `table1` ON `table2`.`product_id` = `table1`.`entity_id` WHERE `table2`.`created_at` > '2012-04-23' and (`table1`.`value` = 264 OR `table1`.`value` = 260) order by order_id Which returns a result set like this (This is only a part of the returned results): value order_id ... 260 1234 260 1235 260 1236 264 1236 260 1237 260 1238 260 1239 264 1239 264 1240 260 1241 What I want is a query that will take these

Converting From NOT EXISTS to NOT IN

帅比萌擦擦* 提交于 2019-12-12 15:09:12
问题 I have three tables: sailor (sname, rating); boat (bname, color, rating); reservation (sname, bname, weekday, start, finish); In order to get a list of sailors who have reserved every red boat, I have: select s.sname from sailor s where not exists( select * from boat b where b.color = 'red' and not exists ( select * from reservation r where r.bname = b.bname and r.sname = s.sname)); I now need to rewrite this query with NOT IN instead of NOT EXISTS. Here's what I have so far: select s.sname

SQL query for finding pairs that share the exact same set of values

喜你入骨 提交于 2019-12-12 13:22:33
问题 I'm having trouble generating a query for this problem. I have this small table Tasks(employee_name, task) Sample Data: Tasks ------------------ Joe | taskA Joe | taskB Ted | taskA Jim | taskB Ray | taskA Ray | taskB John| taskA Tim | taskC I need to find all pairs of employees that have the exact same tasks. For example using the data above the result set should be: --------------------- employee1 | employee2 --------------------- Joe | Ray Ted | John I'm using mySQL for the database. Thanks

How to efficiently set subtract a join table in PostgreSQL?

白昼怎懂夜的黑 提交于 2019-12-12 09:29:28
问题 I have the following tables: work_units - self explanatory workers - self explanatory skills - every work unit requires a number of skills if you want to work on it. Every worker is proficient in a number of skills. work_units_skills - join table workers_skills - join table A worker can request the next appropriate free highest priority (whatever that means) unit of work to be assigned to her. Currently I have: SELECT work_units.* FROM work_units -- some joins WHERE NOT EXISTS ( SELECT skill

Is there a way to remove the nested query in this type of SQL SELECT?

笑着哭i 提交于 2019-12-12 09:17:40
问题 Given this table structure and example data (t3 should not be used in the query, it is only here to show the relation between t1 and t2): t1 t2 t3 -------------- ----------------- -------------------------------- | id | value | | t1key | t3key | | id | value | | 1 | 2008 | | 3 | 1 | | 1 | "New intel cpu in 2010" | | 2 | 2009 | | 4 | 1 | | 2 | "New amd cpu in 2008" | | 3 | 2010 | | 6 | 1 | | | ... | | 4 | intel | | 1 | 2 | -------------------------------- | 5 | amd | | 5 | 2 | | 6 | cpu | | 6

Postgres exclusive tag search

流过昼夜 提交于 2019-12-12 06:05:08
问题 I'm trying to return all rows which are associated with a user who is associated with ALL of the queried 'tags'. My table structure and desired output is below: admin.tags: user_id | tag | detail | date 2 | apple | blah... | 2015/07/14 3 | apple | blah. | 2015/07/17 1 | grape | blah.. | 2015/07/23 2 | pear | blahblah | 2015/07/23 2 | apple | blah, blah | 2015/07/25 2 | grape | blahhhhh | 2015/07/28 system.users: id | email 1 | joe@test.com 2 | jane@test.com 3 | bob@test.com queried tags:

ALL operator in WHERE clause in Rails

社会主义新天地 提交于 2019-12-11 02:07:29
问题 The association is as shown below. InstructorStudent has_many :fees Fee belongs_to :instructor_student I want to get the instructor student who has monthly detail in all given array. If monthly details is not present in any one of them then it should not return any record. due_month = ["2017-01-01","2017-02-01",,"2017-03-01"] Following is the query which I tried, I want to get InstructorStudent which belongs to all given three due_month, if any month have no data then it should return nil :

Why are double nested NOT EXISTS statements unavoidable in SQL

天涯浪子 提交于 2019-12-11 01:06:27
问题 This is more out of curiosity/scientific interest, than based on a real problem and I once asked my databases lecturer about this, but he could not answer/understand my question. So I decided to come over here. A programming language is supposed to be a tool and tools are made to make working easier, right? So why is it that you can find all entries in a one table by simply doing SELECT * FROM foo WHERE bar=42; . But as soon as there are multiple tables involved, there is no easy/intuitive