Mysql多層子查詢示例代碼(收藏夾案例)
用戶表
create table if not exists tb_book ( book_id char(10) not null comment 'book主鍵', book_name char(30) not null comment '書(shū)名', book_type char(10) not null comment '類別', book_brief varchar(500) not null comment '簡(jiǎn)介', book_online_time timestamp not null comment '上線時(shí)間', book_author char(20) not null comment '作者', book_grade int(2) not null comment '評(píng)分', book_word_number int(5) not null comment '售量', book_small_picture char(100) not null comment '書(shū)小圖', book_big_picture char(100) comment '書(shū)大圖', 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)不是衡量一個(gè)人的刻度,只有責(zé)任的疊加才會(huì)讓人逐漸成長(zhǎng)。'), ('324623125','xz','2341235@qq.com','15534562345',0,'風(fēng)一樣的少年',CURDATE(),25,'123456','default.png','昨天再好,也走不回去;明天再難,也要抬腳繼續(xù)。'); select * from tb_user
圖書(shū)表
create table if not exists tb_book ( book_id char(10) not null comment 'book主鍵', book_name char(30) not null comment '書(shū)名', book_type char(10) not null comment '類別', book_brief varchar(500) not null comment '簡(jiǎn)介', book_online_time timestamp not null comment '上線時(shí)間', book_author char(20) not null comment '作者', book_grade int(2) not null comment '評(píng)分', book_word_number int(5) not null comment '售量', book_small_picture char(100) not null comment '書(shū)小圖', book_big_picture char(100) comment '書(shū)大圖', 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', '天降鬼才', '玄幻', '我不會(huì)奇門遁甲,也不懂風(fēng)水八卦,但江湖人都稱我絕世鬼才。為什么?因?yàn)槲夷X子有坑!裝滿來(lái)至新世紀(jì)的現(xiàn)代學(xué)識(shí)!講道理,其實(shí)我是個(gè)很純潔的斯文人,不管你們信不信,反正我是信了。', now(), '武異', 10, 1, '0000000001.png', '0000000001.png'), ('0000000002', '暴君的穿越小邪后', '古代言情', '一名現(xiàn)代A幫千金,機(jī)緣巧合下穿越到大齊——一個(gè)歷史上完全不存在的朝代,在這個(gè)波詭云譎的時(shí)代,看她如何見(jiàn)招拆招,并和那個(gè)冷面帝王談一場(chǎng)千古絕戀。帶著皇帝在現(xiàn)代吃喝玩樂(lè),在古代又玩起了帝后和鳴,好不快活!', now(), '非墨千瀧', 10, 1412342, '0000000002.png', '0000000002.png'), ('0000000003', '汗靈昭烈', '武俠仙俠', '打虎上山,行俠入世,一代英雄聚念而生。單臂摯天,雙劍仗義,千年華夏因義流芳。武松是誰(shuí)?他為何而來(lái)?他又將為誰(shuí)而戰(zhàn)?一代漢靈入世,滄海下暗流涌動(dòng),乾坤里誰(shuí)是英雄!', now(), '千秋一夢(mèng)', 10, 151241, '0000000003.png', '0000000003.png'), ('0000000004', '大明煙火', '歷史軍事', '朱元璋:“一群貪贓枉法之輩,朕要?dú)⒐馑麄??!表n度:“陛下息怒,與其殺了,不如廢物利用,壓榨干凈他們的剩余價(jià)值才好。”朱元璋:......', now(), '陽(yáng)光下寫(xiě)字', 10, 1123424, '0000000004.png', '0000000004.png'), ('0000000005', '重生八零甜妻有點(diǎn)狂', '都市言情', '眼睛一閉一睜,樂(lè)薇重回十六歲。白手起家打拼出的跨國(guó)集團(tuán)沒(méi)了,上億身家也沒(méi)了,只有家徒四壁和兜里的幾毛錢,還有她的小學(xué)學(xué)歷。重活一回,除了賺錢,她還要走進(jìn)知識(shí)殿堂,成為學(xué)霸,走向巔峰,出任CEO,迎娶白富美!一身戎裝又帥又颯的小柳同志疑惑:誰(shuí)是白富美?又白又富又美的樂(lè)大CEO理直氣壯:你。', now(), '林間雪', 10, 112341, '0000000005.png', '0000000005.png'), ('0000000006', '小道姑成仙記', '玄幻', '斬惡鬼,斗尸煞,收妖邪!殺魔人,尋仙草,煉仙丹!初出茅廬的鄭小雅,為了救出師尊,重振師門,踏上了一條危險(xiǎn)曲折,驚心動(dòng)魄的道路一個(gè)又一一個(gè)不可思議的經(jīng)歷,在她的生命中演繹,。最后她發(fā)現(xiàn),自己的命運(yùn)原來(lái)一直都不掌握在自己的手中,且看她如何打破命運(yùn)的枷鎖,成就仙道之軀。', now(), '羊小樂(lè)', 10, 112342, '0000000006.png', '0000000006.png'), ('0000000007', '滿級(jí)游戲宅在獸世惹桃花', '幻想時(shí)空', '游戲玩家顧曉黎因?yàn)橄到y(tǒng)bug攜帶滿級(jí)技能穿越到游戲中的獸人副本,踏上漫漫歸家之路,卻被各色美男拐騙,只得留下,發(fā)展城邦,與獸夫共同對(duì)抗惡勢(shì)力。甜寵瑪麗蘇。', now(), '無(wú)擺', 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 '收藏時(shí)間', 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; # 暫時(shí)取消外鍵約束 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查詢圖書(shū)
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

MLSQL Stack如何讓流調(diào)試更加簡(jiǎn)單詳解

MSQL中DATETIME或TIMESTAMP的區(qū)別小結(jié)

master and slave have equal MySQL server ids

Workbench連接不上阿里云服務(wù)器Ubuntu的Mysql解決方法(已測(cè))

在Mysql數(shù)據(jù)庫(kù)里通過(guò)存儲(chǔ)過(guò)程實(shí)現(xiàn)樹(shù)形的遍歷