SQL Order By 查询问题

发布于 2024-11-09 06:43:24 字数 414 浏览 0 评论 0原文

我有一个包含以下数据集的表,

id   name  parent  
------------------
1    xyz   NULL  
2    def   NULL  
3    mno   abc  
4    abc   NULL  
5    pqr   abc  
6    qfx   def

我希望按以下顺序获取数据:

1    abc   NULL  
3    mno   abc  
5    pqr   abc  
2    def   NULL  
6    qfx   def  
4    xyz   NULL

我的意思是必须按用户名进行排序,但具有相同父级的记录必须一起显示,并且位于父级记录的下方。

我正在使用MySQL。

I have a table with following dataset

id   name  parent  
------------------
1    xyz   NULL  
2    def   NULL  
3    mno   abc  
4    abc   NULL  
5    pqr   abc  
6    qfx   def

I would want to fetch the data in the following order:

1    abc   NULL  
3    mno   abc  
5    pqr   abc  
2    def   NULL  
6    qfx   def  
4    xyz   NULL

What I mean is ORDERing has to occur by Username but records with same parent must show together and just below the record for the parent.

I am using MySQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

戴着白色围巾的女孩 2024-11-16 06:43:24

尝试这个新答案:

SELECT * 
FROM table t
ORDER BY
    CONCAT(parent, name)

它连接了两个字符串,因此它的顺序应该如下所示:

abc
abcmno
abcpqr
def
defqfx
xyz

try this new answer:

SELECT * 
FROM table t
ORDER BY
    CONCAT(parent, name)

it concatenated the two strings so it should order like this:

abc
abcmno
abcpqr
def
defqfx
xyz
调妓 2024-11-16 06:43:24

像这样的东西(不一定完全这样):

SELECT *
FROM atable
ORDER BY COALESCE(CONCAT(parent, '.'), '') + name

我实际上会尝试用不太可能出现在名称中的字符替换'.'

Something like this (not necessarily exactly like this):

SELECT *
FROM atable
ORDER BY COALESCE(CONCAT(parent, '.'), '') + name

I would actually try to replace '.' with a character that is unlikely to appear in the names.

浮云落日 2024-11-16 06:43:24

使用以下查询:

Select * from table_name
    Order by
        name, parent;

Use the below query:

Select * from table_name
    Order by
        name, parent;
彼岸花ソ最美的依靠 2024-11-16 06:43:24

这根本不是问题吗?抓住它:

select * from mytable order by parent, name

it's not a problem at all? catch it:

select * from mytable order by parent, name
筱武穆 2024-11-16 06:43:24

你遇到的最大问题是你想要两个截然不同的方面。您希望它按用户名顺序排列,但如果有匹配的父项,您希望将其分组。

最明显的是

select username, parent from mytable
order by parent,username
group by parent

但这仍然首先按父级排序。

我确信有可能得到你想要的东西,但是,我还没有喝咖啡来制定一个简单的完整解决方案。

The biggest problem you have is you want 2 rather different aspects. You want it in username order, but if there is a matching parent you want to group it.

The most obvious would be

select username, parent from mytable
order by parent,username
group by parent

But that still sorts it by parent first.

Im sure its possible to get exactly what you wanted, but, I havent had the caffine yet to work out an easy full solution.

别挽留 2024-11-16 06:43:24

在 Oracle 中,我发现有必要编写存储过程来进行分层排序。一些 RDBMS 提供 CTE(通用表表达式),可以达到相同的结果。我不熟悉 mySql,也不知道它有什么支持,但以下内容可能会有所帮助。

维基百科文章

mySql 特定

另外 Google“Joe Celko 树和层次结构”

Other in Oracle I have found it necessary to write Stored Proc to do hierarchical ordering. Some RDBMSes offer CTE (common table expressions) which can acheive the same result. I am not familiar with mySql and don't know what support it has but the following may help.

Wikipedia article

mySql specific

Also Google "Joe Celko Trees and Hierarchies"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文