表结构如下
CREATE TABLE chat (id bigint(20) unsigned NOT NULL AUTO_INCREMENT,user1_id bigint(20) unsigned NOT NULL COMMENT '聊天发起者',user2_id bigint(20) unsigned NOT NULL COMMENT '聊天对象',anonymous tinyint(2) unsigned NOT NULL,created_at int(11) unsigned NOT NULL,updated_at int(11) unsigned NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY user1_user2_anon (user1_id,user2_id,anonymous)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
需求是如何取出这张表里每两个人对应的记录,如A-B,B-A,A-C,A-D结果应为A-B,A-C,A-D;怎么查啊?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你是要计算有多少组通话吧?
select distinct
case user1_id<user2_id
when 1 then concat(user1_id,'-',user2_id)
when 0 then concat(user2_id,'-',user1_id)
end
as uids
from chat;
你的最后一句是什么意思??如果是简单的查询某条记录,直接使用where就可以啊。。