mysql四种连接测试

# 创建并选择数据库
CREATE DATABASE shop;
USE shop;

# 创建商品分类表
CREATE TABLE sh_goods_category (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '分类id',
parent_id INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '上级分类id',
name VARCHAR(100) NOT NULL DEFAULT '' COMMENT '名称',
sort INT NOT NULL DEFAULT 0 COMMENT '排序',
is_show TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否显示',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time DATETIME DEFAULT NULL COMMENT '更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

# 添加测试数据
INSERT INTO sh_goods_category (id, parent_id, name) VALUES
(1, 0, '办公'), (2, 1, '耗材'), (3, 2, '文具'),
(4, 0, '电子产品'), (5, 4, '通讯'), (6, 5, '手机'),
(7, 4, '影音'), (8, 7, '音箱'), (9, 7, '耳机'),
(10, 4, '电脑'), (11, 10, '台式电脑'), (12, 10, '笔记本'),
(13, 0, '服装'), (14, 13, '男装'), (15, 14, '风衣'), (16, 14, '毛衣');

# 创建商品表
CREATE TABLE sh_goods (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '商品id',
category_id INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '分类id',
spu_id INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'SPU id',
sn VARCHAR(20) NOT NULL DEFAULT '' COMMENT '编号',
name VARCHAR(120) NOT NULL DEFAULT '' COMMENT '名称',
keyword VARCHAR(255) NOT NULL DEFAULT '' COMMENT '关键词',
picture VARCHAR(255) NOT NULL DEFAULT '' COMMENT '图片',
tips VARCHAR(255) NOT NULL DEFAULT '' COMMENT '提示',
description VARCHAR(255) NOT NULL DEFAULT '' COMMENT '描述',
content TEXT NOT NULL COMMENT '详情',
price DECIMAL(10, 2) UNSIGNED NOT NULL DEFAULT 0 COMMENT '价格',
stock INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '库存',
score DECIMAL(3, 2) UNSIGNED NOT NULL DEFAULT 0 COMMENT '评分',
is_on_sale TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否上架',
is_del TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否删除',
is_free_shipping TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否包邮',
sell_count INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '销量计数',
comment_count INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '评论计数',
on_sale_time DATETIME DEFAULT NULL COMMENT '上架时间',
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
update_time DATETIME DEFAULT NULL COMMENT '更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

# 添加测试数据
INSERT INTO sh_goods (id, category_id, name, keyword, content, price,
stock, score, comment_count) VALUES
(1, 3, '2B铅笔', '文具', '考试专用', 0.5, 500, 4.9, 40000),
(2, 3, '钢笔', '文具', '练字必不可少', 15, 300, 3.9, 500),
(3, 3, '碳素笔', '文具', '平时使用', 1, 500, 5, 98000),
(4, 12, '超薄笔记本', '电子产品', '轻小便携', 5999, 0, 2.5, 200),
(5, 6, '智能手机', '电子产品', '人人必备', 1999, 0, 5, 98000),
(6, 8, '桌面音箱', '电子产品', '扩音装备', 69, 750, 4.5, 1000),
(7, 9, '头戴耳机', '电子产品', '独享个人世界', 109, 0, 3.9, 500),
(8, 10, '办公电脑', '电子产品', '适合办公', 2000, 0, 4.8, 6000),
(9, 15, '收腰风衣', '服装', '春节潮流单品', 299, 0, 4.9, 40000),
(10, 16, '薄毛衣', '服装', '居家旅行必备', 48, 0, 4.8, 98000);


SELECT c.id cid,c.name cname, g.id gid, g.name ganme
from sh_goods_category as c
CROSS JOIN sh_goods as g;
SELECT g.id gid, g.name gname, c.id cid,c.name cname
from sh_goods g join sh_goods_category c
on g.category_id = c.id;
SELECT g.id gid,g.name gname, c.id cid, c.name cname
from sh_goods g left join sh_goods_category c
on g.category_id = c.id and g.score = 5;
from sh_goods g right join sh_goods_category c
on g.category_id = c.id and g.score = 6;

posted @ 2022-12-05 20:44  晚点喝雪碧  阅读(450)  评论(0)    收藏  举报