查询连接三个表

时间:2013-01-24 08:52:39

标签: mysql join

我有3张桌子 -

books (bookid,bookname)

member(mid,mname)

transaction(transid,bookid,mid,ttype,issue_dt,due_dt,transremark)

现在我有一个msql查询 -

SELECT (select count(*) FROM transaction where ttype='issue') as rownum,
transid,bookid,mid,ttype,issue_dt,due_dt,transremark 
FROM transaction 
WHERE ttype='issue' 
ORDER BY transid 
LIMIT 1 offset 3;

我希望得到booknamemname,其中bookidmid出现在上述查询中。 请建议一个查询,我可以在其中获得上述查询的结果以及bookname和mname .. 提前谢谢......

4 个答案:

答案 0 :(得分:1)

试试这个::

SELECT
count(1) as rownum,
transid,
b.bookid,
m.mid,
ttype,
   issue_dt,
    due_dt,
    transremark, 
    b.bookname,
    m.mname
    from transaction t
    INNER JOIN books b on (b.bookid=t.bookid)
    INNER JOIN member m on (t.mid=m.mid)
    where ttype='issue' ORDER BY transid LIMIT 1 offset 3;

答案 1 :(得分:0)

您可以尝试以下查询

SELECT bookname, mname, temp.*
FROM books, member,
(SELECT (select count(*) FROM transaction where ttype='issue') as 
rownum,transid,bookid,mid,ttype,issue_dt,due_dt,transremark 
from transaction where 
ttype='issue' 
ORDER BY transid LIMIT 3,1) as temp
WHERE books.bookid = temp.bookid
AND member.mid = temp.mid

如果不起作用,您可以尝试删除 LIMIT 子句

答案 2 :(得分:0)

在下面给出一些可能对您有所帮助的查询。我没有经过测试就提交了。如果您遇到任何语法错误,请重写此内容。而且如果你认为你想要任何其他想法,那么你可以通知......

SELECT transaction.*, member.mname, books.bookname
FROM transaction
INNER JOIN member ON transaction.mid = member.mid
INNER JOIN books ON transaction.bookid = books.bookid
WHERE transaction.ttype='issue' ;

通过使用此查询,您将获得每个成员的单独行。

答案 3 :(得分:0)

试试这个:

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

编辑:使用LIMIT

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。

祝你好运。