Mysql多层子查询示例代码(收藏夹案例)

Mysql多层子查询示例代码(收藏夹案例),第1张

Mysql多层子查询示例代码(收藏夹案例)

用户表

create table if not exists tb_book (
    book_id char(10) not null comment 'book主键',
    book_name char(30) not null comment '书名',
    book_type char(10) not null comment '类别',
    book_brief varchar(500) not null comment '简介',
    book_online_time timestamp not null comment '上线时间',
    book_author char(20) not null comment '作者',
    book_grade int(2)  not null comment '评分',
    book_word_number int(5)  not null comment '售量',
    book_small_picture char(100) not null comment '书小图',
    book_big_picture char(100)  comment '书大图',
    primary key (book_id)
)
replace into tb_user(user_id,user_name,email,phone_number,balance,briefs,birthday,grade,password,avatar,signature) values
('324123125','llx','[email protected]','17665621006',0,'风一样的少年',CURDATE(),25,'123456','default.png','我就是我,不一样的烟火'),
('324223125','user2345','[email protected]','13722211122',0,'风一样的少年',CURDATE(),25,'123456','default.png','不顾别人感受是自私,太顾别人感受是自虐。'),
('324423125','ling','[email protected]','13351234112',0,'风一样的少年',CURDATE(),25,'123456','default.png','年龄永远不是衡量一个人的刻度,只有责任的叠加才会让人逐渐成长。'),
('324623125','xz','[email protected]','15534562345',0,'风一样的少年',CURDATE(),25,'123456','default.png','昨天再好,也走不回去;明天再难,也要抬脚继续。');
select * from tb_user

图书表

create table if not exists tb_book (
    book_id char(10) not null comment 'book主键',
    book_name char(30) not null comment '书名',
    book_type char(10) not null comment '类别',
    book_brief varchar(500) not null comment '简介',
    book_online_time timestamp not null comment '上线时间',
    book_author char(20) not null comment '作者',
    book_grade int(2)  not null comment '评分',
    book_word_number int(5)  not null comment '售量',
    book_small_picture char(100) not null comment '书小图',
    book_big_picture char(100)  comment '书大图',
    primary key (book_id)
)
insert into tb_book(book_id, book_name, book_type, book_brief, book_online_time, book_author, book_grade,
                    book_word_number, book_small_picture, book_big_picture)
values ('0000000001', '天降鬼才', '玄幻',
        '我不会奇门遁甲,也不懂风水八卦,但江湖人都称我绝世鬼才。为什么?因为我脑子有坑!装满来至新世纪的现代学识!讲道理,其实我是个很纯洁的斯文人,不管你们信不信,反正我是信了。', now(), '武异', 10, 1,
        '0000000001.png', '0000000001.png'),
       ('0000000002', '暴君的穿越小邪后', '古代言情',
        '一名现代A帮千金,机缘巧合下穿越到大齐——一个历史上完全不存在的朝代,在这个波诡云谲的时代,看她如何见招拆招,并和那个冷面帝王谈一场千古绝恋。带着皇帝在现代吃喝玩乐,在古代又玩起了帝后和鸣,好不快活!',
        now(), '非墨千泷', 10, 1412342, '0000000002.png', '0000000002.png'),
       ('0000000003', '汗灵昭烈', '武侠仙侠',
        '打虎上山,行侠入世,一代英雄聚念而生。单臂挚天,双剑仗义,千年华夏因义流芳。武松是谁?他为何而来?他又将为谁而战?一代汉灵入世,沧海下暗流涌动,乾坤里谁是英雄!',
        now(), '千秋一梦', 10, 151241, '0000000003.png', '0000000003.png'),
       ('0000000004', '大明烟火', '历史军事', '朱元璋:“一群贪赃枉法之辈,朕要杀光他们。”韩度:“陛下息怒,与其杀了,不如废物利用,压榨干净他们的剩余价值才好。”朱元璋:......', now(),
        '阳光下写字', 10, 1123424, '0000000004.png', '0000000004.png'),
       ('0000000005', '重生八零甜妻有点狂', '都市言情',
        '眼睛一闭一睁,乐薇重回十六岁。白手起家打拼出的跨国集团没了,上亿身家也没了,只有家徒四壁和兜里的几毛钱,还有她的小学学历。重活一回,除了赚钱,她还要走进知识殿堂,成为学霸,走向巅峰,出任CEO,迎娶白富美!一身戎装又帅又飒的小柳同志疑惑:谁是白富美?又白又富又美的乐大CEO理直气壮:你。',
        now(), '林间雪', 10, 112341, '0000000005.png', '0000000005.png'),
       ('0000000006', '小道姑成仙记', '玄幻',
        '斩恶鬼,斗尸煞,收妖邪!杀魔人,寻仙草,炼仙丹!初出茅庐的郑小雅,为了救出师尊,重振师门,踏上了一条危险曲折,惊心动魄的道路一个又一一个不可思议的经历,在她的生命中演绎,。最后她发现,自己的命运原来一直都不掌握在自己的手中,且看她如何打破命运的枷锁,成就仙道之躯。',
        now(), '羊小乐', 10, 112342, '0000000006.png', '0000000006.png'),
       ('0000000007', '满级游戏宅在兽世惹桃花', '幻想时空',
        '游戏玩家顾晓黎因为系统bug携带满级技能穿越到游戏中的兽人副本,踏上漫漫归家之路,却被各色美男拐骗,只得留下,发展城邦,与兽夫共同对抗恶势力。甜宠玛丽苏。',
        now(), '无摆', 10, 15234, '0000000007.png', '0000000007.png')

收藏夹表

create table if not exists tb_collection(
    collection_id char(11) not null comment '收藏id',
    book_id char(10) not null comment 'book主键',
    user_id CHAR(16) NOT NULL COMMENT '用户id',
    collection_time timestamp not null comment '收藏时间',
    primary key (collection_id),
    constraint `collection_book` foreign key (`book_id`) references `tb_book`(`book_id`),
    constraint `collection_user` foreign key (`user_id`) references `tb_user`(`user_id`)
)
set foreign_key_checks = 0; # 暂时取消外键约束
insert into tb_collection(collection_id, book_id, user_id, collection_time)
values ('00000000001', '0000000001', '324123125', now()),
       ('00000000002', '0000000002', '324123125', now()),
       ('00000000003', '0000000003', '324123125', now()),
       ('00000000004', '0000000004', '324123125', now());
set foreign_key_checks = 1; # 恢复外键约束

查询 思路:从根据用户id查询收藏夹表里的book_id,再用拿到的book_id查询图书

select *
from tb_book
where tb_book.book_id
          in (select tb_collection.book_id
              from tb_collection
              where user_id = '324123125')

到此这篇关于Mysql多层子查询(收藏夹案例)的文章就介绍到这了,更多相关mysql多层子查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/2997130.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-26
下一篇 2022-09-26

发表评论

登录后才能评论

评论列表(0条)

保存