Home  >  Q&A  >  body text

MySQL WHERE clause usage

MySQL I'm trying to get the Category_id values ​​between 9 and the maximum category id without using the Max function (using a subquery).

I tried the MySQL query given below. It works for the latter part, i.e. it gives category_id up to the maximum category_id. However, it gives all category IDs from the beginning (1), i.e. it does not start from "9".

SELECT columns 
FROM table_name 
WHERE (9 <= category_id <=  (
                 SELECT category_id 
                 FROM table_name 
                 ORDER BY category_id 
                 DESC LIMIT 1 )
       );

P粉403804844P粉403804844407 days ago523

reply all(1)I'll reply

  • P粉221046425

    P粉2210464252023-09-10 19:32:46

    Logically, your query is

    SELECT { columns }
    FROM table_name 
    WHERE 9 <= category_id;

    There is no point in using a subquery condition - the column value cannot be greater than the maximum value in this column.

    reply
    0
  • Cancelreply