B
B
beduin012016-02-15 16:39:30
MySQL
beduin01, 2016-02-15 16:39:30

How does group by work?

Question. group by selects unique values. Ie, for example, if I need to select all unique urls, I will write ... group by url
Question. What is the default sort order? That is, records with which id will be included in the selection.
id url
1 /foo
2 /bar
3 /foo
4 /baz
in this case 1 or 3 will be in the selection?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aleksej, 2016-02-15
@Shwed_Berlin

GROUP BY has nothing to do with uniqueness - it's grouping.
If you specify only the second field in GROUP BY, and want to get id in SELECT, you will get an error.
All fields used in SELECT must be either grouped or aggregated (SUM, MIN, etc.)

S
s0ci0pat, 2016-02-16
@s0ci0pat

Aleksej : beduin01 : sorry for the late reply)
MySQL usually outputs the first value
676dbcfb707c45ff80caec8ab5ec95b1.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question