Mysql Order By Json Count



  • I try that code;

    select * 
    from `diary` 
    where date(`created_at`) = 2022-05-10 
    order by JSON_LENGTH(like) asc limit 1
    

    Like column in my table;

    +----+--------------+-------------------------+
    | id | like         | created_at              |
    +----+--------------+-------------------------+
    |  1 | ['asas']     | 2022-05-10 07:02:42     |
    +----+--------------+-------------------------+
    

    Here is my error; You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'like) asc limit 1' at line 1

    If I don't use order by, code work fine. How can I fix it?



  • like is a reserved word. I solved the problem by quoting the column name using backticks (`), like this:

    select * from `diary` 
    where date(`created_at`) = 2022-05-10 
    order by JSON_LENGTH(`like`) asc limit 1
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2