I have a say table table1
that has 3 columns column1, column2 and column3
.
column1
and column2
are FOREIGN KEY
with two other tables. However, the data in column3
refers to n number of tables.
For example, let's take a look at Facebook. To display actions, it can maintain a table that can have user1 photoliked photo1
or user1 statusliked status1
. Therefore, in this case, column3
cannot be a FOREIGN KEY
with a specific table.
Now there are two ways to get real data -
The first way is
SELECT user_id, verb_id, CASE WHEN verb_id = photoliked THEN (SELECT photo_name FROM photos WHERE photo_id = column3)
The second way is
SELECT user_id, verb_id, CASE WHEN verb_id = photoliked THEN p.photo_name WHEN verb_id = statusliked THEN s.status ELSE '' END AS performedon FROM table1 JOIN table2 ON user_id = user_id
Question
Which of the two ways is better to get the data? and which of the two queries is cheaper?
source share