在mysql表中的两个选择查询的内连接

时间:2017-09-18 16:20:38

标签: php mysql

我有这张桌子

id empid  reaction        date_t
1  emp090 not_interested  2017-09-16
2  emp090 not_interested  2017-09-16 

我希望根据empiddate_t计算反应次数。 我试过这个查询

SELECT c.COUNT(reaction) as interested,c.empid FROM `cases` c 
WHERE c.reaction="interested" and c.empid="EMP12654" 
  AND c.date_t BETWEEN  "2017-09-15"  AND "2017-09-18" 
INNER JOIN 
( SELECT cases.empid COUNT(reaction) not interested FROM `cases` 
  WHERE cases.reaction="not_interested" and cases.empid="EMP12654" 
    AND cases.date_t BETWEEN  "2017-09-15"  AND "2017-09-18" ) 
AS alpha on alpha.empid=c.empid;

有谁能告诉我如何正确地做到这一点?

2 个答案:

答案 0 :(得分:0)

你走了。

SELECT empid, count(reaction) as count, date_t
FROM cases
WHERE reaction="interested" AND empid="EMP12654" 
AND date_t BETWEEN "2017-09-15" AND "2017-09-18" 
GROUP BY empid, date_t;

答案 1 :(得分:0)

您的查询中的错误与几个错误有关:连接子句之间的顺序错误,  内部联接subselect缺少表名,子查询中列名缺少逗号,子句

缺少

但是如果你只想要对date_t和empid感兴趣的计数

您可以通过例如:

使用分组
  SELECT  
         c.empid
       , c.date_t
       , c.COUNT(*) as interested
  FROM `cases` c 
  WHERE  WHERE c.reaction="interested" 
  AND c.empid="EMP12654" 
  AND c.date_t BETWEEN  "2017-09-15"  AND "2017-09-18"
  GROUP BY  c.empid, c.date_t