Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用三个表连接sql查询_Php_Mysql_Sql Server_Transactions - Fatal编程技术网

Php 用三个表连接sql查询

Php 用三个表连接sql查询,php,mysql,sql-server,transactions,Php,Mysql,Sql Server,Transactions,我必须加入三张桌子 1个表是交易,下一个表是商店,第三个表是天气 我想从这三个字段中提取数据,但如果是weather table>id、tamp_c、datetime列名 查询是:- select HOUR(transaction.time) as Hour , TRUNCATE(sum(transaction.total),2) as Total_Sales, shops.gstIncludedSales as GST_Inc_Sales, s

我必须加入三张桌子

1个表是交易,下一个表是商店,第三个表是天气

我想从这三个字段中提取数据,但如果是weather table>id、tamp_c、datetime列名

查询是:-

 select HOUR(transaction.time) as Hour ,
        TRUNCATE(sum(transaction.total),2) as Total_Sales,
        shops.gstIncludedSales as GST_Inc_Sales,
        shops.gstFreeSales as GST_Free,weather.temp_c 
        from transaction,shops,weather 
        where transaction.shopid=shops.id and transaction.shopid=7
        and transaction.transaction_date ='2015-05-25' 
        group by hour 
        ORDER BY hour DESC
问题是,我想在
where
子句中使用
weather.datetime
表分隔特定日期,例如
time(datetime)='2015-05-25'
,但它不起作用。

尝试以下操作:

其中transaction.transaction_date>='2015-05-25'和transaction.transaction_date<'2015-05-26'

这将返回一整天中带有时间戳的行(午夜到午夜-暗示时间)


如果您有SQLServer2014,您可以使用日期(transaction.transaction\u DATE)。

这是您想要的吗

SELECT HOUR(transaction.time) as Hour ,
   TRUNCATE(sum(transaction.total),2) as Total_Sales,
   shops.gstIncludedSales as GST_Inc_Sales,
   shops.gstFreeSales as GST_Free,weather.temp_c 
FROM transaction
JOIN shops ON transaction.shopid=shops.id
JOIN weather ON DATE(transaction.transaction_date) = DATE(weather.datetime)
WHERE DATE(transaction.transaction_date)='2015-05-25' AND transaction.shopid=7
GROUP by hour 
ORDER BY hour DESC

(假设您使用的是MySQL)

您是否尝试过
date(weather.datetime)='2015-05-25'
?weather似乎没有连接到其他任何一个表的条件。您得到了什么结果,您期望得到什么?感谢您的回复Paul,我想获取此表,同时选择*FROM
weather
WHERE date(datetime)='2015-05-25'和shop_id=7或者选择WHERE transaction.transaction_date介于'2015-05-25'和'2015-05-26'之间或日期函数(假设为MySQL)您还将获得2015-05-26 00:00:00的记录以及中间语句。Date函数已经出现在我的答案中:)你是对的-刚刚注意到我测试的数据没有第二天的任何数据-所以看起来是有效的。Date函数-我在评论这在MySQL和SQLServer2014Thanks中都可用,但我的问题是Date列也在transaction表中,weather表也在datetime列中,所以我想要weather表中带有where子句Date(weather.datetime)的记录='2015-05-25'所以两个单独的日期都用在不同的表上。提前感谢我不知道你的意思-你能展示这三个表的模式并给出一些预期结果的例子吗。weather=>id shop\u id temp\u c datetime shops=>id gstIncludedSales GST\u Inc\u Sales,gstFreeSales transacion=>id transaction total Times选择小时(transaction.time)作为小时,截断(sum(transaction.total),2)作为总销售额,shops.GST包括销售为GST_Inc_Sales,shops.GST零售为GST_免费交易,shops where transaction.shopid=shops.id和transaction.shopid=7和transaction.transaction_date='2015-05-25'按小时分组订单按小时描述我想用SELECT*from weather where date(datetime)加入此查询结果='2015-05-25'和shop_id=7提前感谢。在您显示表schema transaction.shopid缺失的注释中,这是一个疏忽。