Karem
Karem

Reputation: 18103

Showing one row only for rows with same order_id in MySQL

Currently i have this query:

SELECT refnumbers.order_id FROM orders INNER JOIN refnumbers ON (refnumbers.order_id = orders.ID)
WHERE refnumbers.refno LIKE '%MAGT-%'

This return 24 rows, from the refnumbers. I would like it to make it group by(?), so it returns only one row, if there's 4 rows with the same order_id.

Example it shows like this now:

OrderID: 2
OrderID: 2
OrderID: 2
OrderID: 3
OrderID: 3

I would like it to return this:

OrderID: 2
OrderID: 3

Upvotes: 1

Views: 55

Answers (1)

user359040
user359040

Reputation:

Simplest answer: add DISTINCT immediately after SELECT.

Note that this is only useful if all you want to return is the order_id. Otherwise (for example, if you want a count of the number of matching refnumbers per order_id) it would be better to add GROUP BY refnumbers.order_id to the end of the query.

Upvotes: 2

Related Questions