0

Hello guys i have this error in mysql with the following query, I don't know where do mistake, thank you for the help :)

SELECT a.*,b.*,users.*,
    (SELECT p.msg_text,p.occured_at 
    FROM message_private p 
    WHERE p.group_id=a.group_id 
    ORDER BY p.occured_at DESC LIMIT 1) as message,
    f.countf,message.occured_at
FROM message_group a
INNER JOIN message_group b ON a.group_id=b.group_id
INNER JOIN users ON users.profile_id = b.profile_id
LEFT JOIN 
(
    SELECT COUNT(profile_id) countf, id_group
    FROM message_view
    WHERE profile_id = 'sN07X2'
    GROUP BY id_group
) f
  on f.id_group = b.group_id
WHERE a.profile_id = 'sN07X2'
    AND b.profile_id != a.profile_id 
    AND countf > 0 
ORDER BY p.occured_at DESC 
LIMIT 9
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Fabrizio Fenoglio
  • 5,767
  • 14
  • 38
  • 75

2 Answers2

1

The field list in your query is

SELECT a.*,b.*,users.*,
    (SELECT p.msg_text,p.occured_at 
    FROM message_private p 
    WHERE p.group_id=a.group_id 
    ORDER BY p.occured_at DESC LIMIT 1) as message,
    f.countf,message.occured_at

This aliases the result from the subquery as message.

BUT, the subquery selects two columns.

nurdglaw
  • 2,107
  • 19
  • 37
1

This :

 (SELECT p.msg_text,p.occured_at 
    FROM message_private p 
    WHERE p.group_id=a.group_id 
    ORDER BY p.occured_at DESC LIMIT 1) as message

Should return 1 value and it returns 2 that's why the error occurs

Solution 1:

CONCAT(p.msg_text,p.occured_at )

Solution 2:

have 2 subqueries one for p.msg_text and one for p.occured_at

Stephan
  • 8,000
  • 3
  • 36
  • 42