0

在 MySQL 中,我有两个表。

任务

ID

姓名

清单

ID

会员ID

任务ID

在 php 页面中,我创建了 sql 选择字符串,我有一个 php 变量,它的members_id值是登录人的成员 ID。如何获取tasks表的所有记录,并向其中添加一个新列调用completed,值是表中true是否存在与members id php变量相同的记录,并且它的值与表的相同,如果不存在?checklistmember_idtasks_ididtasksfalse

谢谢

4

1 回答 1

3

你想要的是一个left outer join

select t.*, (cl.id is not null) as IsCompleted
from tasks t left outer join
     checklist cl
     on t.id = cl.taskid and
        cl.memberid = <your member id goes here>

当表中有记录时,表达式(cl.id is not null)返回,否则返回。truechecklistfalse

于 2013-06-08T18:12:07.867 回答