2

我有两个 sql 查询,我想将它们组合在一个 SQL 查询中并显示在一个表上:

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

应该在表格中显示类似这样的内容:

名 sub1 sub2

耳鼻喉科 222 202

你能帮我吗,因为我是 mysql 和 php 的新手?

4

3 回答 3

2

已经给出了一些更好和更专业的答案,但是这个最好了解正在发生的事情

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
于 2013-10-15T06:05:11.667 回答
1

让我们知道这是否有效。

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`
于 2013-10-15T05:52:38.130 回答
1

IF您可以在列选择中使用简单的:

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
于 2013-10-15T06:09:53.460 回答