表人:
ID,NAME,AGE,DESCRIPTION,STATUS
值:
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
表格许可:
PermID,TYPE,VALUE
值:
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
努力实现:
输出 - 显示Person row id 2,3,4
输出 - 附加权限'输入'和'价值'人行id 2,3
输出 - 人员ID行3作为超级用户和extradinoaryuser
重复期望的结果:
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命令(不工作):
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’);
答案 0 :(得分:0)
对字符串变量使用正确的引号(''
):
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');
P.S。确保表中字符串值的(下/上)情况。
答案 1 :(得分:0)
问题是使用正确的引号(“”)和permission.name列不存在。使用permission.type代替
构建架构:
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");
将您的查询更正为
首先获取状态为Approve或Confirmed的所有用户,然后将用户权限类型过滤为超级用户或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) ;
答案 2 :(得分:0)
问题似乎是你的报价。但是,我建议查询两件事:
IN
查询看起来像:
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');
在SQL中,标准字符串分隔符是单引号。这些应该用于定义字符串和日期常量(而不是用于其他目的)。