我有以下问题:
select transactions.timestamp, products.productHash
from transactions
inner join transactionItems on transactionItems.transactionHash = transactions.transactionHash
inner join products on products.productHash = transactionItems.productHash
where userKey = '8eb6a8a9-4f1a-4402-89ab-4d1fbffd1284'
and (products.type = 'applier' and products.gender = 'f')
and products.additionalData->'$."skintone"' in ('-1', -1)
and exists
(
select 1 from materialAppliers
where materialAppliers.productHash = products.productHash
and materialAppliers.applierType = 'skin'
)
group by transactionItems.productHash
给我以下结果:
+---------------------+----------------------------------------+
| timestamp | productHash |
+---------------------+----------------------------------------+
| 2014-12-30 23:37:32 | cHJvZF81YWZhZmJkY2JiMzQ0OC4yMTg5MTY3OQ |
| 2014-11-30 19:17:47 | cHJvZF81YWZiZDVlOTQ3ZjM5Mi44NTc2Mjc0MQ |
+---------------------+----------------------------------------+
我必须加入其中 materialAppliers
哪里 productHash
正在匹配:
+----------------------------------------+------------------------------------------+-------+
| productHash | applierHash | asset |
+----------------------------------------+------------------------------------------+-------+
| cHJvZF81YWZhZmJkY2JiMzQ0OC4yMTg5MTY3OQ | bWF0QXBwXzVhZmFmYmRjY2E1ZTExLjE3NDA3NjYx | val1 |
| cHJvZF81YWZhZmJkY2JiMzQ0OC4yMTg5MTY3OQ | bWF0QXBwXzVhZmFmYmRjY2Q1MmE0LjI3NTA4Nzcx | val2 |
| cHJvZF81YWZiZDVlOTQ3ZjM5Mi44NTc2Mjc0MQ | bWF0QXBwXzVhZmJkNWU5NDdmNWQ0LjU1MzQ1NDg5 | val3 |
| cHJvZF81YWZiZDVlOTQ3ZjM5Mi44NTc2Mjc0MQ | bWF0QXBwXzVhZmJkNWU5NmY2MTA0LjQyOTkxNzY5 | val4 |
| cHJvZF81YWZiZDVlOTQ3ZjM5Mi44NTc2Mjc0MQ | bWF0QXBwXzVhZmJkNWU5OTZlNGU3LjcxNTI1MDY1 | val5 |
+----------------------------------------+------------------------------------------+-------+
所以每个 materialApplier
有自己的行,包括 timestamp
并取自上一个查询; applierHash
以及 asset
取自 materialAppliers
:
+---------------------+----------------------------------------+------------------------------------------+-------+
| timestamp | productHash | applierHash | asset |
+---------------------+----------------------------------------+------------------------------------------+-------+
| 2014-12-30 23:37:32 | cHJvZF81YWZhZmJkY2JiMzQ0OC4yMTg5MTY3OQ | bWF0QXBwXzVhZmFmYmRjY2E1ZTExLjE3NDA3NjYx | val1 |
| 2014-12-30 23:37:32 | cHJvZF81YWZhZmJkY2JiMzQ0OC4yMTg5MTY3OQ | bWF0QXBwXzVhZmFmYmRjY2Q1MmE0LjI3NTA4Nzcx | val2 |
| 2014-11-30 19:17:47 | cHJvZF81YWZiZDVlOTQ3ZjM5Mi44NTc2Mjc0MQ | bWF0QXBwXzVhZmJkNWU5NDdmNWQ0LjU1MzQ1NDg5 | val3 |
| 2014-11-30 19:17:47 | cHJvZF81YWZiZDVlOTQ3ZjM5Mi44NTc2Mjc0MQ | bWF0QXBwXzVhZmJkNWU5NmY2MTA0LjQyOTkxNzY5 | val4 |
| 2014-11-30 19:17:47 | cHJvZF81YWZiZDVlOTQ3ZjM5Mi44NTc2Mjc0MQ | bWF0QXBwXzVhZmJkNWU5OTZlNGU3LjcxNTI1MDY1 | val5 |
+---------------------+----------------------------------------+------------------------------------------+-------+
我怎样才能做到这一点?
3条答案
按热度按时间bfnvny8b1#
加入吧
materialAppliers
你的问题。但是列列表中有一列,没有
GROUP BY
你没有在上面应用任何聚合函数。尽管mysql在较低的版本或某些设置中接受这一点,但这并不是一件好事。我建议你把它修好。使用例如。max()
获取最新的时间戳。slhcrj9b2#
我将尝试对整个第一个查询使用别名,然后将最后一个连接添加到materialappliers表中
4ioopgfo3#
看起来很直截了当:使用materialappliers添加一个内部连接,然后再向select添加两个选定值。。。我错过了什么吗?
你应该能够摆脱存在,并简单地添加
and ma.applierType = 'skin'
相反。我之所以说应该,是因为我不完全理解appliertype和producthash之间的关系,但看起来应该可以。例如: