Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对每个表进行验证的SQL左联接_Sql_Postgresql_Request_Left Join - Fatal编程技术网

对每个表进行验证的SQL左联接

对每个表进行验证的SQL左联接,sql,postgresql,request,left-join,Sql,Postgresql,Request,Left Join,首先,我的SQL技能非常差。 这是我的桌子: Message : id | title | arrayusers 1 |Title1 | a:2:{i:1;i:1;i:5;i:5;} 2 |Title2 | a:2:{i:1;i:1;i:5;i:5;} 3 |Title3 | a:2:{i:1;i:1;i:5;i:5;} 4 |Title4 | a:2:{i:1;i:1;i:5;i:5;} 5 |Title5 | a:2:{i:1;i:1;i:5;i:5;} 6 |Title6 |

首先,我的SQL技能非常差。 这是我的桌子:

Message :
id | title | arrayusers
1  |Title1 | a:2:{i:1;i:1;i:5;i:5;}
2  |Title2 | a:2:{i:1;i:1;i:5;i:5;}
3  |Title3 | a:2:{i:1;i:1;i:5;i:5;}
4  |Title4 | a:2:{i:1;i:1;i:5;i:5;}
5  |Title5 | a:2:{i:1;i:1;i:5;i:5;}
6  |Title6 | a:2:{i:1;i:1;i:5;i:5;}

Read :
id | status | userid | message_id
1  |   0    |   5    |    1
2  |   0    |   1    |    2
3  |   0    |   5    |    2
4  |   0    |   1    |    3
5  |   0    |   5    |    4
6  |   1    |   1    |    5
7  |   1    |   5    |    5
7  |   1    |   5    |    6
我使用userid 1进行测试:

我的目标是从
消息
中获取所有行,其中我的用户1位于
阵列用户
中,具有一个类似的(i:1;i:1;)

用户1不在表
中读取
(userid),其
状态等于0。如果另一个用户(5)而不是我(用户1)在
Read
表中,我想查看我的
消息

有了上面的数据,我想从
Message
表返回id 1/4

我用Left join启动了一个请求,但我的请求隐藏了行id 1和4,因为此表中有另一个用户具有此id

SELECT * FROM message as m
LEFT JOIN read as r ON m.id = r.message_id
WHERE m.arrayusers LIKE '%i:1;i:1;%'
AND r.id IS NULL
希望你能理解我的要求。 有人能帮我调试我的请求吗


谢谢

我会使用
不存在
不在
查找
读取
表中的状态:

select * 
from message
where arrayusers like '%i:1;i:1;%'
and id not in
(
  select message_id
  from read
  where userid = 1
  and status = 0
);
SELECT m.*
FROM message as m
LEFT JOIN read as r ON m.id = r.message_id and r.userid = 1 and r.status = 0
WHERE m.arrayusers LIKE '%i:1;i:1;%'
AND r.id IS NULL;
至于您自己的查询:您只是缺少
read
表中的条件:

select * 
from message
where arrayusers like '%i:1;i:1;%'
and id not in
(
  select message_id
  from read
  where userid = 1
  and status = 0
);
SELECT m.*
FROM message as m
LEFT JOIN read as r ON m.id = r.message_id and r.userid = 1 and r.status = 0
WHERE m.arrayusers LIKE '%i:1;i:1;%'
AND r.id IS NULL;

和r.id为NULL的用法是什么?请删除此条件并检查:

从消息中选择*作为m
左连接在m.id=r.message\u id上读取为r
其中,像“%i:1;i:1;%'
r.id为空

使用
和r.id为NULL
是为了创建反联接(即,查找在
读取中不匹配的所有
消息
记录)。这是一种在DBMS中使用的方法,存在直接的
不存在
/
不在
中的问题。我不喜欢表中的数组。你为什么不把这张桌子分开?而
a:2:{i:1;i:1;i:5;i:5;}
意味着用户1发送了消息,而用户5收到了消息?但是为什么在表
read
中有消息2用户1的记录呢?表示用户1没有阅读他们自己的消息?我相信您会从一个更为关系化的数据模型中受益。您说您希望看到消息1和消息4。但这应该是1、4、5和6,因为在
read
表中没有一条用户1状态为0的记录。到目前为止,你自相矛盾。请澄清。
{i:1;i:1;i:5;i:5;}
意思是,消息已到达用户1和5的目的地。数据模型不是我的,我不能更改它,我也不喜欢数组字段。。。是的,我在结果上犯了一个错误,我想要1/4/5/6,你说得对。我对你的要求有个问题。除了已经获取的数据外,如何获取
读取的
表数据?因为我在表
read
的每个字段中都得到了
NULL
值,但有些字段不是空的。感谢您正在查找不存在
read
记录的邮件,那么您如何期望任何
read
值?这毫无意义。