MySql查询图数据组 - 按列中的数据过滤

时间:2016-01-11 06:41:54

标签: mysql group-by

我有两张桌子,我想连接,
这是表1"日历"

enter image description here

这是表2"主要"
enter image description here

我有这个查询

SELECT calendar.datefield AS DATE, IFNULL( count( lead.insertDate ) , 0 ) AS task FROM lead RIGHT JOIN calendar ON ( DATE( lead.insertDate ) = calendar.datefield ) GROUP BY DATE LIMIT 30

效果很好,这是我得到的结果:

enter image description here

问题是我想按lead.lpid过滤数据。 我试图在查询结尾添加' WHERE lead.lpid = x'并在查询的开头添加lead.lpid数据,如下所示:

SELECT lead.lpid, calendar.datefield AS DATE, 
        IFNULL( count( lead.insertDate ) , 0 ) AS task
        FROM lead 
        RIGHT JOIN calendar ON ( DATE( lead.insertDate ) = calendar.datefield ) 
        GROUP BY DATE LIMIT 30 WHERE lead.lpid =  9

但我收到此错误

.....'WHERE lead.lpid =  9' at line 5 

我很善良,很遗憾。 任何帮助都会很棒!

这是我的SQL小提琴,我尝试过查询:SQLFiddle

3 个答案:

答案 0 :(得分:1)

错误的地方:

SELECT lead.lpid, calendar.datefield AS DATE, 
        IFNULL( count( lead.insertDate ) , 0 ) AS task
        FROM lead 
        RIGHT JOIN calendar ON ( DATE( lead.insertDate ) = calendar.datefield )
        WHERE lead.lpid =  9 
        GROUP BY DATE LIMIT 30 

注意:必须在GROUP BY

之前

您想要的新查询:

SELECT lead.lpid, calendar.datefield AS DATE, 
        IFNULL( count( lead.insertDate ) , 0 ) AS task
        FROM lead 
        RIGHT JOIN calendar ON ( DATE( lead.insertDate ) = calendar.datefield )
        AND lead.lpid =  9 
        GROUP BY DATE LIMIT 30

答案 1 :(得分:1)

SELECT lead.lpid, calendar.datefield AS DATE, 
        IFNULL( count( lead.insertDate ) , 0 ) AS task
        FROM lead 
        RIGHT JOIN calendar ON ( DATE( lead.insertDate ) = calendar.datefield ) 
        WHERE lead.lpid =  9 GROUP BY DATE LIMIT 30

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<table style="background-color:#9300C4;position:absolute;top:0;left:0;z-index:2;border:1px solid black" border="1" width="100%" >
        <tr>
          <td>
             <div ><a data-toggle="collapse" data-target="#demo"><h7 style="color:#FFFFFF;cursor: pointer;font-weight:600;"> Collapse >></h2></a></div>
             <div id="demo" class="collapse" style="width:100%;background-color:#FF3B29;" >
             
      			<br/>
              		<font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="white" style="margin-left:2%;"> This is for testing purpose. This is for testing purpose. </Font></a>
             	<br/>
               	 	<font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="white" style="margin-left:2%;"> This is for testing purpose. This is for testing purpose. </Font></a>
            	
            </div>
          </td>
        </tr>
  </table>
</body>
</html>

在线使用sql formater:Source