Skip to content
Advertisement

SQL query for key value table with 1:n relation

I have a table in which I want to store images. Each image has arbitrary properties that I want to store in a key-value table.

The table structure looks like this

id fk_picture_id key value
1 1 camera iphone
2 1 year 2001
3 1 country Germany
4 2 camera iphone
5 2 year 2020
6 2 country United States

Now I want a query to find all pictures made by an iphone I could to something like this

This works without any problems. But as soon as I want to add another key to my query I am get stucked. Lets say, I want all pictures made by an iPhone in the year 2020, I can not do something like

…because this selects the id 1, 4 and 5.

At the end I might have 20 – 30 different criteria to look for, so I don’t think some sub-selects would work at the end.

I’m still in the design phase, which means I can still adjust the data model as well. But I can’t think of any way to do this in a reasonable way – except to include the individual properties as columns in my main table.

Advertisement

Answer

The solution I found – thanks to this article How to query data based on multiple ‘tags’ in SQL?

is that I made some changes to the database model

picture

id name
1 Picture 1
2 Picture 2

And then I created a table for the tags

tag

id tag
100 Germany
101 IPhone
102 United States

And the cross table

picture_tag

fk_picture_id fk_tag_id
1 100
1 101
2 101
2 102

For a better understanding of the datasets

Picture Tagname
Picture 1 Germany & Iphone
Picture 2 United States & IPhone

Now I can use the following statement

The only thing I need to do before the query is to collect the IDs of the tags I want to search for and put the number of tags in the having count statement.

If someone needs the example data, here are the sql statements for the tables and data

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement