dongren9966 2013-06-08 18:02
浏览 22
已采纳

如何在MySQL中使用join

In MySQL, I have two tables.

tasks

id

name

checklist

id

member_id

tasks_id

In the php page, I create the sql select string, I have a php variable that has a members_id value which is the member id of the person logged in. How can I get all the records of the tasks table, and add a new column to it called completed, and the value is true if there exists a record in the checklist table that has the member_id the same as the members id php variable and it's tasks_id value is the same as the id of the tasks table, and false if it doesn't exist?

Thanks

  • 写回答

1条回答 默认 最新

  • dongtuo3795 2013-06-08 18:12
    关注

    What you want is a 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>
    

    The expression (cl.id is not null) returns true when there is a record in the checklist table and false otherwise.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退