Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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 同一表中的其他列如何使用chceck is id_Sql_Database_Mariadb_Mariadb 10.3 - Fatal编程技术网

Sql 同一表中的其他列如何使用chceck is id

Sql 同一表中的其他列如何使用chceck is id,sql,database,mariadb,mariadb-10.3,Sql,Database,Mariadb,Mariadb 10.3,我使用的是MariaDB 10.3,我有如下表格: post_id post_content post_user_id post_shared 1 Test1 1 0 2 Test2 2 0 3 Test2 1 2 post_shared=0表示这是原始帖子,不共享 我正在寻找一种方法,以了解该帖子是否已被来自post_user_id的特定用户共

我使用的是MariaDB 10.3,我有如下表格:

post_id post_content post_user_id post_shared
1       Test1        1             0
2       Test2        2             0
3       Test2        1             2
post_shared=0表示这是原始帖子,不共享

我正在寻找一种方法,以了解该帖子是否已被来自post_user_id的特定用户共享。示例输出如下:

post_id isShared                     ( post_user_id 1)
1       0 (false)
2       1 (true)
3       0 (false)
我尝试对同一个表进行左连接,并使用if条件进行检查,但代码返回了错误的值

Thx all以获取帮助:

使用corealted子查询

select t1.* from table_name t1
where exists( select 1 from table_name t2 where t1.post_user_id=t2.post_user_id
                          and post_shared<>0)

可以使用相关子查询或左联接添加布尔标志。如果没有重复项:

select t.*, (ts.post_id is not null) as isShared
from t left join
     ts
     on ts.post_shared = t.post_id and
        ts.post_user_id = 1;
作为一个相关子查询,如下所示:

select t.*,
       (exists (select 1
                from ts
                where ts.post_shared = t.post_id and
                      ts.post_user_id = 1
               )
       ) as isShared
存在着:

select
  t.post_id,
  case when exists (
      select 1 from tablename
      where post_id <> t.post_id
      and post_shared = t.post_id
    ) then 1 
    else 0
  end isShared                     
from tablename t
使用左连接

SELECT t1.post_id, IF(t2.post_id IS NULL, FALSE, TRUE)
FROM Test as t1
LEFT JOIN Test as t2 ON (t1.post_id = t2.post_shared and t2.post_user_id = 1)
order by t1.post_id;

使用案例和基于文本的解决方案:


Thx@Gordon Linoff:这项工作:我无法理解子查询是如何工作的:你不能理解这个问题;您的输出如下所示:post_id1未共享、post_id2未共享、post_id3已共享。
SELECT *,
CASE

    WHEN post_shared > 0 THEN 'This story has been shared' 
    ELSE 'This story has not been shared' 

END AS share_status
FROM Test