1
Assuming the Users.Id field is unique auto incremented MY MAIN QUESTION: Is "Distinct", in this query, doing anything, is it completely pointless having it in this specific query?
SELECT DISTINCT users.id, CONCAT(users.firstname, ' ', users.surname) AS name FROM users
WHERE users.id!=$me AND (LOWER(users.firstname) LIKE '$term%' AND (email='$email' OR username='$email'))
-
If the query was changed to:
SELECT DISTINCT users.surname, firstname, surname
FROM users WHERE surname = 'Smith''
Then I might only get Jane Smith or John Smith as my result but not both of them or anyone else with the name Smith, Correct?
(The Top query is the original)
Yes, that is correct, if you change to use
– Pimp Juice IT – 2018-03-21T00:49:04.317DISTINCT users.surname, firstname, surname
then you can be assured that you will only get distinct values for those selected fields. If you have Jane Smith and John Smith, both of those records will still be listed. If you have 2 or more John Smith's though for example, you would only get the one distinct record for John Smith, not both so usingDISTINCT
can be used for one or more fields ensuring those field records are not duplicate values (i.e. distinct) where all values are the same. https://www.w3schools.com/sql/sql_distinct.aspSo what you are saying is: "all selected fields are used in the distinct". So in the first query the id field would make the distinct useless because every id is unique? and in the second query 2 John Smiths would be returned as one result , but John and Jane would be returned as 2 result? – NZ Dev – 2018-03-21T01:04:29.680
This sounds correct to me but you can easily test this to confirm, correct? I am almost certain that is the same so give it a test run and confirm that, that's how I recall it working the last I used it but you can easily test and confirm with a simple test select query. – Pimp Juice IT – 2018-03-21T01:12:53.350