sql-match-all

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

SQL one-to-many match the one side by ALL in many side

夙愿已清 提交于 2020-01-13 08:44:29
问题 In the following one to many CREATE TABLE source(id int, name varchar(10), PRIMARY KEY(id)); CREATE TABLE params(id int, source int, value int); where params.source is a foreign key to source.id INSERT INTO source values(1, 'yes'); INSERT INTO source values(2, 'no'); INSERT INTO params VALUES(1,1,1); INSERT INTO params VALUES(2,1,2); INSERT INTO params VALUES(3,1,3); INSERT INTO params VALUES(4,2,1); INSERT INTO params VALUES(5,2,3); INSERT INTO params VALUES(6,2,4); If i have a list of param

How to do this query in Mysql?

无人久伴 提交于 2020-01-05 08:26:32
问题 I have 3 tables, message, subject and message_subject_rel . The idea is to have messages that can relate to a lot of subjects and then do a cross subject search. Lets say I have a message: Id: 1, Message: This is a message 2 subjects: Id:1, Subject: Math Id:2, Subject: Science And there's 2 message_subject_rel entries that go: Id: 1, message_id: 1, subject_id: 1 Id: 2, message_id: 1, subject_id: 2 If i wanted to search the messages that are related with math, I would do a simple join with the

Can you solve this simple SQL query?

别说谁变了你拦得住时间么 提交于 2019-12-19 10:17:23
问题 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

Querying based on a set of Named Attributes/Values

孤者浪人 提交于 2019-12-08 11:48:26
问题 I am working with a set of what is essentially Attribute/Value pairs (there's actually quite a bit more to this, but I'm simplifying for the sake of this question). Effectively you can think of the tables as such: Entities (EntityID,AttributeName,AttributeValue) PK=EntityID,AttributeName Targets (TargetID,AttributeName,AttributeValue) PK=TargetID,AttributeName How would you query with SQL the set of EntityID,TargetID for which an Entity has all the attributes for a target as well as the

SQL one-to-many match the one side by ALL in many side

老子叫甜甜 提交于 2019-12-05 03:27:49
In the following one to many CREATE TABLE source(id int, name varchar(10), PRIMARY KEY(id)); CREATE TABLE params(id int, source int, value int); where params.source is a foreign key to source.id INSERT INTO source values(1, 'yes'); INSERT INTO source values(2, 'no'); INSERT INTO params VALUES(1,1,1); INSERT INTO params VALUES(2,1,2); INSERT INTO params VALUES(3,1,3); INSERT INTO params VALUES(4,2,1); INSERT INTO params VALUES(5,2,3); INSERT INTO params VALUES(6,2,4); If i have a list of param values (say [1,2,3]), how do I find all the sources that have ALL of the values in the list (source 1,

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

Select in a many-to-many relationship in MySQL

▼魔方 西西 提交于 2019-11-30 05:44:58
问题 I have two tables in a MySQL database, Locations and Tags, and a third table LocationsTagsAssoc which associates the two tables and treats them as a many-to-many relationship. Table structure is as follows: Locations --------- ID int (Primary Key) Name varchar(128) LocationsTagsAssoc ------------------ ID int (Primary Key) LocationID int (Foreign Key) TagID int (Foreign Key) Tags ---- ID int (Primary Key) Name varchar(128) So each location can be tagged with multiple tagwords, and each

How do I avoid dynamic SQL when using an undetermined number of parameters?

给你一囗甜甜゛ 提交于 2019-11-29 00:10:36
I have a StackOverflow-like tagging system for a database I'm working on. And I'm writing a stored procedure that looks for results based on an undetermined number of tags in a WHERE clause. There could be anywhere between 0 and 10 tags to filter results. So for example the user could be searching for items tagged with 'apple', 'orange', and 'banana' and each result must include all 3 tags. My query is made even more complicated because I'm also dealing with a cross reference table for the tagging, but for the purposes of this question I won't go into that. I know I can do some string