0

I am trying to select multiple rows from a database based on the date (descending), and making sure that I do not get any rows that have the same part number. So basically I need to do something like this:

SELECT `part_number`, `second_field`, `third_field`, `date`
FROM `my_table`
GROUP BY DISTINCT('part_number'), order_date DESC

I know the syntax for this query is not correct, I am just not sure how to do it. I think there is a possibility I might need to use ORDER BY too.

Tigerman55
  • 233
  • 10
  • 20

2 Answers2

1

Try to remove the , and change the GROUP BY

SELECT `part_number`, `second_field`, `third_field`, `date`
FROM `my_table`
GROUP BY part_number
ORDER BY `date` DESC
Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55
  • Unfortunately your query does not get the parts with the most recent date - which is what I was looking for. I am not sure why. The above answer I made shows a query I found which does work. – Tigerman55 Aug 13 '14 at 16:31
0

I've done some research on how to write a query according to my needs. I've found the best way is using the following query:

SELECT `part_number`, `second_field`, `third_field`, MAX(`date`) AS most_recent_date
FROM `my_table`
GROUP BY `part_number`

I found the information I needed in the best answer of this question.

Community
  • 1
  • 1
Tigerman55
  • 233
  • 10
  • 20