I'm creating database for a place which have meeting rooms for reserve & sell books. The schema is genericized to allow reuse of tables for multiple data sets.
The diagram is slightly incorrect; there are two selector
tables which actually have distinct names. The selector
table in the top right is actually called option_selector
and the bottom left is values_selector
. I fill the resource_type
table with 1, room
; 2, book
then resource_option
with 1, Name
; 2, size
; 3,Author
; 4,Publish Year
and fill the option_selector
table with the rows.
1, 1, 1
for mapping room to name2, 1, 2
for mapping room to size3, 2, 1
for mapping book to name4, 2, 3
for mapping book to author5, 2, 4
for mapping book to Publish Year
I also fill the option_selector
and option_value
tables with some rows. Then I run this query
SELECT r.id as 'book ID', v.value as 'Name'
FROM resources r
JOIN value_selector s ON r.id = s.resource_id
JOIN resourse_option_value v ON s.value_id = v.id
WHERE v.option_selector_id = 3
ORDER BY r.id
The result
book ID Name
5 Journey to the center of the Earth
6 Oliver Travels
7 C How To Program
8 Java How To Program
And that's the right output.
The problem is I want add more data columns to be selectable with the book records, for example 'Author' & 'Publish Year' to make this output table look like
book ID Name Author Publish Year
I tried this
SELECT r.id as 'book ID', v.value as 'Name', v2.value as 'Author', v3.value as 'Year'
FROM resourses r
JOIN value_selector s ON r.id = s.resourse_id
JOIN resourse_option_value v ON s.value_id = v.id
JOIN resourse_option_value v2 ON s.value_id = v2.id
JOIN resourse_option_value v3 ON s.value_id = v3.id
WHERE
v.option_selector_id = 3 AND
v2.option_selector_id = 4 AND
v3.option_selector_id = 5
ORDER BY r.id
This gives an empty result
Database sql : http://sqlfiddle.com/#!9/302671/3