Php 在尝试基于其他表中的日期从表中返回日期时,不确定以下WHERE语句是否有效

Php 在尝试基于其他表中的日期从表中返回日期时,不确定以下WHERE语句是否有效,php,mysql,date,Php,Mysql,Date,我编写了一个sql查询,并对其进行了测试,得出了WHERE语句给我带来问题的结论 情景: 我有一张包含日期列表的表格。我们把这张桌子叫做A桌 我有第二个表,其中包含表2列,“开始日期”和“结束日期” 我要做的是,筛选表A中的日期,该表包含基于开始日期和结束日期之间存在的日期的日期列表。 例如,如果“开始日期”-01/01/2017和“结束日期”是01/02/2017,那么我想从包含所有日期的表A返回开始日期和结束日期之间的日期 以下是我编写的SQL查询: $query = "SELECT ec.

我编写了一个sql查询,并对其进行了测试,得出了WHERE语句给我带来问题的结论

情景: 我有一张包含日期列表的表格。我们把这张桌子叫做A桌

我有第二个表,其中包含表2列,“开始日期”“结束日期”

我要做的是,筛选表A中的日期,该表包含基于开始日期和结束日期之间存在的日期的日期列表。

例如,如果“开始日期”-01/01/2017和“结束日期”是01/02/2017,那么我想从包含所有日期的表A返回开始日期结束日期之间的日期

以下是我编写的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 ";

在WHERE语句的正上方,您的查询在JOIN子句中有一个错误:

有两个具有相同别名的join子句:

    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
st.customerID=ua.id上的内部联接energytarget st st.energyID=ec.id上的内部联接energytarget st 选择ec.ElecEnergy、ec.GasEnergy、ec.Date、c.ElecCharge、c.GasCharge、, 欧共体用户,圣阿蒙塞特莱,圣阿蒙塞特加斯,圣斯塔德, st.EndDate,st.TARGETELECHARGE,st.TargetGasCharge,ua.用户名 来自能源消耗ec ec.User=ua.id上的内部联接用户帐户ua st.customerID=ua.id上的内部联接energytarget st ec.ChargeID=c.id上的内部连接电荷c st.energyID=ec.id上的内部联接energytarget st 哪里
ec.Date=ec.Date>=st.StartDate和ec.Date
其中ec.Date=ec.Date>=st.StartDate和ec.Date@JayBlanchard Hi,哈哈,是的,我刚刚试过。不确定如何处理此问题您需要在
之间使用关键字
。您也在分组,这可能会产生问题。我正在工作,但如果还没有找到解决方案,我可以稍后再看。@Shawn干杯,伙计!我来试一试你的建议。为什么你两次加入同一张桌子,但标准不同?你能发布一些示例表内容和你想要得到的结果吗?啊,好的,那么我应该使用不同的别名吗?这能解决那个问题吗?好的,我把“st”改为“stt”。不幸的是,问题仍然存在。两次使用NergyTarge,哪一次用于选择日期?
INNER JOIN
    energytarget st ON
    st.energyID = ec.id"
    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