camfab Posted March 11, 2017 Share Posted March 11, 2017 Hello, I'm trying to retrieve via sql query the list of all customers which have not ordered yet (in order to give them a particular discount)This is the code: select c.id_customer, c.firstname, c.lastname, count(o.id_order) as ordercnt from ps_customer c left join ps_orders o on c.id_customer = o.id_customer group by o.id_customer but this query report all customers with order count as 1 (also the customers which have not ordered yet...Can you help me?thank you ! Link to comment Share on other sites More sharing options...
bellini13 Posted March 11, 2017 Share Posted March 11, 2017 this should do it SELECT c.id_customer, c.firstname, c.lastname FROM ps_customer c WHERE NOT EXISTS (SELECT id_order FROM ps_orders o where c.id_customer = o.id_customer) Link to comment Share on other sites More sharing options...
camfab Posted March 14, 2017 Author Share Posted March 14, 2017 (edited) Thank you, with a little correction, your code solved the problem! SELECT c.id_customer, c.firstname, c.lastname FROM ps_customer c WHERE (NOT EXISTS (SELECT id_order FROM ps_orders o where c.id_customer = o.id_customer) ) Edited March 14, 2017 by camfab (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now