mysql - SQL中用where子句實(shí)現(xiàn)的表連接怎么用inner join實(shí)現(xiàn)?
問(wèn)題描述
RBAC(Role-Based Access Control,基于角色的訪(fǎng)問(wèn)控制)用5張表實(shí)現(xiàn):
用戶(hù)表 rbac_user(id, name)角色表 rbac_role(id, name)操作表 rbac_oper(id, name)用戶(hù)對(duì)應(yīng)的角色表 rbac_relation_user_role(user_id, role_id)角色對(duì)應(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)行表連接獲取編號(hào)為1的用戶(hù)具有的操作權(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)在的問(wèn)題是用inner join獲得相同的結(jié)果該怎么寫(xiě)?我這樣寫(xiě),會(huì)提示錯(cuò)誤:
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錯(cuò)誤: #1066 - Not unique table/alias: ’rbac_role’
問(wèn)題解答
回答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
等價(jià)于下面的inner join(縮寫(xiě)join)寫(xiě)法
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不能寫(xiě)作:join rbac_role on rbac_relation_role_oper.role_id = rbac_role.id因?yàn)橐WC所有需要關(guān)聯(lián)的表都被join,同一個(gè)表不能join兩次.
相關(guān)文章:
1. 對(duì)mysql某個(gè)字段監(jiān)控的功能2. javascript - windows下如何使用babel,遇到了困惑3. html5 - ElementUI table中el-table-column怎么設(shè)置百分比顯示。4. javascript - js中向下取整5. python - 為什么正常輸出中文沒(méi)有亂碼,zip函數(shù)之后出現(xiàn)中文編程unicode編碼的問(wèn)題,我是遍歷輸出的啊。6. html - vue項(xiàng)目中用到了elementUI問(wèn)題7. showpassword里的this 是什么意思?代表哪個(gè)元素8. java enum 變量不能再case里面重復(fù)定義?9. JavaScript事件10. javascript - table列過(guò)多,有什么插件可以提供列排序和選擇顯示列的功能
