relational-division

Can you solve this simple SQL query?

风格不统一 提交于 2019-12-01 10:46:13
Suppose it's a website that sells photo cameras. Here are my entities (tables): Camera: A simple camera Feature: A feature like: 6mp, max resolution 1024x768, The thing is between cameras and feature i've got a Many to Many relationship, so i have an extra table: camera -> cameras_features -> feature So, the query is simple: How to get all the cameras that have the feature 1,2 and 3? It's like constructing a bitmap index. Data you can use to test if the solution is ok C1 has features 1,2,3 C2 has features 1,2,4 C3 has features 1,2 Here are querys and the expected result: Show all the cameras

Many-to-many relation filter

江枫思渺然 提交于 2019-12-01 10:00:42
问题 I need to filter my query with categories table which has many2many relation with another table. Is it possible to filter query with many2many relation? Table res_partner has many2many field category_id relating to table res_partner_category.res_partner , or let's just say partners can have many categories. What I need is to filter res_partners table where it has category named 'business' or 'retail'. If it doesn't have any of these categories, it should not be shown. Also there is another

SQL queries involving ' for all' [closed]

允我心安 提交于 2019-12-01 01:42:57
I could not get a hint on how to write SQL queries for A and B for the following schema. Programme (Pid:int, Department:string...) Employee (Eid:int, Department:string..) Participation (Pid:int, Eid:int, ..) A. Names of programmes participated by all employees B. Names of employees participating in all his department's programmes. Any guidelines would be helpful. Haven't tried these, but this is what I would be thinking: SELECT pg.Name FROM Participation AS p INNER JOIN Programme AS pg ON p.Pid = pg.Pid GROUP BY p.Pid HAVING COUNT(*) = (SELECT COUNT(*) FROM Employeee) SELECT e.Name FROM

SQL queries involving ' for all' [closed]

吃可爱长大的小学妹 提交于 2019-11-30 20:54:03
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I could not get a hint on how to write SQL queries for A and B for the following schema. Programme (Pid:int, Department:string...) Employee (Eid:int, Department:string..) Participation (Pid:int, Eid:int, ..) A.

Join query with only columns that have all values in `in` clause

旧时模样 提交于 2019-11-30 09:37:21
问题 I'm creating a simple filtering system for my website. I have a many to many relationship between venues and amenities. Here are my tables. NOTE: all ids are uuids. Making them short for simplicity venues: | id | name | _________________________ | 'aaa' | 'first venue' | | 'bbb' | 'second venue' | | 'ccc' | 'third venue' | amenities: | id | name | ___________________________ | 'aaa' | 'first amenity' | | 'bbb' | 'second amenity' | | 'ccc' | 'third amenity' | amenity_venue: | amenity_id |

SQL find sets with common members (relational division)

别等时光非礼了梦想. 提交于 2019-11-30 09:20:33
问题 I have separate sets of "classes" and "groups", each of which has been assigned one or more tags. I would like to find, for each group, the subset of classes that contains the same (or more) tags for each group. Some sample data: declare @Groups table ( GroupID int, TagID int ) insert @Groups values (1,1),(1,2),(1,3), (2,1),(2,2), (3,1),(3,2),(3,3),(3,4) declare @Classes table ( ClassID int, TagID int ) insert @Classes values (1,1),(1,2), (2,1),(2,2), (3,1),(3,2),(3,3) select * from @Groups

MySQL select join where AND where

我怕爱的太早我们不能终老 提交于 2019-11-30 06:57:01
问题 I have two tables in my database: Products id (int, primary key) name (varchar) ProductTags product_id (int) tag_id (int) I would like to select products having all given tags. I tried: SELECT * FROM Products JOIN ProductTags ON Products.id = ProductTags.product_id WHERE ProductTags.tag_id IN (1, 2, 3) GROUP BY Products.id But it gives me products having any of given tags, instead of having all given tags. Writing WHERE tag_id = 1 AND tag_id = 2 is pointless, because no rows will be returned.

Join query with only columns that have all values in `in` clause

*爱你&永不变心* 提交于 2019-11-29 16:02:24
I'm creating a simple filtering system for my website. I have a many to many relationship between venues and amenities. Here are my tables. NOTE: all ids are uuids. Making them short for simplicity venues: | id | name | _________________________ | 'aaa' | 'first venue' | | 'bbb' | 'second venue' | | 'ccc' | 'third venue' | amenities: | id | name | ___________________________ | 'aaa' | 'first amenity' | | 'bbb' | 'second amenity' | | 'ccc' | 'third amenity' | amenity_venue: | amenity_id | venue_id | ______________________________ | 'aaa' | 'aaa' | | 'bbb' | 'aaa' | | 'ccc' | 'aaa' | | 'aaa' |

SQL find sets with common members (relational division)

梦想的初衷 提交于 2019-11-29 14:57:22
I have separate sets of "classes" and "groups", each of which has been assigned one or more tags. I would like to find, for each group, the subset of classes that contains the same (or more) tags for each group. Some sample data: declare @Groups table ( GroupID int, TagID int ) insert @Groups values (1,1),(1,2),(1,3), (2,1),(2,2), (3,1),(3,2),(3,3),(3,4) declare @Classes table ( ClassID int, TagID int ) insert @Classes values (1,1),(1,2), (2,1),(2,2), (3,1),(3,2),(3,3) select * from @Groups select * from @Classes And output: GroupID TagID 1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3 3 4 ClassID TagID 1 1 1

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

倾然丶 夕夏残阳落幕 提交于 2019-11-29 08:32:05
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 in both categories 1 and 2 and in invoices 3 and 4. As a start, I've written a stored-procedure that