复杂的 group-by MySQL 语句
我有一个包含以下列的表:
boxid - nouns - username
1 - w1,w2,w3 - user1
1 - w3,w2,w4 - user2
1 - w1,w8,w5 - user1
2 - w7,w2,w5 - user2
目前我有一个查询,允许我提取以下内容:
SELECT boxid, group_concat(concat(username,":",nouns) SEPARATOR "|") as listn
FROM table group by boxid;
该查询返回以下内容:
box1 - user1:w1,w2,w3|user2:w3,w2,w4|user1:w1,w8,w5
但是,我想获取同一用户的所有名词而不重复用户(但重复每个用户的话),类似于以下结果:
box1 - user1:w1,w2,w3,w1,w8,w5|user2:w3,w2,w4
有人可以告诉我这是否可以用 mysql 完成以及如何完成吗?我不知道这一点...
(我可以用 php 做到这一点,但我认为在 sql 中获得直接结果会更快...)
提前致谢...
I have a table with the following columns:
boxid - nouns - username
1 - w1,w2,w3 - user1
1 - w3,w2,w4 - user2
1 - w1,w8,w5 - user1
2 - w7,w2,w5 - user2
and at the present I have a query that allows me to extract the following:
SELECT boxid, group_concat(concat(username,":",nouns) SEPARATOR "|") as listn
FROM table group by boxid;
that query gives me back the following:
box1 - user1:w1,w2,w3|user2:w3,w2,w4|user1:w1,w8,w5
However, I would like to get all nouns of the same user without repeating users (but repeating words of every user), something like the following result:
box1 - user1:w1,w2,w3,w1,w8,w5|user2:w3,w2,w4
Can someone show me if this can be done with mysql and how? I have no idea of this...
(I can do this with php, but I think getting the direct result in sql would be faster...)
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: