在列上使用distinct,并在另一列上执行排序会产生错误 [英] Using distinct on a column and doing order by on another column gives an error

查看:122
本文介绍了在列上使用distinct,并在另一列上执行排序会产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子: abc_test的列为n_num,k_str.

I have a table: abc_test with columns n_num, k_str.

此查询无效:

    select distinct(n_num) from abc_test order by(k_str)

但这一项有效:

    select n_num from abc_test order by(k_str)

DISTINCT和ORDER BY关键字在内部如何工作以使两个查询的输出都被更改?

How do DISTINCT and ORDER BY keywords work internally that output of both the queries is changed?

推荐答案

据我从您的问题中了解.

As far as i understood from your question .

distinct:-表示选择一个distinct(所有选择的值都应该是唯一的). order By:-只是意味着根据您的要求对选定的行进行排序.

distinct :- means select a distinct(all selected values should be unique). order By :- simply means to order the selected rows as per your requirement .

您的第一个查询中的问题是 例如 : 我有一张桌子

The problem in your first query is For example : I have a table

ID name
01 a
02 b
03 c
04 d 
04 a

现在查询select distinct(ID) from table order by (name)混淆了ID-04应该记录的记录(因为那里有两个值d和Name列中的a).所以当您说数据库引擎的问题就在这里 按(名称)排序.........

now the query select distinct(ID) from table order by (name) is confused which record it should take for ID - 04 (since two values are there,d and a in Name column). So the problem for the DB engine is here when you say order by (name).........

这篇关于在列上使用distinct,并在另一列上执行排序会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆