如何在左连接中使用不同的 WHERE 子句
我有两个文本字段,我将每个文本字段值发布到 MySQL 查询中,特别是在 WHERE 子句中。
$sWhere = " WHERE MONTH(Inspection_datetime) = '".$Month."' AND
YEAR(Inspection_datetime) = '".$Year."' ";
然后我尝试将这个查询组合起来,如下所示:
$sQuery = "SELECT A.id,A.Line,
week1.1st, week2.2nd, week3.3rd, week4.4th, week5.5th,
IFNULL(week1.1st,0) + IFNULL(week2.2nd,0) + IFNULL(week3.3rd,0)
+ IFNULL(week4.4th,0) + IFNULL(week5.5th,0) AS Total
FROM inspection_report AS A
LEFT JOIN(
SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 +
SUM(C)*0.1)/COUNT(Serial_number) AS 1st
FROM inspection_report
WHERE DAY(Inspection_datetime) BETWEEN 1 AND 7
GROUP BY Line, WEEK(Inspection_datetime),
YEAR(Inspection_datetime)
) AS week1 USING (Line)
LEFT JOIN(
SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS 2nd
FROM inspection_report
WHERE DAY(Inspection_datetime) BETWEEN 8 AND 14
GROUP BY Line, WEEK(Inspection_datetime), YEAR(Inspection_datetime)
) AS week2 USING (Line)";
.........//SAME LIKE ABOVE JUST CHANGE INTO another week's range
$sWhere = " WHERE MONTH(Inspection_datetime) = '".$Month."' AND
YEAR(Inspection_datetime) = '".$Year."' ";
$sGroupBy = " GROUP BY A.Line ";
$sQuery.= $sWhere.$sGroupBy;
我希望它可以每周计算数据,并且还可以按 $Month
和 $Year
进行过滤。
但我得到了不正确的结果。结果每周统计整个月份和全年的数据。 我该如何修复它?
I have two textfields, I posted each textfield value into MySQL query specially in WHERE CLAUSE.
$sWhere = " WHERE MONTH(Inspection_datetime) = '".$Month."' AND
YEAR(Inspection_datetime) = '".$Year."' ";
Then i tried to combine this query become like below:
$sQuery = "SELECT A.id,A.Line,
week1.1st, week2.2nd, week3.3rd, week4.4th, week5.5th,
IFNULL(week1.1st,0) + IFNULL(week2.2nd,0) + IFNULL(week3.3rd,0)
+ IFNULL(week4.4th,0) + IFNULL(week5.5th,0) AS Total
FROM inspection_report AS A
LEFT JOIN(
SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 +
SUM(C)*0.1)/COUNT(Serial_number) AS 1st
FROM inspection_report
WHERE DAY(Inspection_datetime) BETWEEN 1 AND 7
GROUP BY Line, WEEK(Inspection_datetime),
YEAR(Inspection_datetime)
) AS week1 USING (Line)
LEFT JOIN(
SELECT Line, (SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS 2nd
FROM inspection_report
WHERE DAY(Inspection_datetime) BETWEEN 8 AND 14
GROUP BY Line, WEEK(Inspection_datetime), YEAR(Inspection_datetime)
) AS week2 USING (Line)";
.........//SAME LIKE ABOVE JUST CHANGE INTO another week's range
$sWhere = " WHERE MONTH(Inspection_datetime) = '".$Month."' AND
YEAR(Inspection_datetime) = '".$Year."' ";
$sGroupBy = " GROUP BY A.Line ";
$sQuery.= $sWhere.$sGroupBy;
i want it can counting data in weekly and also it can filtering by $Month
and $Year
.
But i got incorrect result. The result count data weekly for all month and all year.
How do i do to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
WHERE MONTH(Inspection_datetime) = '".$Month."' AND YEAR(Inspection_datetime) = '".$Year.
添加到每个子查询当前存在一些问题查询:
Line
字段连接表,您告诉 MySql 要做的就是找到包含一个月和一年数据的行,并为这些行提取所有每周数据您You need to add the
WHERE MONTH(Inspection_datetime) = '".$Month."' AND YEAR(Inspection_datetime) = '".$Year.
to every subqueryThere are a couple things wrong with your current query:
Line
field only, what you are telling MySql to do, is find the lines that have data for a month and year, and for those lines pull all their weekly data.