Greetings to all. Tell me, please, what am I doing wrong?

SELECT cabinet.name_test, MIN(answers.id), a1.answer_id as answer_id, a2.answer_id as answer_id2 FROM cabinet JOIN answers a1 ON a1.test_id = cabinet.test_id AND a1.user_id = '184918649' AND a1.friend_id = '353281' LEFT JOIN answers a2 ON a2.test_id = cabinet.test_id AND a2.user_id = '353281' ORDER BY answers.id 

I need to sort the user responses by the minimum id field.

  • So I understand, displays one entry. Explain in more detail. - KaZatsa
  • It displays error # 1054 - Unknown column 'answers.id' in 'field list'. Undefined column. Why does he write like that? In my answers table, the id column is the very first. - Dimaz
  • If the code is changed, then it is possible to return quotes. - KaZatsa

2 answers 2

 ORDER BY answers.id DESC 

it is not clear what is required

  • It displays error # 1054 - Unknown column 'answers.id' in 'field list'. Undefined column. Why does he write like that? In my answers table, the id column is the very first. - Dimaz

You do not have the answers table in the FROM sample. You have redefined this table with aliases a1 and a2 . Here from them and choose, for example:

SELECT ...

MIN (a1.id),

...

ORDER BY a1.id DESC

In addition, when you use such functions as MIN, MAX, COUNT, etc., there should be an additional GROUP BY grouping, and you do not have it