Mysql 从两个表中选择相同的值对

Mysql 从两个表中选择相同的值对,mysql,qsqlquery,Mysql,Qsqlquery,我的数据库中有4个MySQL表: 用户uid,名称 商店店名 评论评论ID、文本、用户ID、店铺ID、费率、日期 TransactionHistory transactionId、userId、shopId、金额、日期 用户将他们的评论写到评论表中的商店。某些商店的用户付款历史记录保存在TransactionHistory表中 我需要选择一段时间内所有用户的评论,如果用户在同一时间段内在该商店付款 选择userId、shopId、rate、text、date from Review,其中日期介于

我的数据库中有4个MySQL表:

用户uid,名称 商店店名 评论评论ID、文本、用户ID、店铺ID、费率、日期 TransactionHistory transactionId、userId、shopId、金额、日期 用户将他们的评论写到评论表中的商店。某些商店的用户付款历史记录保存在TransactionHistory表中

我需要选择一段时间内所有用户的评论,如果用户在同一时间段内在该商店付款

选择userId、shopId、rate、text、date from Review,其中日期介于2019年1月1日和2019年2月1日之间,其中shopId位于Select distinctshopId from Shops中

从TransactionHistory中选择userId、shopId和日期,其中日期介于2019年1月1日和2019年2月1日之间


所以我有两个结果集,一些记录具有相同的pair userId,shopId-这就是我想要得到的:来自1个SQL请求的所有记录,其中pair userId,shopId出现在2个SQL查询中。

如果我理解正确,您所需要的就是这样一个join语句:

   SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
                 ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)
这是资源

在你的情况下,On语句内的内容将是你想要平等的内容

Select userId, shopId, rate, text, date from Review r join TransactionHistory th on (r.userId == th.userId and r.shopId == th.shopId) where r.date between "01.01.2019" and "01.02.2019" where r.shopId in (select distinct(shopId) from Shops)

如果我理解正确,您所需要的就是这样一个join语句:

   SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
                 ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)
这是资源

在你的情况下,On语句内的内容将是你想要平等的内容

Select userId, shopId, rate, text, date from Review r join TransactionHistory th on (r.userId == th.userId and r.shopId == th.shopId) where r.date between "01.01.2019" and "01.02.2019" where r.shopId in (select distinct(shopId) from Shops)