问题
Its been a while since I have done any SQL and I am not sure if this problem has an easy solution or not. Also I am a bit of a Noob.
I am trying to put together an image gallery that allows users to use tags in order to search for images and then click on additional tags to refine the search and lower the number of results but I am having a big issue with the queries involved.
This is a simplified version of my current database structure:
( 2 tables with an additional Many-to-Many link table )
CREATE TABLE images(
image_id INT(12) AUTO_INCREMENT,
image_name VARCHAR(128),
PRIMARY KEY(image_id)
)ENGINE= INNODB;
CREATE TABLE tags(
tag_name VARCHAR(64) NOT NULL,
PRIMARY KEY(tag_name)
)ENGINE= INNODB;
CREATE TABLE images_tags_link(
image_id_fk INT(12),
tag_name_fk VARCHAR(64) NOT NULL,
PRIMARY KEY(image_id_fk,tag_name_fk),
FOREIGN KEY(image_id_fk) REFERENCES images(image_id),
FOREIGN KEY(tag_name_fk) REFERENCES tags(tag_name)
)ENGINE= INNODB;
Sample Data:
===images===
___________________________
| image_id | image_name |
|----------|----------------|
| 1 | image_001.jpg |
| 2 | image_002.jpg |
| 3 | image_003.png |
| 4 | image_004.jpg |
| 5 | image_005.gif |
---------------------------
===tags===
_______________
| tag_name |
|---------------|
| Landscape |
| Portrait |
| Illustration |
| Photo |
| Red |
| Blue |
| Character |
| Structure |
---------------
===images_tags_link===
________________________________
| image_id_fk | tag_name_ fk |
|-------------|------------------|
| 1 | Landscape |
| 1 | Illustration |
| 1 | Blue |
| 2 | Blue |
| 2 | structure |
| 2 | Landscape |
| 3 | Illustration |
| 4 | Red |
| 4 | Portrait |
| 4 | Character |
| 5 | Blue |
| 5 | Photo |
--------------------------------
My Problem is with the following Query:
I am looking for a single Query that can select all 'image_names' from the IMAGES table that have all the users listed tags, for example a user may search for the 'Blue' AND 'Landscape' tags which should only output the image_names 'image_001.jpg' AND 'image_002.jpg'.
===INPUT===
The Users chosen tags: ( 'Blue' , 'Landscape' )
===OUTPUT===
The image_names that have ALL the listed tags: ( 'image_001.jpg' , 'image_002.jpg' )
Thanks in advance.
回答1:
2 simple ways to do it.
Either of the following, depending on columns required, number of possible tags, etc.
SELECT *
FROM images
INNER JOIN images_tags_link a ON images.image_id = a.image_id_fk AND a.tag_name_fk = 'Blue'
INNER JOIN images_tags_link b ON images.image_id = b.image_id_fk AND b.tag_name_fk = 'Landscape'
SELECT images.image_id, images.image_name, COUNT(*) AS tag_count
FROM images
INNER JOIN images_tags_link a ON images.image_id = a.image_id_fk
WHERE a.tag_name_fk IN ('Blue', 'Landscape')
GROUP BY images.image_id, images.image_name
HAVING tag_count = 2
回答2:
This what they called Relation Division, and here is one way to do so:
SELECT i.*
FROM Images i
INNER JOIN
(
SELECT image_id_fk
FROM images_tags_link
WHERE tag_name_fk IN ('Blue' , 'Landscape')
GROUP BY image_id_fk
HAVING COUNT(DISTINCT tag_name_fk) = 2
) t ON i.image_id = t.image_id_fk;
SQL Fiddle Demo
This will give you:
| IMAGE_ID | IMAGE_NAME |
----------------------------
| 1 | image_001.jpg |
| 2 | image_002.jpg |
The idea behind this query is:
GROUP BY image_id_fk
HAVING COUNT(DISTINCT tag_name_fk) = 2
in the subquery, which will ensure that any image have both tags, if it has only one, the COUNT
will be 0 and therefore it will be eliminated.
回答3:
Here's another example using EXISTS:
SELECT *
FROM Images I
WHERE EXISTS (
SELECT image_id_fk
FROM Images_Tags_Link I2
WHERE I2.image_id_fk = I.Image_Id
AND tag_name_fk IN ('Blue' , 'Landscape')
GROUP BY I2.image_id_fk
HAVING COUNT(DISTINCT I2.tag_name_fk) > 1)
And one using IN:
SELECT *
FROM Images I
WHERE Image_Id IN (
SELECT image_id_fk
FROM Images_Tags_Link I2
WHERE I2.image_id_fk = I.Image_Id
AND tag_name_fk IN ('Blue' , 'Landscape')
GROUP BY I2.image_id_fk
HAVING COUNT(DISTINCT I2.tag_name_fk) > 1)
Good luck.
回答4:
Try this simplier way to do it
Select
image_name
from
images_tags_link
where tag_name_fk in ('Blue','Landscape')
来源:https://stackoverflow.com/questions/14683485/tag-based-sql-query