tags:

views:

30

answers:

4

Hi I have a table with names and numbers entered along with other data . The table called events contains many instances where the name and numbers are the same but the other entries are different. I want to perform a search using names so I can display the number. But in my search I only need to return one instance of a name . The table is like this

        Name    Number  Responisble     Position

        Paul    8455    Chorley     t7
        Dave    3821    PR south    f5
        Paul    8455    PR North    p9
        Paul    8455    Leyland     t6
        Dave    3821    Ribbleton   r4

and my script is this

    $condition = "name LIKE 'Paul' ";
    $result = mysql_query("SELECT * FROM events WHERE $condition ")  ;

     while($row = mysql_fetch_array($result)) { 

The script is not complete but I hope you can see that the results would return 3 results but what i want is a result with just one for each name

I hope this makes sense ! thanks for any help

A: 

try using distinct -

SELECT DISTINCT (columns) FROM (table) WHERE (condition)

edit probably disregard this, mis-understood your question i think

MySql return only one instance of duplicate entries

Ross
+1  A: 
$condition = "name LIKE 'Paul' ";
$result = mysql_query("SELECT * FROM events WHERE $condition GROUP BY name")  ;
jatt
A: 

You can either use group by or distinct in the select statement see http://dev.mysql.com/doc/refman/5.1/en/select.html

In your example it would be

$condition = "name LIKE 'Paul' ";
$result = mysql_query("SELECT * FROM events WHERE $condition GROUP BY name")  ;

 while($row = mysql_fetch_array($result)) { 
Obsidian
A: 
SELECT name, number FROM events WHERE $condition GROUP BY name, number
Thomas