av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

mysql - SQL中用where子句實(shí)現(xiàn)的表連接怎么用inner join實(shí)現(xiàn)?

瀏覽:109日期:2022-06-17 14:03:40

問題描述

RBAC(Role-Based Access Control,基于角色的訪問控制)用5張表實(shí)現(xiàn):

用戶表 rbac_user(id, name)角色表 rbac_role(id, name)操作表 rbac_oper(id, name)用戶對應(yīng)的角色表 rbac_relation_user_role(user_id, role_id)角色對應(yīng)的操作表 rbac_relation_role_oper(role_id, oper_id, value)

建表并插入數(shù)據(jù):

CREATE TABLE `rbac_user` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT ’’, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;CREATE TABLE `rbac_role` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT ’’, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;CREATE TABLE `rbac_oper` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT ’’, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;CREATE TABLE `rbac_relation_user_role` ( `user_id` int(10) unsigned NOT NULL, `role_id` int(10) unsigned NOT NULL, UNIQUE KEY (`user_id`, `role_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `rbac_relation_role_oper` ( `role_id` int(10) unsigned NOT NULL, `oper_id` int(10) unsigned NOT NULL, `value` int(10) unsigned NOT NULL, UNIQUE KEY (`role_id`, `oper_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO rbac_user VALUES(1,’張三豐’);INSERT INTO rbac_role VALUES(1,’管理員’);INSERT INTO rbac_oper VALUES(1,’foo’);INSERT INTO rbac_oper VALUES(2,’bar’);INSERT INTO rbac_relation_user_role VALUES(1,1);INSERT INTO rbac_relation_role_oper VALUES(1,1,1);INSERT INTO rbac_relation_role_oper VALUES(1,2,0);

用where子句進(jìn)行表連接獲取編號為1的用戶具有的操作權(quán)限:

select rbac_oper.name, rbac_relation_role_oper.valuefrom rbac_user, rbac_role, rbac_oper, rbac_relation_user_role, rbac_relation_role_operwhere rbac_relation_user_role.user_id = rbac_user.id and rbac_relation_user_role.role_id = rbac_role.idand rbac_relation_role_oper.role_id = rbac_role.idand rbac_relation_role_oper.oper_id = rbac_oper.idand rbac_user.id = 1結(jié)果:name valuefoo 1bar 0

現(xiàn)在的問題是用inner join獲得相同的結(jié)果該怎么寫?我這樣寫,會提示錯誤:

select * from rbac_relation_user_role inner join rbac_user on rbac_relation_user_role.user_id = rbac_user.id inner join rbac_role on rbac_relation_user_role.role_id = rbac_role.id inner join rbac_role on rbac_relation_role_oper.role_id = rbac_role.id inner join rbac_oper on rbac_relation_role_oper.oper_id = rbac_oper.idwhere rbac_user.id = 1錯誤: #1066 - Not unique table/alias: ’rbac_role’

問題解答

回答1:

select rbac_oper.name, rbac_relation_role_oper.valuefrom rbac_user, rbac_role, rbac_oper, rbac_relation_user_role, rbac_relation_role_operwhere rbac_relation_user_role.user_id = rbac_user.id and rbac_relation_user_role.role_id = rbac_role.idand rbac_relation_role_oper.role_id = rbac_role.idand rbac_relation_role_oper.oper_id = rbac_oper.idand rbac_user.id = 1

等價于下面的inner join(縮寫join)寫法

select rbac_oper.name, rbac_relation_role_oper.value from rbac_relation_user_role join rbac_user on rbac_relation_user_role.user_id = rbac_user.idjoin rbac_role on rbac_relation_user_role.role_id = rbac_role.idjoin rbac_relation_role_oper on rbac_relation_role_oper.role_id = rbac_role.idjoin rbac_oper on rbac_relation_role_oper.oper_id = rbac_oper.idwhere rbac_user.id = 1

注意其中:join rbac_relation_role_oper on rbac_relation_role_oper.role_id = rbac_role.id不能寫作:join rbac_role on rbac_relation_role_oper.role_id = rbac_role.id因?yàn)橐WC所有需要關(guān)聯(lián)的表都被join,同一個表不能join兩次.

相關(guān)文章:
主站蜘蛛池模板: 啪啪网页| 天天操天天干天天透 | 久久久久久久久久毛片 | 中文字幕日韩在线观看 | 日本成人福利视频 | 99国产精品久久久 | 午夜ww| 亚洲欧美另类在线观看 | 久久国产精品一区 | 中文字幕一区二区三区四区五区 | 欧美在线观看一区 | 亚洲视频在线观看免费 | 女朋友的闺蜜3韩国三级 | 涩涩视频在线播放 | 日韩一区二区免费视频 | 欧美日韩最新 | 国产区精品在线观看 | 亚洲第一色av | 成人欧美日韩一区二区三区 | 免费精品 | 国产精品久久久久aaaa | 日韩色视频 | 午夜专区| 6996成人影院网在线播放 | 福利视频网站 | 四虎首页| 91在线视频网址 | 国产成人久久精品 | 91在线免费观看网站 | 99国产精品久久久久久久 | 免费在线一区二区 | 欧美性大战久久久久久久蜜臀 | 久久久久久高潮国产精品视 | 91影库| 久久网一区二区三区 | 日本成人片在线观看 | 国产精品一区二区av | 色黄网站 | 国产第二页 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | av资源中文在线 |