I'm writing a query to determine if a user is an administrator on my website or not.
我正在编写一个查询来确定用户是否是我网站上的管理员。
I can't quite figure out how to display "True" or "False" after my initial select.
在我最初的选择之后,我不知道如何显示“True”或“False”。
select count(*) as IsAdmin
from UserRoles
where UserID = '1' and RoleID = 1
IF count is != 0 => True
ELSE False
My IF is also not working, probably because I'm writing something incorrectly. RoleID=1
means they are administrators.
我的IF也不起作用,可能是因为我写错了东西。RoleID=1表示他们是管理员。
2 个解决方案
#1
3
Use exists in your IF and then proceed with your logic / what you want to return
使用存在于IF中,然后继续您的逻辑/您想要返回的内容
IF(exists(select 1 as IsAdmin from UserRoles where UserID='1' and RoleID = 1))
SELECT 'IS ADMIN'
ELSE
SELECT 'IS NOT ADMIN'
If in another scenario you were trying to check for a certain number, you can use it as such:
如果在另一个场景中,您试图检查某个数字,您可以这样使用:
IF(select count(*) as IsAdmin from UserRoles where UserID='1' and RoleID = 1) > 0
SELECT 'IS ADMIN'
ELSE
SELECT 'IS NOT ADMIN'
#2
1
select (case when UserID = '1' and RoleID = 1 then 1 else 0 end) as IsAdmin
from UserRoles
#1
3
Use exists in your IF and then proceed with your logic / what you want to return
使用存在于IF中,然后继续您的逻辑/您想要返回的内容
IF(exists(select 1 as IsAdmin from UserRoles where UserID='1' and RoleID = 1))
SELECT 'IS ADMIN'
ELSE
SELECT 'IS NOT ADMIN'
If in another scenario you were trying to check for a certain number, you can use it as such:
如果在另一个场景中,您试图检查某个数字,您可以这样使用:
IF(select count(*) as IsAdmin from UserRoles where UserID='1' and RoleID = 1) > 0
SELECT 'IS ADMIN'
ELSE
SELECT 'IS NOT ADMIN'
#2
1
select (case when UserID = '1' and RoleID = 1 then 1 else 0 end) as IsAdmin
from UserRoles