我正在跟踪用户连接到网络应用并在问题提要中留下问题
id name
---------
1 Jim
2 Joe
由于用户可以从各种浏览器连接到应用并具有多个连接,因此我使用令牌表跟踪用户连接:
id userid
----------
1 1
2 1
3 2
4 2
然后用户提出问题:
id userid pageno text
------------------------------------------------
1 1 1 'Whats going on here?'
2 1 2 'How are you?'
4 2 2 'Good and you?'
我的目标是输出以下内容
username questions Questions Page 1 Questions Page 2
-----------------------------------------------------------------------------------
Jim 'Whats going on here?', 'How are you?' 'Whats going on here?' 'How are you?'
Joe 'Good and you?' 'Good and you?'
现在我尝试直接查询users表,但由于我的db中有很多用户,我需要查询令牌表而不是用户性能然后按用户ID进行分组,因为一个用户可以获得许多令牌。 / p>
虽然现在我按用户ID分组,但我不确定如何分组连接不同的问题。 请参阅查询:
SELECT u.id, u.name, q.text
FROM token t
LEFT JOIN user as u ON t.userid = u.id
LEFT JOIN question as q ON t.userid = q.userid
GROUP BY t.userid
我的问题是我只收到第一个问题,但我试图将concat分组,但没有运气。
忘了补充一点,如果这可以动态完成会很好,因为页码的变化非常大 你能帮忙吗? 感谢
答案 0 :(得分:0)
尝试完这个GROUP_CONCAT(DISTINCT q.text) as question FROM
你得到了什么?
SELECT u.id, u.name, GROUP_CONCAT(DISTINCT q.text) as question
FROM token t
LEFT JOIN user as u ON t.userid = u.id
LEFT JOIN question as q ON t.userid = q.userid
GROUP BY t.userid
答案 1 :(得分:0)
查询应如下所示:
SELECT temp.*, (SELECT GROUP_CONCAT(q.text) FROM questions q WHERE q.pageno = temp.pageno) as Pagewiseqtn FROM (SELECT u.id, u.name, GROUP_CONCAT(DISTINCT q.text) as question, q.pageno
FROM tokens t
LEFT JOIN users as u ON t.userid = u.id
LEFT JOIN questions as q ON t.userid = q.userid
GROUP BY t.userid) as temp
<强>输出:强>