Forum Moderators: open
eg, I wan select distinct customer name and id from the customer table. one customer may have different cus_ids since cus_ids are auto increment and depend on the purchased items. so what I want is to select distinct customer name so that I can print customer name and customer id once.
code is here:
select distinct cus_name, cus_id from customers order by cus_name asc
It's returning distinct rows - that is, distinct combinations of customer names and ids. In order to get what you want, you have to decide which customer id you want returned - the first or the last. The following query should work for you:
SELECT cus_name, max(cus_id) as cus_id FROM customers GROUP BY cus_name ORDER BY cus_name asc
This will return the last cus_id - if you want the first one, change max to min. Hope this helps!
Chad