-1

我有 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;

我想得到上面查询中存在的bookname以及mnamebookidmid..请提出一个查询,我可以在其中得到上述查询的结果以及 bookname 和 mname..提前谢谢...

4

4 回答 4

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;
于 2013-01-24T09:04:50.540 回答
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子句

于 2013-01-24T08:57:58.850 回答
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' ;

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

于 2013-01-24T09:37:21.073 回答
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

编辑:有限制

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。

祝你项目好运。

于 2013-01-24T10:42:00.943 回答