Answer the question
In order to leave comments, you need to log in
How to select items for which there are currently no records in the database?
Good afternoon, perhaps the title does not fully reflect the essence of the issue, so I will be glad if someone corrects it.
For example, there is a table of users and their orders
Users
id_user | name | ...
500 | Jack | ...
501 | Nick | ....
Orders
id_order | id_user | .... | date | ....
1052 | 500 | .... | 2015-02-23 | ....
1053 | 500 | .... | 2015-02-23 | ....
So, you need to select the number of orders from each user for the last month, the query looks like this
SELECT `users`.`id_user`, count(`orders`.`id_order`) as `sum_orders`
FROM `users`
JOIN `orders` ON (`users`.`id_user` = `orders`.`id_user`)
WHERE `orders`.`date` >= '2015-02-01 00:00:00' AND `orders`.`date` < '2015-03-01 00:00:00'
group by `users`.`id_user`
having `sum_orders` < 15
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question