I have two tables.
It has 3 fields..
It has 2 fields
My problem is my customer table has 259 rows where as my customer address table has 400+ rows. I mean my customer address table contains duplicate rows.
Now i would like select only unique rows from my customer address table...
Can anyone help me?
Thanks
In the event that the customer_id
and address
are the same for each duplicate, you could use DISTINCT
:
SELECT DISTINCT(`customer_id`), `address` FROM `customer_addresses`;
If a customer has two different addresses in the table, you will receive two results with the above query. To get a single result per customer, you can use GROUP BY
:
SELECT `customer_id`, `address` FROM `customer_addresses` GROUP BY `customer_id`;
This will be guaranteed to return a single result per-customer.
To build upon the use of GROUP BY
, you can also use it to find the customers with duplicate entries:
SELECT `customer_id` FROM `customer_addresses` GROUP BY `customer_id` HAVING COUNT(*) > 1;
This will return only the customer's that have duplicate entries (i.e. - a count of >1) in the customer_addresses
table - which may help you resolve your duplicate problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With