I have two sql queries which i want to combine in one SQL Query and display on one table:
SELECT subname,subscribers as sub1 FROM reports_subreport where country ='1' and mp='0' and date ='2013-10-15' and NOT(subname LIKE '%Test%') order by site,subname
SELECT subscribers as sub2 FROM reports_subreport where country ='1' and mp='0' and date ='2013-10-08' and NOT(subname LIKE '%Test%') order by site,subname
Should Display something Like This in a table:
subname sub1 sub2
ENT 222 202
Could you please help me out here because iam new to mysql and php?
Let us know if this works.
SELECT `subname`, CASE WHEN `date`='2013-10-15' THEN `subscribers` ELSE 'NONE' `s1` , CASE WHEN `date`='2013-10-08' THEN `subscribers` ELSE 'NONE' `s2`
WHERE `country` ='1' AND `mp`='0' AND NOT(`subname` LIKE '%Test%') ORDER BY `site`,`subname`
There are some better and professional answers already been given, but this one is best to understand what is going around
SELECT subname,
(SELECT subscribers
FROM reports_subreport
WHERE country ='1' AND mp='0'
AND date ='2013-10-15'
AND NOT(subname LIKE '%Test%')
ORDER BY site,subname LIMIT 1) AS sub1,
(SELECT subscribers
FROM reports_subreport
WHERE country ='1' AND mp='0'
AND date ='2013-10-08'
AND NOT(subname LIKE '%Test%')
ORDER BY site,subname LIMIT 1) AS sub2,
FROM reports_subreport WHERE country ='1' AND mp='0'
AND date ='2013-10-15' AND NOT(subname LIKE '%Test%')
ORDER BY site,subname
You can use a simple IF
in your column selection:
SELECT
subname,
if(date ='2013-10-15', subscribers) as sub1,
if(date ='2013-10-08', subscribers) as sub2
FROM reports_subreport
WHERE country ='1' and mp='0' and date IN ('2013-10-15','2013-10-08') and NOT(subname LIKE '%Test%') order by site,subname