mysql group by 大小写_MySQL - 边界大小写使用GROUP BY

bd96500e110b49cbb3cd949968f18be7.png

My understanding of GROUP BY is that its standard use is to aggregate items. So a typical example might be:

select

count(id),

department,

from table

group by department

The above would a count of all id's per department.

So, I got taught a very useful (but possible pretty dodgy!) trick using group by. I was wondering if this usage has any problems. Although the query runs as expected [results as expected in all cases], my spidey sense is tingling a bit...

Imagine the following data set:

id | user_id | cost | note

----------------------------------

1 1 120 Test 1

2 1 150 Test 2

3 2 100 Test 3

4 3 120 Test 4

Now if we do the following SQL:

select * from table

group by user_id

You get the following result set.

id | user_id | cost | note

----------------------------------

1 1 120 Test 1

3 2 100 Test 3

4 3 120 Test 4

The query runs apparently as follows:

run through the table

when a groupable user id is found, ignore the subsequent ones

return this table of unique user_id items

Effectively I get a "unique", with specific boundaries and I am able to select * from this list.

Furthermore, by ordering the table prior to the order by, I can use this to filter all costs.

So - this is also as you'd expect.. BUT:

In the ABOVE example - Say I actually ensured that for user_id 1, the value 120 was shown (as opposed to it's other possible values - 150 in this case). Then 120 seems to be guaranteed to be the response.

The approach could be then to sort by some order alphabetical/numeric/other advanced filters etc... THEN use this sort to force the first item in the table to be the "answer".

The actual query I want to do is pretty complex. Using MIN or similar are not suitable for the end value I want... However: this "order your table then take the first unique item using group by" approach is actually quite elegant (I think).

I am actually using group by constrained across 4 fields, and this, combined with other SQL makes a CORRECT answer.

So. After that long background: a question!

All documentation I have used only talks about using group by with aggregate functions. I can't seem to find the behaviour of JUST group by.

This strikes me as one of two things:

a correct (mis)use case that's not been documented

an accidental behaviour of whichever version of mySQL I'm using.

So... which one is it?

If it's a correct, but edge case, behaviour, then great. If I'm tricking the SQL engine to spit something out, then I've got no proof this is compatible with future versions so I'd be uneasy to use it.

Cheers in advance all.

解决方案

After looking into this through the above links/help, I think it's unfortunately the case that: while the answer is correct, it's not guaranteed to be correct... More accurately it is "indeterminate".

I am genuinely confident following my repeated successful use of this that the internal workings are "first come first show", but as the spec also says this isn't guaranteed so I can't rely on it.

Cheers for help all. Have up-voted all comments.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值