[英]Unsure if the following WHERE statement is valid when trying to return dates from a table based on dates in a different table
I have written an sql query, and I've tested it such that, I've come to the conclusion that the WHERE statement is giving me issues.我已经编写了一个 sql 查询,并且我已经对其进行了测试,因此我得出的结论是 WHERE 语句给我带来了问题。
Scenario: I have a table which contains a list of dates.场景:我有一个包含日期列表的表格。 Lets call this table, table A.
让我们称这个表为表 A。
I have a second table which contains table 2 columns, "Start Date" and "End Date" .我有第二个表,其中包含表 2 列, “开始日期”和“结束日期” 。
What I want to do is, filter the dates in table A which contains a list of dates based on the dates that exist between the "Start Date" and "End Date".我想要做的是,根据“开始日期”和“结束日期”之间存在的日期过滤包含日期列表的表 A 中的日期。
For instance, if "Start Date" - 01/01/2017 and "End Date" is 01/02/2017, then I want to return the dates between the Start Date and End Date from table A which contains all the dates.例如,如果“开始日期” - 01/01/2017 和“结束日期”是 01/02/2017,那么我想从包含所有日期的表 A 中返回开始日期和结束日期之间的日期。
Here is the SQL Query I have written:这是我编写的 SQL 查询:
$query = "SELECT ec.ElecEnergy, ec.GasEnergy, ec.Date, c.ElecCharge, c.GasCharge, ec.User, st.AmountSetElec, st.AmountSetGas,
st.StartDate, st.EndDate, st.TargetElecCharge, st.TargetGasCharge, ua.Username
FROM energyconsumption ec INNER JOIN
useraccount ua
ON ec.User = ua.id
INNER JOIN
energytarget st
ON st.customerID = ua.id INNER JOIN
Charge c
ON ec.ChargeID = c.id
INNER JOIN
energytarget st ON
st.energyID = ec.id
WHERE ec.Date = ec.Date >= st.StartDate AND ec.Date <= st.EndDate
GROUP BY ec.ElecEnergy, ec.GasEnergy, c.ElecCharge, c.GasCharge, st.AmountSetElec, st.AmountSetGas, st.StartDate, st.EndDate, ua.Username ";
Question How do I write a WHERE statement that filters for dates in table A based on the dates that exist between the Start Date and End Date in table B.问题如何编写 WHERE 语句,根据表 B 中的开始日期和结束日期之间存在的日期筛选表 A 中的日期。
Thanks!谢谢!
EDIT:编辑:
I tried to use the BETWEEN statement to resolve the statement, this is what I did:我尝试使用 BETWEEN 语句来解决该语句,这就是我所做的:
$query = "SELECT ec.ElecEnergy, ec.GasEnergy, ec.Date, c.ElecCharge, c.GasCharge, ec.User, st.AmountSetElec, st.AmountSetGas,
st.StartDate, st.EndDate, st.TargetElecCharge, st.TargetGasCharge, ua.Username
FROM energyconsumption ec INNER JOIN
useraccount ua
ON ec.User = ua.id
INNER JOIN
energytarget st
ON st.customerID = ua.id INNER JOIN
Charge c
ON ec.ChargeID = c.id
INNER JOIN
energytarget st ON
st.energyID = ec.id
WHERE ec.Date BETWEEN ec.Date >= st.StartDate AND ec.Date <= st.EndDate
GROUP BY ec.ElecEnergy, ec.GasEnergy, c.ElecCharge, c.GasCharge, st.AmountSetElec, st.AmountSetGas, st.StartDate, st.EndDate, ua.Username ";
Also, could there perhaps be an issue with the statement "此外,该声明是否可能存在问题“
INNER JOIN
energytarget st ON
st.energyID = ec.id"
which is just above the WHERE statement就在 WHERE 语句之上
Your query has an error in JOIN clauses:您的查询在 JOIN 子句中有错误:
There are two join clauses with the same alias:有两个具有相同别名的连接子句:
INNER JOIN energytarget st ON st.customerID = ua.id
INNER JOIN energytarget st ON st.energyID = ec.id
SELECT ec.ElecEnergy, ec.GasEnergy, ec.Date, c.ElecCharge, c.GasCharge,
ec.User, st.AmountSetElec, st.AmountSetGas, st.StartDate,
st.EndDate, st.TargetElecCharge, st.TargetGasCharge, ua.Username
FROM energyconsumption ec
INNER JOIN useraccount ua ON ec.User = ua.id
INNER JOIN energytarget st ON st.customerID = ua.id
INNER JOIN Charge c ON ec.ChargeID = c.id
INNER JOIN energytarget st ON st.energyID = ec.id
WHERE
ec.Date = ec.Date >= st.StartDate AND ec.Date <= st.EndDate
GROUP BY ec.ElecEnergy, ec.GasEnergy, c.ElecCharge,
c.GasCharge, st.AmountSetElec, st.AmountSetGas,
st.StartDate, st.EndDate, ua.Username
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.