如何将多个select查询调用批量处理为单个mysql查询或过程



我有这个函数,使用他们的userid检查2个用户之间的友谊状态,它工作得很好。

function checkFriendShipBetweenUsers(user1Id, user2Id) {
var checkFriendShipBetweenUsersQuery = "SELECT status  FROM friends WHERE (user1Id=? AND user2Id =?) OR (user1Id=? AND user2Id =?)"
var queryParameterList = [user1Id, user2Id, user2Id, user1Id]
}

我有一个案例,我需要检查一个用户和其他3个用户之间的友谊状态。我可以调用上述函数3次,一个为其他用户获得所需的结果,但我想使用单个查询或使用mysql过程进行单个db调用。

function checkFriendShipBetweenUsers(user1Id, userIdList) {
var checkFriendShipBetweenUsersQuery = ""
var queryParameterList = []
}

所以这个查询/过程调用应该返回3个整数,表示user1与userIdList中用户的友谊状态。

下面是一个例子:db-fiddle.com/f/p5RP61V3AcawRgJcogeXey/1

给定user1Id: 'a8t57h6p8n2efden' and

userIdList: ['typ3vg6xb1vt7nw2', 'cy6mqqyykpldc2j1g5vm5cqsi6x1dgrl', '0bw87kprb97pes1crom8ceodi07r2kd0']

我怎么写这样的查询或过程?

DEMO

-- source data
CREATE TABLE test (
id INT,
user1Id VARCHAR(100),
user2Id VARCHAR(100),
status INT
);
INSERT INTO test (id,user1Id,user2Id,status) VALUES 
(1,'a8t57h6p8n2efden','typ3vg6xb1vt7nw2',0),
(2,'cy6mqqyykpldc2j1g5vm5cqsi6x1dgrl','a8t57h6p8n2efden',1),
(3,'0bw87kprb97pes1crom8ceodi07r2kd0','a8t57h6p8n2efden',2),
(4,'a8t57h6p8n2efden','ap21wzbew0bprt5t',0);
SELECT * FROM test;
00

最新更新