Mysql多層子查詢示例代碼(收藏夾案例)
更新時間:2022年03月31日 11:17:29 作者:修羅MIUI
這篇文章主要介紹了Mysql多層子查詢示例代碼,以收藏夾案例給大家詳細(xì)介紹,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
用戶表
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','1753552134@qq.com','17665621006',0,'風(fēng)一樣的少年',CURDATE(),25,'123456','default.png','我就是我,不一樣的煙火'),
('324223125','user2345','2341235@qq.com','13722211122',0,'風(fēng)一樣的少年',CURDATE(),25,'123456','default.png','不顧別人感受是自私,太顧別人感受是自虐。'),
('324423125','ling','234125@qq.com','13351234112',0,'風(fēng)一樣的少年',CURDATE(),25,'123456','default.png','年齡永遠(yuǎn)不是衡量一個人的刻度,只有責(zé)任的疊加才會讓人逐漸成長。'),
('324623125','xz','2341235@qq.com','15534562345',0,'風(fēng)一樣的少年',CURDATE(),25,'123456','default.png','昨天再好,也走不回去;明天再難,也要抬腳繼續(xù)。');
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', '天降鬼才', '玄幻',
'我不會奇門遁甲,也不懂風(fēng)水八卦,但江湖人都稱我絕世鬼才。為什么?因為我腦子有坑!裝滿來至新世紀(jì)的現(xiàn)代學(xué)識!講道理,其實我是個很純潔的斯文人,不管你們信不信,反正我是信了。', now(), '武異', 10, 1,
'0000000001.png', '0000000001.png'),
('0000000002', '暴君的穿越小邪后', '古代言情',
'一名現(xiàn)代A幫千金,機(jī)緣巧合下穿越到大齊——一個歷史上完全不存在的朝代,在這個波詭云譎的時代,看她如何見招拆招,并和那個冷面帝王談一場千古絕戀。帶著皇帝在現(xiàn)代吃喝玩樂,在古代又玩起了帝后和鳴,好不快活!',
now(), '非墨千瀧', 10, 1412342, '0000000002.png', '0000000002.png'),
('0000000003', '汗靈昭烈', '武俠仙俠',
'打虎上山,行俠入世,一代英雄聚念而生。單臂摯天,雙劍仗義,千年華夏因義流芳。武松是誰?他為何而來?他又將為誰而戰(zhàn)?一代漢靈入世,滄海下暗流涌動,乾坤里誰是英雄!',
now(), '千秋一夢', 10, 151241, '0000000003.png', '0000000003.png'),
('0000000004', '大明煙火', '歷史軍事', '朱元璋:“一群貪贓枉法之輩,朕要?dú)⒐馑麄儭!表n度:“陛下息怒,與其殺了,不如廢物利用,壓榨干凈他們的剩余價值才好。”朱元璋:......', now(),
'陽光下寫字', 10, 1123424, '0000000004.png', '0000000004.png'),
('0000000005', '重生八零甜妻有點(diǎn)狂', '都市言情',
'眼睛一閉一睜,樂薇重回十六歲。白手起家打拼出的跨國集團(tuán)沒了,上億身家也沒了,只有家徒四壁和兜里的幾毛錢,還有她的小學(xué)學(xué)歷。重活一回,除了賺錢,她還要走進(jìn)知識殿堂,成為學(xué)霸,走向巔峰,出任CEO,迎娶白富美!一身戎裝又帥又颯的小柳同志疑惑:誰是白富美?又白又富又美的樂大CEO理直氣壯:你。',
now(), '林間雪', 10, 112341, '0000000005.png', '0000000005.png'),
('0000000006', '小道姑成仙記', '玄幻',
'斬惡鬼,斗尸煞,收妖邪!殺魔人,尋仙草,煉仙丹!初出茅廬的鄭小雅,為了救出師尊,重振師門,踏上了一條危險曲折,驚心動魄的道路一個又一一個不可思議的經(jīng)歷,在她的生命中演繹,。最后她發(fā)現(xiàn),自己的命運(yùn)原來一直都不掌握在自己的手中,且看她如何打破命運(yùn)的枷鎖,成就仙道之軀。',
now(), '羊小樂', 10, 112342, '0000000006.png', '0000000006.png'),
('0000000007', '滿級游戲宅在獸世惹桃花', '幻想時空',
'游戲玩家顧曉黎因為系統(tǒng)bug攜帶滿級技能穿越到游戲中的獸人副本,踏上漫漫歸家之路,卻被各色美男拐騙,只得留下,發(fā)展城邦,與獸夫共同對抗惡勢力。甜寵瑪麗蘇。',
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; # 恢復(fù)外鍵約束
查詢 思路:從根據(jù)用戶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')
到此這篇關(guān)于Mysql多層子查詢(收藏夾案例)的文章就介紹到這了,更多相關(guān)mysql多層子查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SUSE Linux下通過RPM方式卸載MySQL 5過程筆記
這篇文章主要介紹了SUSE Linux下通過RPM方式卸載MySQL 5過程筆記,本文針對使用rpm方式安裝的mysql,需要的朋友可以參考下2014-09-09
解決mysql數(shù)據(jù)庫設(shè)置遠(yuǎn)程連接權(quán)限執(zhí)行g(shù)rant all privileges on&n
這篇文章主要介紹了解決mysql數(shù)據(jù)庫設(shè)置遠(yuǎn)程連接權(quán)限執(zhí)行g(shù)rant all privileges on *.* to 'root'@'%' identified by '密碼' with grant optio報錯,通過本文給大家分享問題原因解析及解決方法,需要的朋友可以參考下2022-11-11
Linux下MYSQL 5.7 找回root密碼的問題(親測可用)
這篇文章主要介紹了Linux下MYSQL 5.7 找回root密碼的問題(親測可用),通過 --skip-grant-tables 找回,新增完成后,:wq 保存退出,重啟mysqld服務(wù),具體內(nèi)容詳情跟隨小編一起看看吧2021-10-10

