I'm just encountering an issue that i have no clue on how to solve. It is related to this (solved) problem.
Like mentioned in the other post, i have a Media
table that can hold up many records of the same user, but i only want to display a maximum of six records (in order, In any case, only one Type
1 image, followed by a maximum of five Type
2 images).
The query i now have works fine as long as there is only one Type
1 image, but when i add another Type
1 image, the query displays them both. Unfortunatly, something like ORDER BY UserMedia.m_Type = 1 DESC LIMIT 1
in an GROUP_CONCAT
doesn't work, but it is exactly what i need. Anybody a clever idea how to realise this?
I have a SQL Fiddle here with the relevant code. My query looks like this
SELECT
User.u_UserName, User.u_UserMail, User.u_UserRegistration,
Status.us_PaymentStatus,
Sex.us_Gender, Sex.us_Interest,
Personal.up_Name, Personal.up_Dob, Personal.up_City, Personal.up_Province,
UserMedia.m_Id, UserMedia.m_Type, SUBSTRING_INDEX(
GROUP_CONCAT(
CONCAT(
UserMedia.m_Type, ':', UserMedia.m_File
)
ORDER BY UserMedia.m_Type = 1, UserMedia.m_Date DESC SEPARATOR '|'
),'|',6
) AS userFiles
FROM User AS User
JOIN User_Status AS Status ON Status.User_u_UserId = User.u_UserId
JOIN User_Sex_Info AS Sex ON Sex.User_u_UserId = User.u_UserId
LEFT JOIN User_Personal_Info AS Personal ON Personal.User_u_UserId = User.u_UserId
LEFT JOIN Media AS UserMedia ON UserMedia.User_u_UserId = User.u_UserId
WHERE User.u_UserId = '18'
GROUP BY User.u_UserId