我想使用MYSQL進行左連接。目前,我有一個表是這樣的:與條件左加入不返回所需的數據
CREATE TABLE `books` (
`bookId` int(11) NOT NULL,
`bookTitle` varchar(100) NOT NULL,
`bookAuthor` varchar(100) NOT NULL,
`bookStatus` tinyint(1) NOT NULL,
`bookDeleteFlag` tinyint(1) NOT NULL
);
CREATE TABLE `lends` (
`lendId` int(11) NOT NULL,Primary
`lendBookId` int(11) NOT NULL,
`lendBorrowerName` Varchar(100) NOT NULL,
`lendBorrowStatus` int(11) NOT NULL,
`lendReturnStatus` int(11) NOT NULL,
);
insert into books values (1, 'The Da Vinci Code', 'Dan Brown', 1,0)
insert into books values (2, 'Theory of Relativity', 'Albert Einstein', 1,0)
insert into books values (3, 'Harry Potter', 'J K Rowling', 1,0)
insert into books values (1, '1', 'Chris', 1,1)
insert into books values (2, '1', 'Lilly', 1,0)
insert into books values (3, '2', 'Chris', 1,0)
insert into books values (3, '3', 'Chris', 1,1)
這樣
bookId bookTitle availability
-----------------------------------------------
1 The Da Vinci Code 0
2 Theory of Relativity 0
3 Harry Potter 1
我基本上是開發一個圖書館管理模塊所需的輸出。我希望該書的可用性在圖書搜索頁面上列出。
當前的代碼,我有是:
SELECT B.bookTitle,
L.lendBorrowerName AS takenName,
count(L.lendStatus) AS taken
FROM books as B
LEFT JOIN lends AS L ON B.bookId = L.lendBookID
WHERE L.lendReturnStatus = 0 // if i remove this code, all rows in books table is listed. However i loose the ability to check the availability of that book
GROUP BY B.bookTitle
,這是什麼問題,典型的解決方案? 在此先感謝。
所需輸出的列不匹配SELECT查詢。 –
創建表借還有一個語法錯誤。請檢查 – Deep
@DeepKakkar我只是想了解一下桌子。我省略了我認爲這個問題沒有必要的專欄,因此增加了一個逗號。 –