relational-division

check if a column contains ALL the values of another column - Mysql

邮差的信 提交于 2019-12-08 17:43:35
问题 Let's suppose I have a table T1 with people IDs and other stuff IDs, as the following Table: T1 personID | stuffID 1 | 1 1 | 2 1 | 3 1 | 4 2 | 1 2 | 4 3 | 1 3 | 2 And another table T2 with just one column of stuffIDs Table: T2 stuffID 1 2 3 The result that I would get, by a SELECT , is a table of peopleIDs who are connected with ALL the stuffIDs of T2. Following the example the result would be only the id 1 (the personID 3 has no to appear even if all the stuffIDs it is associated are

PostgreSQL: select all types that have an entry corresponding to all entries in another table

微笑、不失礼 提交于 2019-12-08 07:19:12
问题 I have two tables, producers and skis. Producers have their ID, and each model of skis knows the ID of its producer, each model also has a type, which is not unique among skis. I need to select all types that are produced by all of the producers (regardless of models). I wrote a query: select type from skis s where not exists ( select name from producers p except ( select name from producers p where (p.name=s.producer) ) ); It only works when I have 1 ski and one producer. What is a good way

PostgreSQL: select all types that have an entry corresponding to all entries in another table

拟墨画扇 提交于 2019-12-08 05:03:30
I have two tables, producers and skis. Producers have their ID, and each model of skis knows the ID of its producer, each model also has a type, which is not unique among skis. I need to select all types that are produced by all of the producers (regardless of models). I wrote a query: select type from skis s where not exists ( select name from producers p except ( select name from producers p where (p.name=s.producer) ) ); It only works when I have 1 ski and one producer. What is a good way to do that? EDIT for clarification: in the producer table, the column 'name' is their ID, and in the

Join between mapping (junction) table with specific cardinality

怎甘沉沦 提交于 2019-12-07 02:48:37
问题 I have a simple question about the most efficient way to perform a particular join. Take these three tables, real names have been changed to protect the innocent: Table: animal animal_id name ... ====================== 1 bunny 2 bear 3 cat 4 mouse Table: tags tag_id tag ================== 1 fluffy 2 brown 3 cute 4 small Mapping Table: animal_tag animal_id tag_id ================== 1 1 1 2 1 3 2 2 3 4 4 2 I want to find all animals that are tagged as ' fluffy ', ' brown ', and ' cute '. That

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

限于喜欢 提交于 2019-12-05 17:57:41
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 | | 1 | 1 | | 5 | Psychology | | 1 | 2 | +-------------------------------+ | 2 | 3 | | 2 | 5 | Course: |

Compound course prerequisites (One or more of a,b,c and either x or y as well as z style)

烈酒焚心 提交于 2019-12-05 13:12:36
Thanks everyone for the input, especially during the closing hours of the bounty, it's been incredible helpful. This is a followup question to Select courses that are completely satisfied by a given list of prerequisites , and further explains the situation. It is definitely recommended to read to help understand this question further. (Courses and subjects are distinct entities, subjects are only prerequisites for courses and need not be prerequisites for other subjects - think high school subjects leading to possible university courses) I have my database laid out as such. Prerequisite: +---

Join between mapping (junction) table with specific cardinality

元气小坏坏 提交于 2019-12-05 08:08:25
I have a simple question about the most efficient way to perform a particular join. Take these three tables, real names have been changed to protect the innocent: Table: animal animal_id name ... ====================== 1 bunny 2 bear 3 cat 4 mouse Table: tags tag_id tag ================== 1 fluffy 2 brown 3 cute 4 small Mapping Table: animal_tag animal_id tag_id ================== 1 1 1 2 1 3 2 2 3 4 4 2 I want to find all animals that are tagged as ' fluffy ', ' brown ', and ' cute '. That is to say that the animal must be tagged with all three . In reality, the number of required tags can

How to efficiently set subtract a join table in PostgreSQL?

无人久伴 提交于 2019-12-04 22:36:37
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_id FROM work_units_skills WHERE work_unit_id = work_units.id EXCEPT SELECT skill_id FROM workers_skills

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

≯℡__Kan透↙ 提交于 2019-12-04 19:08:03
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 | 2 | | | ... | | | ... | -------------- ----------------- How would you build a SQL query that would

What are ways to get mutual friends in a (1, 2) (2, 1) friendship scenario?

自作多情 提交于 2019-12-04 17:02:21
I'm developing a website wherein when a person adds another person as a friend, 2 entries are created. Let's say uid 1 adds uid 2 as a friend, the following rows are created in MySQL. activity_id uid1 uid2 1 1 2 2 2 1 uid 2 becomes friends with uid 3 : activity_id uid1 uid2 1 1 2 2 2 1 3 2 3 4 3 2 What are the ways to get the mutual friends between uid 1 and uid 2 ? I'm using php and MySQL with no PDO experience (for now). [EDIT] Okay, so I decided to organize/normalize the tables. The relationship only generates 1 row now. which makes the table: activity_id uid1 uid2 1 1 2 2 2 3 Joachim