简体   繁体   English

如果ID匹配,则内部联接追加

[英]Inner join append if ID match

Table Person: 表人:

ID, NAME, AGE, DESCRIPTION, STATUS 身份证,姓名,年龄,描述,状态

Values: 值:

1, Casey, 18, A fresh grad, Probation
2, Wedy, 25, Middle career, Approve
3, Stacy, 33, A working professional, Confirmed
4, Steve, 45, Senior Management, Confirmed
5, Goat, 55, Retire, Rejected

Table Permission: 表格权限:

PermID, TYPE, VALUE PermID,TYPE,VALUE

Values: 值:

1, normaluser, can view training 1 and 2
1, probationuser, can view training 3 part 1
2, normaluser, can view training 1 and 2
2, traininguser, can view training 3
2, extradinoaryuser, Wendy can view full training
3, extradinoaryuser, Stacy can view full training
3, superuser, Stacy also can edit full training
3, supertrainer, can create full training

Trying to achieve: 试图实现:

  1. Display full Person table data with 'status' = Approved or 'status' = Confirmed 显示“状态” =已批准或“状态” =已确认的完整人员表数据

Output - display Person row id 2,3,4 输出-显示人员行ID 2,3,4

  1. If Match Person ID with Permission PermID, get 'type' = extradinoaryuser or 'type' = superuser, ignore the rest type, merge with Person table data. 如果将Person ID与Permission PermID匹配,则获取'type'= extradinoaryuser或'type'= superuser,忽略其余类型,并与Person表数据合并。

Output - append Permission 'type' and 'value' Person row id 2,3 输出-追加权限“类型”和“值”人员行ID 2,3

  1. If entry have both superuser and extradinoaryuser type, duplicate the entry 如果条目同时具有超级用户和Extradinoaryuser类型,请复制该条目

Output - Person id row 3 is duplicated as both superuser and extradinoaryuser 输出-Person id第3行被复制为超级用户和extradinoaryuser

Desired results: 所需结果:

2, Wedy, 25, Middle career, Approve, extradinoaryuser, Wendy can view full training<br/>
3, Stacy, 33, A working professional, Confirmed, extradinoaryuser, Stacy can view full training<br/>
3, Stacy, 33, A working professional, Confirmed, superuser, Stacy also can edit full training<br/>
4, Steve, 45, Senior Management, Confirmed

Sql commands(Not working): SQL命令(不起作用):

select Person.id, Person.name, Person.age, Person.description, Person.status 
from Person  
inner join Permission ON Person.id = Permission.PermID 
where Person.status like “Approve”  
   or Person.status like “Confirmed” 
  AND (permission.name = 'superuser' or permission.name = ‘extradinoaryuser’); 

Use proper Quotation Marks( '' ) for string variables : 对字符串变量使用正确的引号( '' ):

select p.id, p.name, p.age, p.description, p.status 
  from Person p inner join Permission m ON ( p.id = m.PermID )
 where p.status like 'Approve' or p.status like 'Confirmed'
   and (m.name = 'superuser' or m.name = 'extradinoaryuser');

PS Make sure about the (lower/upper)cases of string values in your tables. PS确保表中的字符串值(大写/小写)。

the issue is use proper quotation marks ("") and permission.name column not exists. 问题是使用正确的引号(“”),permission.name列不存在。 use permission.type instead 改用Permissions.type

Build schema: 构建模式:

create table Person
(ID int primary key Auto_Increment
, `NAME` varchar(25)
, AGE int
, DESCRIPTION varchar(255)
, STATUS varchar(25) );

insert into person values 
( 1, "Casey", 18, "A fresh grad", "Probation" ), 
(2, "Wedy", 25, "Middle career", "Approve"),
(3, "Stacy", 33, "A working professional", "Confirmed"), 
(4, "Steve", 45, "Senior Management", "Confirmed"), 
(5, "Goat", 55, "Retire", "Rejected");

create table Permission 
(PermID int
, `TYPE` varchar(25)
, `VALUE` varchar(255)
);

insert into Permission values
( 1, "normaluser","can view training 1 and 2"), 
(1, "probation user", "can view training 3 part 1"), 
(2, "normaluser", "can view training 1 and 2"), 
(2, "traininguser", "can view training 3"), 
(2, "extradinoaryuser", "Wendy can view full training"), 
(3, "extradinoaryuser", "Stacy can view full training"),
(3, "superuser", "Stacy also can edit full training"), 
(3,"supertrainer", "can create full training");

Correct your query as 将查询更正为

First get all user which have a status as Approve or Confirmed and then filter the user permission type as superuser or extradinoaryuser or null for display the user has no permission 首先获取状态为“批准”或“已确认”的所有用户,然后将用户权限类型筛选为超级用户或Extradinoaryuser或null以显示该用户没有权限

select * 
  from ( select Person.id
          , Person.name
          , Person.age
          , Person.description
          , Person.status
          , Permission.type
          , Permission.value
      from Person 
      left 
      join Permission 
        ON Person.id = Permission.PermID
     where (Person.status like "Approve" or Person.status like "Confirmed")
        ) t 
 where (t.type = 'superuser' or t.type = "extradinoaryuser" or t.type IS NULL) ;

see result in http://sqlfiddle.com/#!9/f5a6be/27 http://sqlfiddle.com/#!9/f5a6be/27中查看结果

The problem seems to be your quotes. 问题似乎是您的报价。 However, I would recommend two things for the query: 但是,我建议查询两件事:

  • Table aliases. 表别名。
  • IN

The query would look like: 查询如下所示:

select p.id, p.name, p.age, p.description, p.status 
from Person p inner join
     Permission pn
     ON p.id = pn.PermID 
where p.status in ('Approve', 'Confirmed') and
      pn.name in ('superuser', 'extradinoaryuser'); 

In SQL, the standard string delimiter are single quotes. 在SQL中,标准字符串定界符是单引号。 These should be used for defining string and date constants (and not for other purposes). 这些应用于定义字符串和日期常量(而不用于其他目的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM