PHP查询带有日期BETWEEN和多个子句的2个MySQL表

时间:2019-06-23 17:20:15

标签: php mysql

我有两个表名为 收据和物料_收据_标记

我需要查询表单和特定用户输入日期之间的这些mysql表,以及要从两个表中搜索特定用户(techID)的所有日期的条件。到目前为止,我很难过,甚至认为我都没有。这是我到目前为止所拥有的

"SELECT Receipts.TechID, Receipts.TotalPrice, Receipts.TotalProfit,
Receipts.Date, Materials_Receipts_Markup.Markup, 
Materials_Receipts_Markup.DateOfReceipt,
Materials_Receipts_Markup.TechID".

"FROM Receipts, Materials_Receipts_Markup".

"WHERE Receipts.Date BETWEEN '$newStartDate' AND '$newEndDate' AND WHERE
Materials_Receipts_Markup.DateOfReceipt BETWEEN '$newStartDate' AND
'$newEndDate'".

"AND Receipts.TechID AND Materials_Receipts_Markup.TechID ='$FormTechID' ";

$newStartDate$newEndDate是日期,例如2019-06-23

我的SQL技能还没有达到这么复杂的水平。任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

我认为您想使用OR来搜索每个分开的表,并使用TechID运算符来获得两个=

WHERE 
(
    (Receipts.Date BETWEEN '$newStartDate' AND '$newEndDate') 
    OR
    (Materials_Receipts_Markup.DateOfReceipt BETWEEN '$newStartDate' AND '$newEndDate') 
)
AND Receipts.TechID = Materials_Receipts_Markup.TechID AND Receipts.TechID ='$FormTechID'

答案 1 :(得分:0)

除非两个表之间有另一个链接字段,否则它们之间似乎没有任何逻辑连接,只是数据是由相同的TechID输入的。您可能需要UNION代替JOIN

{3=40, 4=40}

如果还有其他字段(例如ReceiptID),则需要在该字段上联接表。