Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicates from drop down list

Tags:

sql

php

I'm using the code below to return a list of LastNames into a form's drop down menu.

<?php
    include 'conn.inc.php';
    $sql_dropdown_lastname = "SELECT LastName FROM Individuals";
    $sql_run_lastname = odbc_exec($conn_general, $sql_dropdown_lastname);
        echo "<table><form action='index.php' method='POST'><tr><td>Individual Last Name</td><td><select name='IndivSurname'>";
            while($lastname_row = odbc_fetch_array($sql_run_lastname)){
                $AllLastName=$lastname_row['LastName'];
                    echo"<option value='$AllLastName'>$AllLastName</option>";
            }

        echo"</select></td>
                </tr>
                <tr>
                    <td><input type='submit' value='submit' name='submit'></td>
                </tr>
                </form>
        </table>";

?>

However several entries are duplicate. How would I be able to eliminate any duplicates from the drop down list ?

Thanks in advance, J

like image 990
joebegborg07 Avatar asked Feb 02 '26 17:02

joebegborg07


1 Answers

If they are truly duplicates (and not just people with the same last name), you can do it like this:

SELECT DISTINCT LastName FROM Individuals

Note, if they are people who are different, but have the same last name, you'd want to bring in another field in your query, such as this:

SELECT DISTINCT LastName, FirstName FROM Individuals
like image 116
nomistic Avatar answered Feb 05 '26 06:02

nomistic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!