欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据库sql查询语句备忘

程序员文章站 2022-05-08 17:41:21
...
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查询语句备忘

版权声明:本文为博主原创文章,未经博主允许不得转载。