MySql数据库结构:基于单列和不同值的搜索

MySql数据库结构:基于单列和不同值的搜索,第1张

MySql数据库结构:基于单列和不同值的搜索

试试这个:

create table City    (     Id int,     Name varchar(50)    );    insert into City (Id, Name)     VALUES     (1, 'Toronto'),     (2, 'Chicago')    create table Libraries(    Id int,    Name varchar(50),    CityId int    );    insert into Libraries (Id, Name, CityId)     VALUES     (1, 'Toronto Library 1', 1),    (2, 'Toronto Library 2', 1),    (3, 'Chicago Library 1', 2),    (4, 'Chicago Library 2', 2)    create table Books(    Id int,    Isbn varchar(12),    LibraryId int    );    insert into Books (Id, Isbn, LibraryId)     Values    (1, '1234567891', 1),    (2, '13344555', 1),    (3, 'x123sada', 1),    (4, 'xasdsadas', 2),    (5, 'axxzksda', 2)    select DISTINCT b.Name     from Books a    inner join Libraries b    on a.LibraryId = b.Id    where Isbn in ('1234567891', '13344555')    and b.CityId = 1

编辑:或4NF:

create table City( Id int, Name varchar(50));insert into City (Id, Name) VALUES (1, 'Toronto'), (2, 'Chicago')create table Libraries(Id int,Name varchar(50),CityId int);insert into Libraries (Id, Name, CityId) VALUES (1, 'Toronto Library 1', 1),(2, 'Toronto Library 2', 1),(3, 'Chicago Library 1', 2),(4, 'Chicago Library 2', 2)create table Books(Id int,Isbn varchar(12),);insert into Books (Id, Isbn) Values(1, '1234567891'),(2, '13344555'),(3, 'x123sada'),(4, 'xasdsadas'),(5, 'axxzksda')create table LibraryBooks(LibraryId int,BookId int);insert into LibraryBooks (LibraryId, BookId)VALUES(1, 1),(1, 2),(3, 1),(2, 4),(5, 2)select DISTINCT c.Namefrom Books ainner join LibraryBooks bon a.Id = b.BookIdinner join Libraries c onc.Id = b.LibraryIdwhere Isbn in ('1234567891', '13344555')and c.CityId = 1


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

原文地址: http://outofmemory.cn/zaji/5640449.html

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

发表评论

登录后才能评论

评论列表(0条)

保存