简体   繁体   English

通过左联接选择多个

[英]Select multiple with Left join

I have a chat table, where users can post message and it is stored in db and read by the intended recipient. 我有一个聊天表,用户可以在其中发布消息,该消息存储在db中,并由预期的收件人读取。

when i send normal chat, its no problem, but when i send multiple or broadcast, i get stock. 当我发送普通聊天时,没问题,但是当我发送多个或广播时,我会得到库存。

table users
id  |  alias
-----------
1   |  mark
2   |  joe

table chat 表聊天

id  |  uid  | msg   | cmd
-----------------------
1   |  1    | hi    | msg
2   |  2    | hello | msg
3   |  1,2  | msg   | brc <- broadcast

for reading normal one to one message 用于阅读正常的一对一消息

Select u.alias, c.id cid from chat c left join user u on u.id=c.uid 
where c.uid= 1

now for the broadcast, for a user with id 1, i tried this 现在是广播,对于ID为1的用户,我尝试了

Select u.alias, c.id cid from chat c left join user u on u.id=c.uid 
where id in IN (1,2)

for another user with id 2, 对于另一个ID为2的用户,

Select u.alias, c.id cid from chat c left join user u on u.id=c.uid 
where id in IN (1,2)

I get an empty result 我得到一个空结果

Lets normalize your tables: 让我们标准化表格:

table users 表用户

id  |  alias
-----------
1   |  mark
2   |  joe

table chat 表聊天

id  | msg   | cmd
-----------------------
1   | hi    | msg
2   | hello | msg
3   | msg   | brc <- broadcast

table chatrouting 表聊天

id  | userid| msgid
-----------------------
1   | 1     | 1
2   | 2     | 2
3   | 1     | 3
4   | 2     | 3

then you can get all mark's messages with: 那么您可以通过以下方式获取所有标记的消息:

SELECT c.msg
FROM chat c
LEFT JOIN chatrouting cr ON cr.msgid = c.id
LEFT JOIN user u ON u.id = cr.userid
WHERE u.alias = 'mark'

Try to use this: 尝试使用此:

Select u.alias, c.id cid from chat c 
left join user u on FIND_IN_SET(u.id,'1,2') 
where id in IN (1,2)

Because row looks like 3 | 因为行看起来像3 | 1,2 | 1,2 | msg | 味精| brc <- broadcast So uid in chat table it's not just int, it can have 2 or more integers separate comma. brc <-broadcast因此, chat表中的uid不仅是int,它还可以具有2个或多个整数分开的逗号。 If I right understand. 如果我正确理解。

The reason for this query to fail is that you're testing a single column to match your own concatenation of columns 1,2 . 该查询失败的原因是,您正在测试单个列以匹配您自己的列1,2的串联。 This is generally a bad idea because it's not inline with the paradigm of normalized relational databases. 这通常是一个坏主意,因为它不符合规范化关系数据库的范式。 Columns should have singular, atomic values. 列应具有奇异的原子值。 It's very easy to redesign your 'broadcast' so that you don't have this problem. 重新设计“广播”非常容易,这样就不会出现此问题。

Two other problems I notice: 我注意到另外两个问题:

Each of these queries should give an error pointing out that there is an "Ambiguously defined column", but apparantly MySQL accepts this. 这些查询中的每一个都应该给出一个错误,指出存在一个“歧义定义的列”,但是MySQL显然接受了这一点。 In any case, you should add a table alias to the id to specify which table you mean. 无论如何,您都应该在id添加表格别名,以指定您要表示的表格。

Also, there is no need at all to use a left join in this case. 同样,在这种情况下根本不需要使用左联接。 This would only make sense if it's possible for the id to not exist in the other table, which shouldn't be possible, because it should be guarded by a foreign key constraint. 只有在另一个表中可能不存在id的情况下,这才有意义,这应该是不可能的,因为应该由外键约束来保护它。

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

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