0
SELECT DISTINCT(a.id) as article_id, title, alias, id_page, file, af.id as file_id
    FROM (
        SELECT 
            id_item, 
            min(rank) as min_rank
        FROM pm_article_file 
        GROUP BY id_item
    ) as mins
    INNER JOIN pm_article_file as af ON mins.id_item = af.id_item AND mins.min_rank = af.rank
    INNER JOIN pm_article as a ON mins.id_item = a.id
    WHERE a.home = 1 AND a.checked = 1 AND af.checked = 1 AND a.lang = 2
    ORDER BY a.add_date DESC LIMIT 4

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') as min_rank FROM pm_article_file GROUP BY id_item ) as' at line 5

how could I fix that syntax error?

lemon
  • 14,875
  • 6
  • 18
  • 38
  • The DISTINCT keyword always applies to all fields, enclosing within parentheses a single field is not relevant. – lemon Oct 18 '22 at 19:01
  • 3
    `rank` is reserved word, if you use it as a column name then you must to quote it. – Akina Oct 18 '22 at 19:03
  • @lemon This is synthactically correct, in this case the parenthesis are treated as "scalarizating operator". – Akina Oct 18 '22 at 19:03
  • The keyword "*rank*" is definitely the problem here, though what do you mean by "scalarizating operator"? – lemon Oct 18 '22 at 19:05
  • 1
    @lemon It is used when you use a subquery which return a rowset of one column and one row which must be converted to single scalar value for further use in some expression/condition. – Akina Oct 18 '22 at 19:07
  • @Akina thank you so much, I didn't realize this. I used that code at Mysql 5.7 and it was work after 8.0 'rank' added to reserved keyword. Thanks a lot. – Enes Aktaş Oct 18 '22 at 20:29

0 Answers0