如果另一个表中存在id,则将列选择为true/false [英] select column as true / false if id is exists in another table

查看:106
本文介绍了如果另一个表中存在id,则将列选择为true/false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子,一张桌子用于会员,另一张桌子用于他们的服务. 这些是MySQL 5.6服务器上的InnoDB表.

I have 2 tables, one for members and another one for their services. Those are InnoDB tables on MySQL 5.6 server.

成员表:

id     |       name       |   phone
----------------------------------------
  1       Daniel             123456789
  2       Liam               123456789
  3       Lucas              123456789

服务表:

 MID    |    profile    |     lastSeen
----------------------------------------
  1       2                  2014-08-13 14:23:23
  3       1                  2014-08-12 15:29:11

我尝试实现此结果:

 id     | name      | services
---------------------------------
  1       Daniel      true
  2       Liam        false
  3       Lucas       true

因此,如果在服务表中存在用户ID,则服务列将为true或false.

So if the user ID is exists in services table, the column services will be true or false otherwise.

我尝试对JOIN和子查询执行此操作没有成功,所以我需要您的帮助;)

I tried to do it with JOINs and Sub-Queries without success, so I need your help ;)

推荐答案

使用LEFT JOIN服务表,请尝试以下查询

use LEFT JOIN Services table, Try this query

SELECT members.id, members.name, 
       IF(services.mid IS NULL, FALSE, TRUE) as services
FROM members
LEFT JOIN services ON (members.id = services.mid)

这篇关于如果另一个表中存在id,则将列选择为true/false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆