简体   繁体   English

用三个表连接sql查询

[英]Joining sql query with three tables

I have to join three tables.我必须加入三张桌子。

1 table is transaction , next table is shops, 3rd is weather. 1 表是交易,下一张表是商店,第 3 表是天气。

I want to fetch data from all of these three ,but in case of weather table > id,tamp_c,datetime column name .我想从所有这三个中获取数据,但在天气表 > id,tamp_c,datetime 列名的情况下。

The query is:-查询是:-

 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

The problem is that I want to apply a where clause with weather.datetime table separation for particular date like time(datetime)='2015-05-25' , but it's not working.问题是我想对像time(datetime)='2015-05-25'这样的特定日期应用带有weather.datetime表分离的where子句,但它不起作用。

Try this:尝试这个:

WHERE transaction.transaction_date >= '2015-05-25' AND transaction.transaction_date < '2015-05-26' WHERE transaction.transaction_date >= '2015-05-25' AND transaction.transaction_date < '2015-05-26'

That will return rows with timestamp during the whole day (midnight until midnight - time is implied).这将在全天(午夜到午夜 - 隐含时间)返回带有时间戳的行。

If you have SQLServer 2014 you canm use DATE(transaction.transaction_date).如果您有 SQLServer 2014,则可以使用 DATE(transaction.transaction_date)。

Does this do what you want这是否做你想做的

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

(Assuming you are using MySQL) (假设您使用的是 MySQL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM