0

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

4 Answers 4

3
$condition = "name LIKE 'Paul' ";
$result = mysql_query("SELECT * FROM events WHERE $condition GROUP BY name")  ;
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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)) { 

Comments

0
SELECT name, number FROM events WHERE $condition GROUP BY name, number

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.