2 Votes Vote

MySQL - Query to return customer's first order

Posted by topdog 174 days ago Questions| customer order mysql query return All

Basic schema (only relevant fields shown):

customers

id  |  fname  |  sname  |  email

orders

order_id  |  customer  |  level  |  timestamp

I need to select every customer, but along with their personal details I want to join the level of their first order.

SELECT c.fname, c.sname, c.email, o.level
FROM customers c
LEFT JOIN orders o ON c.id = o.customer

..?

Edit

I have tried two solutions so far and they completely kill my entire database. No database requests will complete after running the queries, so I'm wondering if maybe the queries are too large. There are about 10,000 orders and 15,000 customers. Is this expected? I've never had these problems before on these tables.

Originally asked by: BadHorsie on Stack Overflow

1 Answer Bury


Who Voted for this Question



Answers

comment-avatar
topdog
174 days ago
You'll want to ORDER BY timestamp and then use LIMIT 1 to choose the first result.
Rating: 0   up down

Log in to answer or register here.