Hello. This question is an addition to the question. Counting data from a table with M-M mysql links .

Input data:

Tables orders, products, order_products . In the order_products table order_products are order_id, product_id, price, count . I get the total field through the use of sum() , the following query is received:

 select `orders`.*, sum( order_products.price * order_products.count) as `total` from `orders` left join `order_products` on `order_products`.`order_id` = `orders`.`id` where `id` = 2 group by `orders`.`id` 

The question is in the use of where for the total field. If I add the following to the query in where : where 'total' > 1000 , I get an error saying that the total field does not exist. Yes, the field itself does not exist, but the received data is. How do I use where in this case?

    1 answer 1

    The correct option is to use HAVING instead of WHERE .

    WHERE does not work on the resulting columns, it looks "directly" in the database, and HAVING filters the data already received after WHERE .

    PS I went to tighten SQL.

    • We are always happy to help you - Manitikyl