Forum Moderators: open

Message Too Old, No Replies

How to use GROUP BY on two fields?

MySQL / PHP

         

TheVanWarehouse

3:56 pm on Jul 21, 2009 (gmt 0)

10+ Year Member



Hi,

I have a MySQL table with fields including Telephone and Email.

I want to do a query that will return all unqiue records where if there are 2+ plus records that have the same telephone (or the same email, or the same email + telephone) only one of the records will be returned (doesn't matter which one).

I can't use DISTINCT as I need to return other fields too.

I tried using GROUP BY (GROUP BY telephone, email) however this seems to be looking at both fields combined and comparing them - hence its still returning records that have the same telephone (e.g. the same telephone but different email - not what I'm looking for).

Can anyone suggest the correct query to use? Thanks

Adastra

8:18 am on Jul 22, 2009 (gmt 0)

10+ Year Member



Can you post your query so I can help you.

TheVanWarehouse

8:29 am on Jul 22, 2009 (gmt 0)

10+ Year Member



$sql_query = "SELECT * FROM table1 WHERE ((table1.email_address!='' && table1.email_address!='-') ¦¦ (table1.telephone!='' && table1.telephone!='-')) GROUP BY table1.email_address, table1.telephone";