0

这是我的数据库结构的示例..

uid  email            name
-------------------------------
1    john@gmail.com   John
2    peter@gmail.com  Peter
3    colin@gmail.com  Colin

书桌

book_id  book_name
-------------------
1        PHP
2        MySQL
3        Javascripts
4        CSS

历史表

history_uid  history_bookid
---------------------------
1            1
1            2
1            3
2            1
2            3
3            3

当前语句,包括多选查询history.history_bookid

SELECT users.uid,
       users.email,
       users.`name`,
       history.history_uid,
       history.history_bookid,
       GROUP_CONCAT(history.history_bookid) AS BookID,
       GROUP_CONCAT(book_name)              AS bookTitles
FROM   users
       INNER JOIN history
         ON users.uid = history.history_uid
       INNER JOIN book
         ON history.history_bookid = book.book_id
WHERE  history.history_bookid = 1
        OR history.history_bookid = 3
GROUP  BY users.uid  

结果

uid email            name   history_uid  history_bookid  BookID   bookTitles
---------------------------------------------------------------------------------
1   john@gmail.com   John   1            3               3,1      Javascripts,PHP
2   peter@gmail.com  Peter  2            1               1,3      PHP,Javascripts
3   colin@gmail.com  Colin  3            3               3        Javascripts

问题 :

我如何获得阅读过的书籍的完整列表uid = 1?我想要的示例输出包括所有书history table

uid email            name   history_uid  history_bookid  BookID   bookTitles
---------------------------------------------------------------------------------
1   john@gmail.com   John   1            3               1,2,3    PHP,MySQL,Javascripts
2   peter@gmail.com  Peter  2            1               1,3      PHP,Javascripts
3   colin@gmail.com  Colin  3            3               3        Javascripts
4

2 回答 2

1

我如何获得阅读过的书籍的完整列表uid = 1

将其作为过滤器选项添加到您的WHERE子句中:

WHERE history.history_bookid IN (1,3) OR users.uid = 1
于 2012-05-07T22:44:30.820 回答
1

你的意思是你想要书名和书名?

将 a 添加GROUP_CONCAT(book_name) AS bookTitles到您的字段列表中。

于 2012-05-07T21:51:23.883 回答