iceMan33
iceMan33

Reputation: 369

Pulling last 10 orders from mysql table

I'm new to mysql and I'm trying to figure out if there is a way that you can pull information from the the last 5 most recent orders.

I'm trying to pull orderNumber, productName, and firstname for last 5 most recent orders.

I created 2 dummy tables that I'm working with:

Table: orders

Fields: orderNumber customerOid orderInformationOid purchaseDateTime

Table: customerData

Fields: customerOid firstName middleInitial lastName

Table: products

Fields: productOid productName companyOid

I was thinking an INNER JOIN but how to determine the most recent orders?

Upvotes: 0

Views: 72

Answers (1)

fthiella
fthiella

Reputation: 49069

I suppose there's a productOid column in your orders table, then you can use this query:

SELECT o.orderNumber, p.productName, c.firstname
FROM
  (SELECT orderNumber, customerOid, productOid
   FROM orders
   ORDER BY purchaseDateTime DESC
   LIMIT 5) o
  INNER JOIN customers c ON o.customerOid = c.customerOid
  INNER JOIN products p ON o.productOid = p.productOid

Upvotes: 1

Related Questions