SQL select distinct but return all columns?

SQL select distinct but return all columns?

Here is an example that works.

This is using “group by” instead of “distinct”.

Here unique is location:

1
2
$requestSQL = mysqli_query($connection, "SELECT state, location from postal_code where state
 = '$state_sql' group by location order by state ASC LIMIT 100");

Another example if you want to echo the id.

Here unique is name:

2
3
4
5
$requestSQL = mysqli_query($connection, "SELECT max(id) as max, name, state, location,
 MIN(id) FROM postal_codes where location = '$location_sql' and state = '$state_sql'
 group by name order by name ASC LIMIT 50");
while {}

to echo the id, use:

echo $row[‘max’];

Add a comment: