简体   繁体   English

仅从组中的第一条记录中选择值

[英]Select value from only the first record in a group

I have a database that contains transaction records. 我有一个包含交易记录的数据库。 Each record belongs to a chain of transactions, and these have an TCID (Transaction Chain ID) that they share. 每个记录都属于一个交易链,并且每个交易都有一个共享的TCID(交易链ID)。 Each transactions contains a sender and a receiver. 每个交易都包含一个发送者和一个接收者。 What I need to do is check to see if the final receiving user in a chain is ever the same as the first sender in another chain. 我需要做的是检查一个链中的最终接收用户是否与另一个链中的第一个发送者相同。

Currently, my MySQL query returns records where the final receiver is in any transaction of another chain, not just the first. 目前,我的MySQL查询返回的记录是最终接收者在另一个链的任何事务中的位置,而不仅仅是第一个。 I need to limit this strictly to final receiver and first sender. 我需要将此严格限制为最终接收者和第一个发送者。

I tried using group by, order by, and limit 1, but these are applied after the query finds some records. 我尝试使用分组依据,排序依据和限制1,但在查询找到一些记录后才应用它们。 Here's the query I had tried so far: 这是我到目前为止尝试过的查询:

SELECT TCID FROM transactions WHERE senderUID = '$receiverUID' GROUP BY TCID LIMIT 1

Anyone know of a way that I can search only the senderUID of the first (lowest TID) record in a group (TCID)? 有人知道我只能搜索组(TCID)中第一个(最低TID)记录的senderUID的方法吗?

Thanks for your help! 谢谢你的帮助!

This should hopefully get you in the right direction - 这有望使您朝正确的方向前进-

//Gets rows where senderUID is the first (lowest TID) record in group
SELECT a.* 
FROM test a 
WHERE a.senderUID = '$receiverUID'
AND NOT EXISTS (select * from test where TCID = a.TCID and id < a.id and senderUID != '$receiverUID')
GROUP BY TCID

UNION

//Gets rows where senderUID is the same as the last receiverUID of TCID
SELECT b.* 
FROM test b
WHERE b.receiverUID = '$receiverUID'
AND NOT EXISTS (select * from test where TCID = b.TCID and id > b.id and receiverUID != '$receiverUID')
GROUP BY TCID

So as simple example I have the following table- 因此,作为一个简单的示例,我有下表:

表数据

And so if I set $receiverUID = 1, I get 2 rows where senderUID was the first in a TCID group (1,9), and 3 rows where that senderUID was the receiverUID in a TCID group (4,7,8) 因此,如果我设置$ receiverUID = 1,则会得到2行,其中senderUID是TCID组(1,9)中的第一个,而3行则是senderUID是TCID组(4,7,8)中的ReceiverUID。

senderUID / receiverUID的TCID组为1

And you could add a LIMIT 1 if you wanted to only get 1 row where the senderUID was the first in a TCID group (1)/(4,7,8) 如果只想获得1行,其中senderUID是TCID组中的第一个(1)/(4,7,8),则可以添加LIMIT 1

SELECT a.* 
FROM test a 
WHERE a.senderUID = '$receiverUID'
AND NOT EXISTS (select * from test where TCID = a.TCID and id < a.id and senderUID != '$receiverUID')
GROUP BY TCID LIMIT 1

senderUID / receiverUID的TCID组为1,仅限制第一行senderUID

Same idea if I set $receiverUID = 2 (3,11)/(6,10) 如果我将$ receiverUID设置为2(3,11)/(6,10)

senderUID / receiverUID的TCID组为2

and with LIMIT 1 (3)/(6,10) 并具有LIMIT 1 (3)/(6,10)

senderUID / receiverUID的TCID组为2,仅限制第一行senderUID

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

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