relational-division

Finding combinations of specific values

。_饼干妹妹 提交于 2019-12-02 07:57:31
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 Try: SELECT col1 FROM mytable WHERE col2 IN (6, 7) GROUP BY col1 HAVING COUNT(DISTINCT col2) = 2 Erwin Brandstetter 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 KEY or UNIQUE constraint on (col1, col2) , like it's typically implemented. Else add DISTINCT .

Inner queries on a single table with IN and NOT IN conditions

安稳与你 提交于 2019-12-02 04:37:40
This is a modification to my previously answered question I have data in the table like below: ROLE_ID | USER_ID ------------------ 14 | USER A 15 | USER A 11 | USER B 13 | USER D 13 | USER A 15 | USER B 15 | USER D 12 | USER C 15 | USER C I would like to get user ids that ONLY have 13 and 15. So based on the example above, I should only get back USER D The query below was provided in my previous answer and the NOT IN part was added by me, however, that doesn't achieve the goal.. select user_id from my_table where role_id in (13,15) AND role_id not in (11,14) group by user_id. having count

MSSQL Select with “vertical”-where

亡梦爱人 提交于 2019-12-02 02:07:49
问题 I don't really know how to explain except with "vertical where". Imagine the following table: TAGID|PRODUCTID|SHOP_ID 59 |3418-7 |38 61 |3418-7 |38 60 |4227-4 |38 61 |4227-4 |38 Now I want to return all product IDs, that have relation to the tag IDs: 59,61. In other words, values of product ID where rows exist for both tag IDs. So I want to return 3418-7, but not 4227-4 How do I write this as simple as possible in a SQL statement? This is the working statement I have so far, but I feel this

MySQL Relational Division

落爺英雄遲暮 提交于 2019-12-02 01:20:41
I am having difficulties to solve one exercise: For which People there is a Restaurant, that serves ALL their favorite beers. (Yes, we actually have this in school :D) I have got 2 Tables that can be used: Table1: Favoritebeer (Name, Surname, beername) Table2: OnStock (beername, restaurant, quantity) My solution would be: OnStock % Favoritebeer There is no such thing like DIVISION in MySQL. Any ideas how I could solve that? I found the following on Wikipedia: http://en.wikipedia.org/wiki/Relational_algebra#Division_.28.C3.B7.29 which is exactly what I need but I am having difficulties to

MSSQL Select with “vertical”-where

放肆的年华 提交于 2019-12-02 01:01:12
I don't really know how to explain except with "vertical where". Imagine the following table: TAGID|PRODUCTID|SHOP_ID 59 |3418-7 |38 61 |3418-7 |38 60 |4227-4 |38 61 |4227-4 |38 Now I want to return all product IDs, that have relation to the tag IDs: 59,61. In other words, values of product ID where rows exist for both tag IDs. So I want to return 3418-7, but not 4227-4 How do I write this as simple as possible in a SQL statement? This is the working statement I have so far, but I feel this could be done in a much smarter way: SELECT productid FROM shop_tag_relations WHERE productid IN (select

I need to use a comma delimited string for SQL AND condition on each word

我的梦境 提交于 2019-12-02 00:52:17
问题 So I have a parameter. Lets say: @Tags = 'Red,Large,New' I have a field in my table called [Tags] Lets say one particular row that field contains "Red,Large,New,SomethingElse,AndSomethingElse" How can I break that apart to basically achieve the following logic, which I'll type for understanding but I know is wrong: SELECT * FROM MyTable WHERE Tags LIKE 'FirstWordInString' AND Tags Like 'SecondWordInString' AND Tags Like 'ThirdWordInString' But it knows where to stop? Knows if there's just one

I need to use a comma delimited string for SQL AND condition on each word

▼魔方 西西 提交于 2019-12-01 21:35:43
So I have a parameter. Lets say: @Tags = 'Red,Large,New' I have a field in my table called [Tags] Lets say one particular row that field contains "Red,Large,New,SomethingElse,AndSomethingElse" How can I break that apart to basically achieve the following logic, which I'll type for understanding but I know is wrong: SELECT * FROM MyTable WHERE Tags LIKE 'FirstWordInString' AND Tags Like 'SecondWordInString' AND Tags Like 'ThirdWordInString' But it knows where to stop? Knows if there's just one word? Or two? Or three? Workflow: Someone clicks a tag and the dataset is filtered by that tag. They

Many-to-many relation filter

核能气质少年 提交于 2019-12-01 11:57:26
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 field in res_partner which is category_value_ids and has one2many relation with res_partners_category

Can all SQL queries be represented in Relational Algebra, Domain and Tuple relational calculus

旧时模样 提交于 2019-12-01 11:54:21
问题 My query includes a having and count or all in. How are these represented in RA/DRC/TRC? Would I have to simplify my SQL query even more? Here is a simplified example: empl(employee (primary key), city) managers(employee (primary key), manager (foreign key of employee)) If I were to find all the employees who are managers (from any city) of ALL the employees in city X.. I would need to use having/count. Not sure how this would be done in RA/DRC/TRC. I know the need for such a query might not

Can all SQL queries be represented in Relational Algebra, Domain and Tuple relational calculus

帅比萌擦擦* 提交于 2019-12-01 11:49:36
My query includes a having and count or all in. How are these represented in RA/DRC/TRC? Would I have to simplify my SQL query even more? Here is a simplified example: empl(employee (primary key), city) managers(employee (primary key), manager (foreign key of employee)) If I were to find all the employees who are managers (from any city) of ALL the employees in city X.. I would need to use having/count. Not sure how this would be done in RA/DRC/TRC. I know the need for such a query might not make sense but assume it is sensible for the purpose of this question. Thanks Your query was stated a