MySQL-如何连接这些表
我有以下表格: 公司MySQL-如何连接这些表,mysql,join,Mysql,Join,我有以下表格: 公司 id | name | domain 1 | sample1 | one.sample.lan 2 | sample2 | two.sample.lan 权限 id | moduleid | permid 5 | 12 | 2 如果在权限表中将模块的权限设置为公司,则将设置模块id(此处为12)和公司id(此处为2)。权限的正常id自动递增。如果将删除权限,则将从权限中删除整行 我想从我的选择中获得以下信息: company.name | com
id | name | domain
1 | sample1 | one.sample.lan
2 | sample2 | two.sample.lan
权限
id | moduleid | permid
5 | 12 | 2
如果在权限表中将模块的权限设置为公司,则将设置模块id(此处为12)和公司id(此处为2)。权限的正常id自动递增。如果将删除权限,则将从权限中删除整行
我想从我的选择中获得以下信息:
company.name | company.domain | selected
我想得到所有的公司,不管他们是否被设置好。如果它们(在“权限”表中)被选择为1或0,则显示该信息
比如:
SELECT company.name, company.domain
FROM company LEFT JOIN permissions ON (company.id = permissions.permid)
WHERE permissions.moduleid = 12;
提前感谢。使用distinct仅获取每个公司一次,并使用if或case结构确定权限表中是否存在该公司:
SELECT DISTINCT company.name, company.domain, if(permissions.permid is null, 0, 1) as selected
FROM company LEFT JOIN permissions ON (company.id = permissions.permid)
WHERE permissions.moduleid = 12;
更新:如果要检查哪些公司有权访问特定模块,请将where条件移动到连接条件中:
SELECT DISTINCT company.name, company.domain, if(permissions.permid is null, 0, 1) as selected
FROM company LEFT JOIN permissions ON (company.id = permissions.permid) and permissions.moduleid = 12;
对不起,我不明白,你是说你想要一个精选的箱子吗
CASE permissions.moduleId为空时,则为0,否则为1
。。。类似这样的事情?@DiogodeGóesZanetti这也是我解释问题的方式:)if子句工作正常,但现在我只在moduleid=12时获得许可。正因为如此,我只得到了comany命名的样本2。我也想在if语句中使用0获取样本1。。。你知道怎么做吗?为这个问题添加更多信息。请查看此主题,并提供有关选定案例示例的信息:@JoshuaH。然后从语句中删除where条件。@Diogo de Góes Zanetti问题在于where子句。我可以在where子句中也做一个例子吗?类似于…WHERE IF(moduleid存在,permissions.moduleid=12,permissions.moduleid为null)@Shadow如果我删除WHERE语句,我将为所有模块获得一个1 in IF子句,而不是我想看到的模块。。。