尝试这个:
SELECT b.bookname, m.mname
FROM (transaction AS t
LEFT JOIN books AS b
ON b.bookid = t.bookid)
LEFT JOIN member AS m
ON m.mid = t.mid
WHERE t.ttype='issue';
样本数据:
CREATE TABLE books(
bookid INT AUTO_INCREMENT PRIMARY KEY,
bookname VARCHAR (50)
);
CREATE TABLE member(
mid INT AUTO_INCREMENT PRIMARY KEY,
mname VARCHAR (30)
);
CREATE TABLE transaction(
transid INT AUTO_INCREMENT PRIMARY KEY,
bookid INT,
mid INT,
ttype VARCHAR (10),
issue_dt DATETIME,
due_dt DATETIME,
transremark varchar(200)
);
INSERT INTO books(bookname)
VALUES ("Harry Potter and the Philosopher's Stone"), ("Harry Potter and the Chamber of Secrets"), ("Harry Potter and the Prisoner of Azkaban");
INSERT INTO member(mname)
VALUES ('bob'), ('jack'), ('joe');
INSERT INTO transaction(bookid,mid,ttype,issue_dt,due_dt,transremark)
VALUES (1,1,'issue', curdate(),curdate()+ INTERVAL 5 DAY, "UPS"),
(1,2,'issue', curdate(),curdate()+ INTERVAL 5 DAY, "UPS"),
(3,1,'issue', curdate(),curdate()+ INTERVAL 5 DAY, "UPS"),
(2,1,'issue', curdate(),curdate()+ INTERVAL 20 DAY, "UPS"),
(3,3,'no_issue', curdate(),curdate()+ INTERVAL 20 DAY, "FEDEX");
http://sqlfiddle.com/#!2/91b0d/5
编辑:有限制
SELECT b.bookname, m.mname
FROM (transaction AS t
LEFT JOIN books AS b
ON b.bookid = t.bookid)
LEFT JOIN member AS m
ON m.mid = t.mid
WHERE t.ttype='issue'
LIMIT 1 offset 3;
http://sqlfiddle.com/#!2/91b0d/7
也许我误解了,但您只要求书名和会员名。如果您想了解更多信息,也可以使用此查询。在上面的当前查询中替换 SELECT * FROM。
祝你项目好运。