数据库sql查询语句备忘
2014-06-26 13:55
218 查看
SELECT menu. NAME FROM t_sys_auth_menu AS auth_menu LEFT JOIN t_sys_menu AS menu ON menu.id = auth_menu.menu_id WHERE auth_menu.auth_id IN ( SELECT DISTINCT auth_id FROM t_sys_role_auth AS role_auth WHERE role_auth.role_id IN ( SELECT role_id FROM t_sys_user_role AS user_role WHERE user_id = 3 ) )
数据库结构和简单测试数据留存
/* Navicat MySQL Data Transfer Source Server : BInhai Source Server Version : 50145 Source Host : 192.168.1.200:3306 Source Database : binhaifast2 Target Server Type : MYSQL Target Server Version : 50145 File Encoding : 65001 Date: 2014-06-26 13:58:57 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `t_sys_auth` -- ---------------------------- DROP TABLE IF EXISTS `t_sys_auth`; CREATE TABLE `t_sys_auth` ( `id` int(11) NOT NULL, `name` varchar(255) CHARACTER SET utf8 NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; -- ---------------------------- -- Records of t_sys_auth -- ---------------------------- INSERT INTO `t_sys_auth` VALUES ('1', '查看成绩'); INSERT INTO `t_sys_auth` VALUES ('2', '打印成绩'); INSERT INTO `t_sys_auth` VALUES ('3', '录入成绩'); INSERT INTO `t_sys_auth` VALUES ('4', '添加学生'); INSERT INTO `t_sys_auth` VALUES ('5', '修改成绩'); INSERT INTO `t_sys_auth` VALUES ('6', '删除学生成绩'); INSERT INTO `t_sys_auth` VALUES ('7', '查看学生信息'); INSERT INTO `t_sys_auth` VALUES ('8', '修改学生信息'); INSERT INTO `t_sys_auth` VALUES ('9', '添加学生信息'); INSERT INTO `t_sys_auth` VALUES ('10', '查看通知'); INSERT INTO `t_sys_auth` VALUES ('11', '修改通知'); INSERT INTO `t_sys_auth` VALUES ('12', '添加通知'); INSERT INTO `t_sys_auth` VALUES ('13', '删除通知'); -- ---------------------------- -- Table structure for `t_sys_auth_menu` -- ---------------------------- DROP TABLE IF EXISTS `t_sys_auth_menu`; CREATE TABLE `t_sys_auth_menu` ( `auth_id` int(11) NOT NULL, `menu_id` int(11) NOT NULL, PRIMARY KEY (`auth_id`,`menu_id`), KEY `index_relation_auth` (`auth_id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=gbk; -- ---------------------------- -- Records of t_sys_auth_menu -- ---------------------------- INSERT INTO `t_sys_auth_menu` VALUES ('1', '1'); INSERT INTO `t_sys_auth_menu` VALUES ('1', '2'); INSERT INTO `t_sys_auth_menu` VALUES ('1', '3'); INSERT INTO `t_sys_auth_menu` VALUES ('4', '1'); INSERT INTO `t_sys_auth_menu` VALUES ('4', '2'); INSERT INTO `t_sys_auth_menu` VALUES ('4', '3'); INSERT INTO `t_sys_auth_menu` VALUES ('4', '4'); -- ---------------------------- -- Table structure for `t_sys_menu` -- ---------------------------- DROP TABLE IF EXISTS `t_sys_menu`; CREATE TABLE `t_sys_menu` ( `id` int(11) NOT NULL, `name` varchar(255) CHARACTER SET utf8 NOT NULL, `parent_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; -- ---------------------------- -- Records of t_sys_menu -- ---------------------------- INSERT INTO `t_sys_menu` VALUES ('1', '当月报表', null); INSERT INTO `t_sys_menu` VALUES ('2', '当月盈利', null); INSERT INTO `t_sys_menu` VALUES ('3', '当月负债', null); INSERT INTO `t_sys_menu` VALUES ('4', '总公司合计', null); -- ---------------------------- -- Table structure for `t_sys_role` -- ---------------------------- DROP TABLE IF EXISTS `t_sys_role`; CREATE TABLE `t_sys_role` ( `role_id` int(11) NOT NULL, `role_name` varchar(255) CHARACTER SET utf8 NOT NULL, PRIMARY KEY (`role_id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; -- ---------------------------- -- Records of t_sys_role -- ---------------------------- INSERT INTO `t_sys_role` VALUES ('1', '学生'); INSERT INTO `t_sys_role` VALUES ('2', '老师'); INSERT INTO `t_sys_role` VALUES ('3', '教务处'); INSERT INTO `t_sys_role` VALUES ('4', '毕业生'); INSERT INTO `t_sys_role` VALUES ('5', '研究生'); -- ---------------------------- -- Table structure for `t_sys_role_auth` -- ---------------------------- DROP TABLE IF EXISTS `t_sys_role_auth`; CREATE TABLE `t_sys_role_auth` ( `role_id` int(11) NOT NULL, `auth_id` int(11) NOT NULL, PRIMARY KEY (`role_id`,`auth_id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; -- ---------------------------- -- Records of t_sys_role_auth -- ---------------------------- INSERT INTO `t_sys_role_auth` VALUES ('1', '1'); INSERT INTO `t_sys_role_auth` VALUES ('1', '2'); INSERT INTO `t_sys_role_auth` VALUES ('3', '1'); INSERT INTO `t_sys_role_auth` VALUES ('3', '2'); INSERT INTO `t_sys_role_auth` VALUES ('3', '3'); INSERT INTO `t_sys_role_auth` VALUES ('3', '4'); -- ---------------------------- -- Table structure for `t_sys_user` -- ---------------------------- DROP TABLE IF EXISTS `t_sys_user`; CREATE TABLE `t_sys_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(100) CHARACTER SET utf8 NOT NULL, `password` varchar(50) CHARACTER SET utf8 NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk; -- ---------------------------- -- Records of t_sys_user -- ---------------------------- INSERT INTO `t_sys_user` VALUES ('1', '用户A', '111111'); INSERT INTO `t_sys_user` VALUES ('2', '用户B', '222222'); INSERT INTO `t_sys_user` VALUES ('3', '用户C', '333333'); -- ---------------------------- -- Table structure for `t_sys_user_role` -- ---------------------------- DROP TABLE IF EXISTS `t_sys_user_role`; CREATE TABLE `t_sys_user_role` ( `user_id` int(11) NOT NULL, `role_id` int(11) NOT NULL, PRIMARY KEY (`user_id`,`role_id`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk; -- ---------------------------- -- Records of t_sys_user_role -- ---------------------------- INSERT INTO `t_sys_user_role` VALUES ('1', '1'); INSERT INTO `t_sys_user_role` VALUES ('1', '2'); INSERT INTO `t_sys_user_role` VALUES ('3', '1'); INSERT INTO `t_sys_user_role` VALUES ('3', '2'); INSERT INTO `t_sys_user_role` VALUES ('3', '3');
万能LEFT JOIN 上场:
SELECT
*
FROM
t_sys_menu AS menu
LEFT JOIN t_sys_auth_menu AS auth_menu ON menu.id = auth_menu.menu_id
LEFT JOIN t_sys_role_auth AS role_auth ON auth_menu.auth_id = role_auth.auth_id
LEFT JOIN t_sys_user_role AS user_role ON user_role.role_id =role_auth.role_id
WHERE user_role.user_id = 3;
相关文章推荐
- SQL Server中查询所有数据库、所有表、字段的Sql语句
- 关于数据库统计查询的sql语句
- ACCESS:跨数据库查询的SQL语句
- 【转贴】不同数据库查询前几条记录的SQL语句
- ACCESS:跨数据库查询的SQL语句
- ACCESS:跨数据库查询的SQL语句[转]
- SQL语句,查询数据库中间连续的记录
- ACCESS:跨数据库查询的SQL语句
- ACCESS:跨数据库查询的SQL语句
- ACCESS:跨数据库查询的SQL语句
- 数据库知识—SQL查询语句精华使用简要
- 数据库查询速度优化 2 优化SQL语句方法
- 数据库查询速度优化 2 优化SQL语句方法
- ACCESS:跨数据库查询的SQL语句
- ACCESS:跨数据库查询的SQL语句
- 数据库知识-SQL查询语句精华使用简要
- 查询数据库表和字段sql语句
- 整理出的数据库中几个常用sql查询语句
- ACCESS:跨数据库查询的SQL语句
- ACCESS:跨数据库查询的SQL语句