1

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?

3
  • Seems like the field named 'reference' is the foreign key, not sub_id
    – pdjota
    Commented Jan 18, 2012 at 17:50
  • @OlofEdler That link you posted does not have any necessary formatting information on it. Aside from the formatting you already did, what do you want changed? Commented Jan 18, 2012 at 17:59
  • I had a database course (remember very little unfortunately) in which we practised different JOIN syntax. (OUTER, INNER, other joins, joins combined). Strongly guessing something like that would work.
    – r4.
    Commented Jan 18, 2012 at 18:34

2 Answers 2

1

I'm not sure what exactly you want to do, but if you want to query all activity that are of "type" blog and all activities that are of "type" video the relations should be the other way round:

ACTIVITY  
=========
id  
what
user  

BLOG 
========= 
id  
activity_id
title 

VIDEO 
========= 
id  
activity_id
title

And then you can make two separate queries:

//First query
SELECT a.user, b.title
FROM activity a 
JOIN blog b ON b.activity_id = a.id 

//Second query
SELECT a.user, v.title 
FROM activity a 
JOIN video v ON v.activity_id = a.id

Or with one query:

SELECT a.user, a.what, IFNULL(b.title,v.title) AS title  
FROM activity a 
LEFT OUTER JOIN blog b ON b.activity_id = a.id 
LEFT OUTER JOIN video v ON v.activity_id = a.id
3
  • I do not have the option of changing the table layouts. This one query will end up retrieving what would be the equivalent of 20+ queries if I loop through two queries to get all the info I need; way too many db requests to be effective. Commented Jan 18, 2012 at 19:20
  • 1
    If you really want to have one query you could do an outer join. See my edited answer. But still you would have to change the table layout or use the solution of @Tickthokk.
    – Sbhklr
    Commented Jan 18, 2012 at 19:33
  • 1
    And regarding performance, joining the tables with the relationship reversed as in my solution is much more efficient than doing a string comparison on the "what" attribute.
    – Sbhklr
    Commented Jan 18, 2012 at 19:38
0

First, this may not be the "best" solution, but it's what I commonly do in those situations. Also, this is not dynamic. This is made with the intention that there will only ever be two, but you could expand it by nesting IF's.

SELECT a.id, a.what, a.user, IF(a.what="BLOG",b.title,v.title) AS title
FROM activity AS a
LEFT JOIN blog AS b ON b.id = a.reference 
LEFT JOIN video AS v ON v.id = a.reference 
WHERE ...
ORDER BY ...

Again, not "pretty", or "dynamic", and I'm pretty sure it'll probably parse more data than needed. You could try adding to the end of the JOIN's "AND a.what = 'BLOG'" or 'VIDEO', but that might be invalid so I didn't include it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.