Is there a performance or other difference between this:
SELECT distinct(users.id),2 as someFieldName ,0 as someOtherFieldName
From users join ...
and this:
SELECT distinct users.id ,2 as someFieldName ,0 as someOtherFieldName
From users join ...
result set is used as part of an insert statement and the select might return the same user i.d multiple time due to the join (not displayed here)
I am using mysql.
There is no difference as DISTINCT
in this context only operates on rows.
From the SELECT docs
The ALL and DISTINCT options specify whether duplicate rows should be returned. ALL (the default) specifies that all matching rows should be returned, including duplicates. DISTINCT specifies removal of duplicate rows from the result set. It is an error to specify both options. DISTINCTROW is a synonym for DISTINCT.
There is no such thing as "distinct field". Your first syntax is parsed identically to the second: the parentheses merely surround a field expression. You could also write:
SELECT DISTINCT (users.id), (2) AS someFieldName, (0) AS someOtherFieldName
They are all equivalent.
If you want to avoid comparisons on your constant columns, you could instead use GROUP BY
:
SELECT users.id, 2 AS someFieldName, 0 AS someOtherFieldName
FROM users JOIN ...
GROUP BY users.id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With