Mysql single line or single field

Is there a difference in performance or another:

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 ... 

the result set is used as part of the insert statement, and select may return the same user id several times due to the connection (not displayed here)

I am using mysql.

+4
source share
2 answers

There is no such thing as a โ€œseparate fieldโ€. Your first syntax is parsed identically to the second: parentheses simply surround the field expression. You can also write:

 SELECT DISTINCT (users.id), (2) AS someFieldName, (0) AS someOtherFieldName 

All of them are equivalent.

If you want to avoid comparisons with your constant columns, you can use GROUP BY instead:

 SELECT users.id, 2 AS someFieldName, 0 AS someOtherFieldName FROM users JOIN ... GROUP BY users.id 
+4
source

There is no difference, since DISTINCT in this context only works with strings.

From SELECT docs

The ALL and DISTINCT options determine whether duplicate rows should be returned. ALL (default) indicates that all matching rows should be returned, including duplicates. DISTINCT indicates the deletion of duplicate rows from the result set. Error specified as options. DISTINCTROW is a synonym for DISTINCT.

+3
source

Source: https://habr.com/ru/post/1441748/


All Articles