0

Question resolved

I have two tables, orders and customers, and I'd like to find all customers where orders is greater than 0.

I currently use two queries:

SELECT * FROM customers 

and

SELECT count(id) 
FROM orders 
WHERE customer='CUSTOMER_VALUE' 
AND siteid='siteid'

I'd like to turn this into one query, so it finds all customers where they've placed one or more orders in the store.

I tried the following, but it doesn't work:

SELECT c.*, 
       COUNT(o.customer) AS numOrders 
FROM customers c, 
     orders o 
WHERE o.siteid= 'calico' 
AND o.customer=c.email

(it only gives one result, which is customer ID 1).

I only need to find the orders value, and a few values from the customers table.

The customer field in orders and the id field in customers are the same value (ie order 1 was placed by customer 5, customer id 5 is "John").

Is there any way to do this?

The current way works, but it would be greatly inefficient if there was to be a large amount of customers.

1
  • Does order.customer store the customer.id or the customer.email ? Commented Dec 24, 2011 at 12:40

6 Answers 6

3

The reason your second query is returning only 1 row, is because oyu dont have a GROUP BY. Unlike many SQL databases, MySQL does allow you to mix non-aggregated fields with aggregated ones, even though its technically not valid sql, and the results are unpredictable.

Try

SELECT c.id, c.email, COUNT(o.customer) AS numOrders 
 FROM customers c 
  INNER JOIN orders o on (o.customer=c.email) 
  WHERE o.siteid= 'calico' 
 GROUP BY c.id, c.email
Sign up to request clarification or add additional context in comments.

1 Comment

When you group by the Primary Key, it's technically valid SQL (by the SQL-2003+ standards) - although MySQL does not really check that and it allows technically invalid queries as well.
0

You can join the two tables like this:

SELECT c.* 
FROM customers c
INNER JOIN orders o ON o.customer = c.id

Using an Inner Join will only result the customers that have entries in the orders table.

Comments

0
SELECT c.*
     , COUNT(*) AS numOrders 
FROM customers c
  JOIN orders o 
    ON o.customer = c.id
WHERE o.siteid = 'calico'
GROUP BY c.id

Comments

0
SELECT
  customers.*,
  count(*) as ordercount
FROM customers
INNER JOIN orders ON customers.id=orders.customer
WHERE orders.siteid= 'calico'
GROUP BY customers.id;

Comments

0
SELECT c.id, c.email, COUNT(o.customer) AS numOrders 
FROM customers c, orders o 
WHERE o.siteid= 'calico' AND o.customer=c.email
GROUP BY c.id, c.email

Comments

0

Not sure if this would work, but you could try:

SELECT c.*, COUNT(o.customer) AS numOrders FROM customers c 
JOIN orders o on o.customer = c.id 
WHERE o.siteid= 'calico' AND o.customer=c.email AND numOrders > 0
GROUP BY c.id, c.email

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.