Table 'categories' (categoryabbrev are content table names):
categoryname categoryabbrev
Current Events ce
Commentary & Analysis ca
Personal Finance & Business pfb
Economics ec
Context cntxt
Family & Home fh
Health hlth
Security & Preparedness sp
Technology tech
Culture, Recreation & Travel crt
Table 'crt' (simplified), all other tables listed above have identical structure:
currentslot dateused mediadate pntitle sourceabbrev storyurl
0 14-Jan 11-Jan How To Travel Well On A Weak Dollar Forbes http://www.forbes.com/home/travel/2008/01/07/
0 14-Jan 14-Jan Fear on Mexican Beaches Near San Diego ABC http://travel.discovery.com/
Tables 'sources':
sourcename sourceabbrev sourceurl
ABC News ABC http://abcnews.go.com/
About.com About http://www.about.com/
Associated Content AC http://www.associatedcontent.com/
What I want to do is loop through the tables listed in the 'categories' table, searching for stories in all 10 tables with a common sourceabbrev, like 'ABC' within a specific date range.
Right now I use the following:
$sourceabbrev=media source name passed in
$datebegin=begin date passed in
$dateend=end date passed in
$query = "
(SELECT * FROM ce RIGHT JOIN sources
ON ce.sourceabbrev=sources.sourceabbrev
WHERE ce.sourceabbrev='$sourceabbrev'
and ce.dateused between '$datebegin' and '$dateend')
UNION
(SELECT * FROM ca RIGHT JOIN sources
ON ca.sourceabbrev=sources.sourceabbrev
WHERE ca.sourceabbrev='$sourceabbrev'
and ca.dateused between '$datebegin' and '$dateend')
UNION
(SELECT * FROM pfb RIGHT JOIN sources
ON pfb.sourceabbrev=sources.sourceabbrev
WHERE pfb.sourceabbrev='$sourceabbrev'
and pfb.dateused between '$datebegin' and '$dateend')
UNION
(SELECT * FROM ec RIGHT JOIN sources
ON ec.sourceabbrev=sources.sourceabbrev
WHERE ec.sourceabbrev='$sourceabbrev'
and ec.dateused between '$datebegin' and '$dateend')
UNION
(SELECT * FROM cntxt RIGHT JOIN sources
ON cntxt.sourceabbrev=sources.sourceabbrev
WHERE cntxt.sourceabbrev='$sourceabbrev'
and cntxt.dateused between '$datebegin' and '$dateend')
UNION
(SELECT * FROM fh RIGHT JOIN sources
ON fh.sourceabbrev=sources.sourceabbrev
WHERE fh.sourceabbrev='$sourceabbrev'
and fh.dateused between '$datebegin' and '$dateend')
UNION
(SELECT * FROM hlth RIGHT JOIN sources
ON hlth.sourceabbrev=sources.sourceabbrev
WHERE hlth.sourceabbrev='$sourceabbrev'
and hlth.dateused between '$datebegin' and '$dateend')
UNION
(SELECT * FROM sp RIGHT JOIN sources
ON sp.sourceabbrev=sources.sourceabbrev
WHERE sp.sourceabbrev='$sourceabbrev'
and sp.dateused between '$datebegin' and '$dateend')
UNION
(SELECT * FROM tech RIGHT JOIN sources
ON tech.sourceabbrev=sources.sourceabbrev
WHERE tech.sourceabbrev='$sourceabbrev'
and tech.dateused between '$datebegin' and '$dateend')
UNION
(SELECT * FROM crt RIGHT JOIN sources
ON crt.sourceabbrev=sources.sourceabbrev
WHERE crt.sourceabbrev='$sourceabbrev'
and crt.dateused between '$datebegin' and '$dateend')
ORDER BY dateused DESC";
This works, but has to be changed if I change the content tables. I know there is an elegant way to do this, just not what it is.
The site is www.prudentnews.com.