MySQL multi-table association query

Multi-table query

Create several tables with relationships between them.

user table
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for sys_admin
-- ----------------------------
DROP TABLE IF EXISTS `sys_admin`;
CREATE TABLE `sys_admin`  (
  `user_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '登录账号',
  `password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '登录密码',
  `datetime` datetime(0) NULL DEFAULT NULL COMMENT '添加时间\r\n',
  `username` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '管理员姓名',
  `salt` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '盐',
  PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of sys_admin
-- ----------------------------
INSERT INTO `sys_admin` VALUES ('123456', 'be5b9922e3392dc42153f9c9870b7e78', '2022-06-05 12:12:59', '三禾', 'Idq29*0uAY');
INSERT INTO `sys_admin` VALUES ('263561', '8df16f91f1139510d0266791ce78acde', '2022-05-31 23:26:52', '胡歌', 'Qa#8uA!KwC');
INSERT INTO `sys_admin` VALUES ('茶凡', 'c7e9768beca397fb4db195b2204b01ec', '2022-06-08 20:33:44', 'Matrix', 'KmXcY)jNOO');

SET FOREIGN_KEY_CHECKS = 1;

User role table
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_user_role
-- ----------------------------
DROP TABLE IF EXISTS `t_user_role`;
CREATE TABLE `t_user_role`  (
  `id` int(6) NOT NULL,
  `userid` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `roleid` int(6) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of t_user_role
-- ----------------------------
INSERT INTO `t_user_role` VALUES (1, '263561', 1);
INSERT INTO `t_user_role` VALUES (2, '123456', 2);
INSERT INTO `t_user_role` VALUES (3, '茶凡', 1);

SET FOREIGN_KEY_CHECKS = 1;

character sheet
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_role
-- ----------------------------
DROP TABLE IF EXISTS `t_role`;
CREATE TABLE `t_role`  (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `name` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of t_role
-- ----------------------------
INSERT INTO `t_role` VALUES (1, 'admin');
INSERT INTO `t_role` VALUES (2, 'user');
INSERT INTO `t_role` VALUES (3, 'product');

SET FOREIGN_KEY_CHECKS = 1;

Permissions table
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_perms
-- ----------------------------
DROP TABLE IF EXISTS `t_perms`;
CREATE TABLE `t_perms`  (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `name` varchar(80) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `url` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of t_perms
-- ----------------------------
INSERT INTO `t_perms` VALUES (1, 'user:*:*', NULL);
INSERT INTO `t_perms` VALUES (2, 'product:*:01', NULL);
INSERT INTO `t_perms` VALUES (3, 'order:*:*', NULL);

SET FOREIGN_KEY_CHECKS = 1;
Role permission table
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_role_perms
-- ----------------------------
DROP TABLE IF EXISTS `t_role_perms`;
CREATE TABLE `t_role_perms`  (
  `id` int(6) NOT NULL,
  `roleid` int(6) NULL DEFAULT NULL,
  `permsid` int(6) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of t_role_perms
-- ----------------------------
INSERT INTO `t_role_perms` VALUES (1, 1, 1);
INSERT INTO `t_role_perms` VALUES (2, 1, 2);
INSERT INTO `t_role_perms` VALUES (3, 2, 1);
INSERT INTO `t_role_perms` VALUES (4, 3, 2);
INSERT INTO `t_role_perms` VALUES (5, 1, 3);

SET FOREIGN_KEY_CHECKS = 1;
relationship between tables

MySQL connection

Use joins to query data from multiple tables.

SELECT
	table1.COLUMN,
	table2.COLUMN 
FROM
	table1,
	table2 
WHERE
	table1.column1 = table2.column2;
  • Write the join condition in the WHERE clause.

  • When there are identical columns in the table, add the table name prefix before the column name.

Related query user table and user role table
SELECT s.user_id,s.username,t.roleid FROM `sys_admin` s,t_user_role t WHERE s.user_id = t.userid;

--------------------------------------------------
结果:
123456		三禾	    2
15681026356	胡歌	     1
茶凡		  Matrix	1
--------------------------------------------------
Related query user table, user role table and role table
SELECT
	s.user_id,
	s.username,
	tur.roleid,
	tr.name
FROM
	`sys_admin` s
	JOIN t_user_role tur ON s.user_id = tur.userid
	JOIN t_role tr ON tur.roleid = tr.id;
	
--------------------------------------------------
结果:
15681026356	胡歌	    1	admin
茶凡	      Matrix	1	admin
123456	    三禾	    2	user
--------------------------------------------------
Multiple table join

To join n tables, at least n-1 join conditions are required. For example: to join three tables, at least two join conditions are required.

Cartesian set

When two tables are associated, the columns of the left table and the columns of the right table are expressed in the form of Cartesian product.

outer join
Left outer join: left join

When two tables are related, all the left table is retained. If the right table cannot be related, null is used to indicate it.

select * from t1 left join t2 on t1.id = t2.id

Insert image description here

Right outer join: right join

When two tables are related, the right table is retained entirely. If the left table is not related, null is used to indicate it.

select * from t1 right join t2 on t1.id = t2.id

Insert image description here

Inner join: inner join

The two tables are associated and the intersection records in the two tables are retained.

select * from t1 inner join t2 on t1.id = t2.id;

Insert image description here

Other connections

Insert image description here

Blog reference (3 messages) MySQL table connection method_Zhayi's blog-CSDN blog_mysql table connection method

Blog reference: Shang Silicon Valley mysql tutorial

Guess you like

Origin blog.csdn.net/weixin_45833112/article/details/125288330