2012-05-07 25 views
0

下面一个例子我的数据库结构..如何使用WHERE语句在结果上获得完整的GROUP_CONCAT列表?

uid email   name 
------------------------------- 
1 [email protected] John 
2 [email protected] Peter 
3 [email protected] 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 [email protected] John 1   3    3,1  Javascripts,PHP 
2 [email protected] Peter 2   1    1,3  PHP,Javascripts 
3 [email protected] Colin 3   3    3  Javascripts 

问:

我如何册图书的完整清单被uid = 1读?示例输出我要的是包括所有的书history table

uid email   name history_uid history_bookid BookID bookTitles 
--------------------------------------------------------------------------------- 
1 [email protected] John 1   3    1,2,3 PHP,MySQL,Javascripts 
2 [email protected] Peter 2   1    1,3  PHP,Javascripts 
3 [email protected] Colin 3   3    3  Javascripts 
+0

为什么你有你'WHERE'子句中的过滤器,如果你想每一个结果? – eggyal

+0

@eggyal我使用了选择jQuery:示例http://i.stack.imgur.com/AWRYH.png – user1286499

回答

1

我如何获得完整的书籍列表被uid = 1读取?

添加,作为一个筛选选项您WHERE条款:

WHERE history.history_bookid IN (1,3) OR users.uid = 1 
+0

我尝试过'IN'&'FIND_IN_SET',结果与'AND'相同。我错过了这里错了吗? – user1286499

+0

@ user1286499:我的回答更多地指的是'OR users.uid = 1'部分。 – eggyal

+0

当你提到'uid'时,它就起作用了。我有一个鸡蛋!其实正确的说法是:WHERE history.history_uid = 1 OR history.history_uid = 3' – user1286499

1

你的意思是你想要的标题,以及书中的id?

将你的字段列表加上GROUP_CONCAT(book_name) AS bookTitles

+0

我已更新我的问题。 +1包括书名:) – user1286499

相关问题