I'm a bit confused about DISTINCT keyword. Let's guess that this query will get all the records distincting the columns set in the query:
$query = "SELECT DISTINCT name FROM people";
Now, that query is fetching all the records distincting column "name" and at the same time only fetching "name" column. How I'm supposed to ONLY distinct one column and at the same time get all the desired columns?
This would be the scheme:
NEEDED COLUMNS
- name
- surname
- age
DISTINCTING COLUMNS
- name
What would be the correct sintaxis for that query? Thanks in advance.
DISTINCTis distinct across all columns you fetch. So if two people have the same name and age, but different surname, you will get two rows. What is the expected result here? If my example happens, which surname do you want to get?SELECT DISTINCT(name), surname, age FROM people?name1, surname1, age1andname1, surname2, age1, if you then want distinct onfirstname, which of the two rows would you expect? The one withsurname1orsurname2? And the same question goes in case theagevalue also differs.