What I am looking for is a query that dynamically selects from ACTIVITY which is then applied based on ACTIVITY.what (either 'posted_blog' or 'posted_video') to retrieve the appropriate information from either the BLOG or VIDEO table.
I wonder whether A.
Is this possible to do? B.
If it is possible, how?
My three tables:
ACTIVITY
=========
id
what - can either be 'posted_blog' or 'posted_video'
reference
user
BLOG
=========
id
title
VIDEO
=========
id
title
The problem I'm having I think is where the case goes, and perhaps the proper usage?
So far I have tried as follows:
$result = mysql_query("SELECT ACTIVITY.user,CASE ACTIVITY.what WHEN "posted_blog" THEN (SELECT BLOG.title FROM BLOG WHERE BLOG.id=ACTIVITY.reference) WHEN "posted_video" THEN (SELECT VIDEO.title FROM VIDOES WHERE VIDEOS.id=ACTIVITY.reference) END FROM ACTIVITY WHERE ACTIVITY.user='10' ORDER BY ACTIVITY.id DESC ")or die(mysql_error());
The solution is:
mysql_query("SELECT CASE ACTIVITY.what
WHEN 'posted_video' THEN(SELECT VIDEO.title FROM VIDEO WHERE
VIDEO.id=ACTIVITY.reference)
WHEN 'posted_blog' THEN(SELECT BLOG.title FROM BLOG WHERE
BLOG.id=ACTIVITY.reference)END AS title
FROM ACTIVITY WHERE ACTIVITY.user='10' AND (ACTIVITY.action='posted_video' OR
ACTIVITY.action='posted_blog')")or die(mysql_error());
Now if I try to get an additional column from the VIDEO or BLOG table it is throwing an error Operand should contain 1 column(s). Does that mean I cannot select two columns? Would I then have to rewrite the case statement for any additional columns I need from those tables?