Skip to content
Advertisement

query to get customer list using JOIN with sum () of the amounts spent in orders

I have the following tables

table anag (customer registry)


table levels (table that connects each customer to his salesperson. For database registration reasons, the link between customer and seller is given by the customer’s telephone number)


table orders (contains all purchases made by customers, of course)


What I’m trying to do is get from the JOIN of these tables a complete list by seller of his customers sorted in descending order by the amount spent on orders as long as the order status is 2 or 3
Something like this (example seller id 7):


I have tried with this query which seems correct to me, but unfortunately it returns me error in fetch_assoc()

If I separate the subquery from the main query, both return the data I expect and it seems strange to me the JOIN between the two does not work properly

Advertisement

Answer

I think the only real error is using curly braces instead of parentheses:

In addition:

  • You can use IN to shorten an equality comparison to multiple values.
  • Although I left them in, status and id_seller look like numbers. If so, drop the single quotes. Don’t mix data types.
  • Your question is ambiguous on what to do if the seller in orders differs from the seller in anag for a customer. This keeps your logic (the sellers need to match).
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement