mysql:多条记录合并为一条记录

时间:2017-11-13 02:27:34

标签: mysql database

我有一个具有多个兴趣的用户表,因此当我查询userinterest表时,它返回具有多个记录但兴趣不同的同一用户。我试图弄清楚如何在重复数据上加入这些记录,但保留唯一数据。下面我添加了我尝试过的sql语句。

SELECT user.FirstName, user.LastName, user.Profilepix, userinterest.UserId, userinterest.InterestId, interest.Interests, interest.PixLoc 
FROM user 
INNER JOIN userinterest 
ON user.UserId = userinterest.UserId 
INNER JOIN interest 
ON userinterest.InterestId = interest.InterestId 
WHERE user.UserId = 'test1112'

sql

SELECT user.FirstName, user.LastName, user.Profilepix, userinterest.UserId, GROUP_CONCAT(userinterest.InterestId), interest.Interests, interest.PixLoc 
FROM user 
INNER JOIN userinterest 
ON user.UserId = userinterest.UserId 
INNER JOIN interest 
ON userinterest.InterestId = interest.InterestId 
WHERE user.UserId = 'test1112'

sql2

当我GROUP_CONCAT InterestId时,InterestsPixLoc记录会被截断。我如何将它们合并为InterestId

0 个答案:

没有答案