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 '上線時(shí)間', 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)不是衡量一個(gè)人的刻度,只有責(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 '上線時(shí)間', 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)水八卦,但江湖人都稱我絕世鬼才。為什么?因?yàn)槲夷X子有坑!裝滿來至新世紀(jì)的現(xiàn)代學(xué)識!講道理,其實(shí)我是個(gè)很純潔的斯文人,不管你們信不信,反正我是信了。', now(), '武異', 10, 1, '0000000001.png', '0000000001.png'), ('0000000002', '暴君的穿越小邪后', '古代言情', '一名現(xiàn)代A幫千金,機(jī)緣巧合下穿越到大齊——一個(gè)歷史上完全不存在的朝代,在這個(gè)波詭云譎的時(shí)代,看她如何見招拆招,并和那個(gè)冷面帝王談一場千古絕戀。帶著皇帝在現(xiàn)代吃喝玩樂,在古代又玩起了帝后和鳴,好不快活!', now(), '非墨千瀧', 10, 1412342, '0000000002.png', '0000000002.png'), ('0000000003', '汗靈昭烈', '武俠仙俠', '打虎上山,行俠入世,一代英雄聚念而生。單臂摯天,雙劍仗義,千年華夏因義流芳。武松是誰?他為何而來?他又將為誰而戰(zhàn)?一代漢靈入世,滄海下暗流涌動,乾坤里誰是英雄!', now(), '千秋一夢', 10, 151241, '0000000003.png', '0000000003.png'), ('0000000004', '大明煙火', '歷史軍事', '朱元璋:“一群貪贓枉法之輩,朕要?dú)⒐馑麄?。”韓度:“陛下息怒,與其殺了,不如廢物利用,壓榨干凈他們的剩余價(jià)值才好。”朱元璋:......', 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', '小道姑成仙記', '玄幻', '斬惡鬼,斗尸煞,收妖邪!殺魔人,尋仙草,煉仙丹!初出茅廬的鄭小雅,為了救出師尊,重振師門,踏上了一條危險(xiǎn)曲折,驚心動魄的道路一個(gè)又一一個(gè)不可思議的經(jīng)歷,在她的生命中演繹,。最后她發(fā)現(xiàn),自己的命運(yùn)原來一直都不掌握在自己的手中,且看她如何打破命運(yùn)的枷鎖,成就仙道之軀。', now(), '羊小樂', 10, 112342, '0000000006.png', '0000000006.png'), ('0000000007', '滿級游戲宅在獸世惹桃花', '幻想時(shí)空', '游戲玩家顧曉黎因?yàn)橄到y(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 '收藏時(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查詢圖書
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)文章
MSQL中DATETIME或TIMESTAMP的區(qū)別小結(jié)
MySQL中的?DATETIME?和?TIMESTAMP?類型都用于存儲日期和時(shí)間信息,本文主要介紹了MSQL中DATETIME或TIMESTAMP的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03master and slave have equal MySQL server ids
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids2013-07-07Workbench連接不上阿里云服務(wù)器Ubuntu的Mysql解決方法(已測)
這兩天為了解決workbench連接不上阿里云服務(wù)器的問題,搞得頭大,網(wǎng)上搜到的教程都大同小異,但唯獨(dú)到我這就是行不通。不過好在最后終于解決了,記錄一下這個(gè)坑爹的過程,另外腳本之家小編特把這些問題整理了一下,看完這一篇文章基本上就解決了2020-02-02在Mysql數(shù)據(jù)庫里通過存儲過程實(shí)現(xiàn)樹形的遍歷
關(guān)于多級別菜單欄或者權(quán)限系統(tǒng)中部門上下級的樹形遍歷,oracle中有connect by來實(shí)現(xiàn),mysql沒有這樣的便捷途徑,所以MySQL遍歷數(shù)據(jù)表是我們經(jīng)常會遇到的頭痛問題,下面給大家介紹在Mysql數(shù)據(jù)庫里通過存儲過程實(shí)現(xiàn)樹形的遍歷,一起看看吧2016-11-11