简体   繁体   English

MySQL: select 具有特定字段值的多行

[英]MySQL: select multiple rows which has specific field value

I'm having a users_conversations table contains a conversation_id and a user_id to tell which users are in the same conversation.我有一个 users_conversations 表,其中包含一个 conversation_id 和一个 user_id 来告诉哪些用户在同一个对话中。 I want to find any conversation_id which has user_id 76 and user_id 146 (which are ids 28, 31 and 32. These ids are not unknown to me. I just have both users ids) I tried我想找到任何具有 user_id 76 和 user_id 146 的对话 ID(它们是 id 28、31 和 32。这些 id 对我来说并不陌生。我只有两个用户 id)我试过

select * from users_conversations where user_id = 146 or user_id = 76 GROUP BY conversation_id

but I get但我明白了

#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 
'forums.users_conversations.user_id' which is not functionally dependent on columns in GROUP BY clause; 
this is incompatible with sql_mode=only_full_group_by

How can I do that?我怎样才能做到这一点?

在此处输入图像描述

select * from users_conversations
where conversations_id in (select conversations_id from user_conversations where user_id=76) 
AND conversations_id in (select conversations_id from user_conversations where user_id=146)

You can filter the table for these users, group by conversation_id and set the condition in the having clause:您可以过滤这些用户的表, group by conversation_id并在having子句中设置条件:

select conversation_id
from users_conversations
where user_id in (76, 146)
group by conversation_id
having count(distinct user_id) = 2

You may omit distinct if there is no case of duplicate users for each conversation_id .如果每个conversation_id没有重复用户的情况,您可以省略distinct

Did you mean instead:你的意思是不是:

select * from users_conversations where user_id = 146 or user_id = 76

Why do you want to use group?为什么要使用组?

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

相关问题 MySQL根据特定的字段值和行数选择多行 - MySQL SELECT multiple rows based on specific field value and number of rows Mysql 多行,其值在 2 或 rows 中具有特定值,并且具有另一个状态 - Mysql multiple rows with a value that has a specific value in 2 or rows and which has another status MySQL:如何选择字段中值最小的行? - MySQL: How to select rows which have the lowest value in a field? MySQL - 选择所有不存在的行或等于value的字段 - MySQL - Select all rows which not exist or a field equal to value MySQL SELECT行可响应其中一列中的特定MULTIPLE值 - MySQL SELECT rows which respond to specific MULTIPLE values in one of the columns MySQL - 选择 json_column 具有特定值的行 - MySQL - Select rows where json_column has specific value MySQL选择值在其他行中具有多个值 - MySQL select where value has multiple values in other rows 选择特定字段的值大于同一字段最大值一半的行 - Select rows where specific field has value more than half of max value of the same field 如何选择在一个字段中具有不同值并在MySQL中按另一个字段排序的行? - How to select rows that has distinct value in one field and sorted by another field in MySQL? MySQL-如何选择具有字段最大值的行 - MySQL - How to select rows with max value of a field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM