3

I have two tables, customers and Orders

Customers columns are

CustomerID,
Username,
Password,
Firstname,
Surname,
Email,
Mobile

Orders columns are

OrderID,
CustomerID,
Date,
Time,
Price,
Complete

I want to select all of the firstname and surnames from all the orders that have been complete. And yes It could be so that [0] = John Smith and [1] also = John Smith.

What I was thinking was

SELECT FirstName, Surname from order, customers
WHERE Complete = 'Yes' AND order.CustomerID = customer.CustomerID;

So first it looks at if the order is complete. If it is then it will look at the customer ID, then it will go to customers and get the firstname and surname of that customer then store it in the datatable.

Thank you for any help!!!

1
  • Tip: Store date and time as a single entity Commented Feb 15, 2017 at 16:18

4 Answers 4

2

You could use EXISTS, the following query will return all customers that don't have incomplete (=0) orders:

select c.firstname, c.lastname
from customers c
where
  not exists (select * from orders o
              where c.customerid = o.orderid
                    and o.complete = 'No')

but it will also return customers that have no orders. If you want to exclude customers with no orders you could use an additional exist clause:

select c.firstname, c.lastname
from customers c
where
  not exists (select * from orders o
              where c.customerid = o.orderid
                    and o.complete = 'No')
  and exists (select * from orders o where c.customerid = o.orderid)

or a group by clause:

select c.firstname, c.lastname
from customers c inner join orders o on c.customerid = o.customerid
group by c.customerid, c.firstname, c.lastname
having sum(o.complete='No') = 0
Sign up to request clarification or add additional context in comments.

1 Comment

Group by wouldn't work, since OP wants duplicates if multiple orders have the same customer.
0

This will give you list of Firstname, Surname even though they don't have orders.

SELECT Customers.Firstname, Customer.Surname
FROM Customers, Orders
WHERE Orders.Complete = 'Yes'
LEFT JOIN Customers.CustomerID = Orders.CustomerID

1 Comment

Who upvotes this stuff? You cannot simply make up syntax.
0

I'd go with this personally:

SELECT c.Firstname, c.Surname FROM Customers c
INNER JOIN Orders o
ON c.CustomerID=o.CustomerID
WHERE o.Complete='Yes'

I like to be as explicit as possible with my queries, so anyone else who has to read my code understands the what, why and how. Though shouldn't you also select something to identify the order? Otherwise all you have is a list of names.

Comments

0
--Unique list of customer id, customer first name and customer surname.
SELECT DISTINCT
    customers.customerid
    , customers.firstname
    , customers.surname
FROM        orders
INNER JOIN  customers 
ON 
    customers.customerid = orders.customerid
    AND orders.complete = 'Yes'

--Unique list of customer first name and customer surname, regardless 
--if same names are tied to different customerid.
SELECT DISTINCT
    customers.customerid
    , customers.firstname
    , customers.surname
FROM        orders
INNER JOIN  customers 
ON 
    customers.customerid = orders.customerid
    AND orders.complete = 'Yes'

Remove DISTINCT word if you want duplicates.

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.