我必须加入三张桌子。
1表是交易,下表是商店,第3是天气。
我想从所有这三个中获取数据,但是如果是天气表> 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'
,但它不起作用。
答案 0 :(得分:0)
试试这个:
WHERE transaction.transaction_date> ='2015-05-25'AND transaction.transaction_date< '2015年5月26日'
这将在一整天(午夜到午夜时间)返回带有时间戳的行 - 暗示时间。
如果您有SQLServer 2014,则可以使用DATE(transaction.transaction_date)。
答案 1 :(得分:0)
这是否符合您的要求
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)