合并 Select 中的两个表(Microsoft SQL Server 2014)
Combine two tables in Select (Microsoft SQL Server 2014)
我有两个表 Publisher
和 Book
:
Publisher(
PublisherID (PK),
Name,
Address
)
Book(
BookID (PK),
Name,
Author,
Price,
PublisherID (FK on Publisher)
)
我想显示 由名为 XXX 的出版商出版的图书列表。我该怎么做?
SELECT
Book.*
FROM
Book
WHERE
PublisherID = (
SELECT
PublisherID
FROM
Publisher
WHERE
Name = 'XXX'
LIMIT 1
)
或者:
SELECT
Book.*
FROM
Book
LEFT JOIN
Publisher
ON
Publisher.PublisherID = Book.PublisherID
WHERE
Publisher.Name = 'XXX'
我有两个表 Publisher
和 Book
:
Publisher(
PublisherID (PK),
Name,
Address
)
Book(
BookID (PK),
Name,
Author,
Price,
PublisherID (FK on Publisher)
)
我想显示 由名为 XXX 的出版商出版的图书列表。我该怎么做?
SELECT
Book.*
FROM
Book
WHERE
PublisherID = (
SELECT
PublisherID
FROM
Publisher
WHERE
Name = 'XXX'
LIMIT 1
)
或者:
SELECT
Book.*
FROM
Book
LEFT JOIN
Publisher
ON
Publisher.PublisherID = Book.PublisherID
WHERE
Publisher.Name = 'XXX'