简体   繁体   English

如何按两列对MySQL查询进行排序

[英]How to sort MySQL query by two columns

The following is my DB data, how can I sorting it by sid and prev_sid with php/mysql!? 以下是我的数据库数据,如何使用php / mysql通过sid和prev_sid对其进行排序!

sid prev_sid    type
000 197         app_home    
197 198         page_teach  
198 218         page_teach  
199 211         page_step   
211 207         link        
218 559         page_step   
559 199         page_step

Result: 结果:

sid prev_sid    type
000 197         app_home
197 198         page_teach
198 218         page_teach
218 559         page_step
559 199         page_step
199 211         page_step
211 207         link

000 --> 197 --> 198 --> 218 --> 559 --> 199 --> 199 --> 211 --> 207 000-> 197-> 198-> 218-> 559-> 199-> 199-> 211-> 207

I would suggest that you reconsider your table design. 我建议您重新考虑表格设计。 This type of data representation prevents the rows from being sorted conveniently and efficiently. 这种类型的数据表示法阻止对行进行方便且有效的排序。

If you need to keep the current table design, I suspect that you will need to involve SQL variables, and that the sorting select becomes quite messy and probably not efficient. 如果您需要保持当前的表设计,我怀疑您将需要包含SQL变量,并且排序选择会变得非常混乱并且可能效率不高。

Another, possibly better solution, would be to do the sorting on the application side, at which point it could easily be done in linear time using a hash-map, mapping sid-values to pairs of prev_sid and type values. 另一个可能更好的解决方案是在应用程序端进行排序,这时可以使用哈希映射在线性时间内轻松完成排序,将sid值映射到prev_sid和类型值对。

It looks like your data is an adjacency list (ie sed and prev_sid define a recursive parent-child relationship). 看来您的数据是一个邻接表(即sed和prev_sid定义了递归的父子关系)。 If this is the case, then to get the items in order you could convert it to a nested set and sort by the left value of every node to get the items in order. 如果是这种情况,则要按顺序获取项目,可以将其转换为嵌套集,然后按每个节点的左值排序以按顺序获取项目。 For more information on hierarchical data refer to Managing Hierarchical Data in MySQL . 有关分层数据的更多信息,请参阅在MySQL中管理分层数据

BTW, if you want to convert an adjacency list to a nested set using PHP check out my answer at PHP Moving mySQL Tree Node . 顺便说一句,如果您想使用PHP将邻接表转换为嵌套集,请在PHP Moving mySQL Tree Node上查看我的答案。

If you look carefully at your data, you will see that you only need to sort by sid . 如果仔细查看数据,您将看到只需要按sid进行排序。 This is done in SQL by appending ORDER BY sid to your query. 这是通过在SQL中将ORDER BY sid附加到查询中来完成的。

SELECT sid, prev_sid, type FROM table ORDER BY sid

If you need to sort on two columns (which is not the case here but it might be useful), ORDER BY can take a list of columns as parameter. 如果需要对两列进行排序(此处不是这种情况,但是可能有用), ORDER BY可以将列列表作为参数。

SELECT sid, prev_sid, type FROM table ORDER BY sid, prev_sid

218 > 559 > 199 218> 559> 199

Not using any numbering system I've ever used it's not. 没有使用任何我从未使用过的编号系统。 I don't think you've explained your problem very well. 我认为您没有很好地解释您的问题。

Looking at the example I suspect you want to sort based on either the value for sid or the value for perv_sid, depending on which is the lesser, so: 查看示例,我怀疑您想基于sid的值还是perv_sid的值进行排序,具体取决于哪一个较小,所以:

ORDER BY LEAST(sid, prev_sid) 最少订购(sid,prev_sid)

C. C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM