我几乎完全重写了这个问题,因为我仍然卡住了......寻找一种方法来查看结果表中的数据,而不必使用 php 或其他一些数据解释器/编程来以我希望的方式查看结果。
基本结果表是这样的(如果你愿意,可以把它弄得更乱):
id1 | id2 | name | ord
------------------------------
a1 | null | name1 | 0
a2 | null | name2 | 1
a3 | null | name3 | 2
b1 | a1 | name4 | 0
b2 | b1 | name5 | 0
b3 | a1 | name6 | 1
b4 | a1 | name7 | 2
c1 | a2 | name8 | 0
c2 | a2 | name9 | 1
d1 | a2 | name10 | 2
d2 | a2 | name11 | 3
d3 | a2 | name12 | 4
d4 | a3 | name13 | 0
c3 | d4 | name14 | 0
c4 | c3 | name15 | 1
d5 | b2 | name16 | 0
我要找的结果是这样的:
id1 | id2 | name | ord
-----------------------------
a1 | null | name1 | 0 <--group header, signified by `id2` is null
b1 | a1 | name4 | 0 <--item that's parent to line below
b2 | b1 | name5 | 0 <--item that's parent to line below
d5 | b2 | name16 | 0 <--last child item
b3 | a1 | name6 | 1 <--special case where `ord` takes over
b4 | a1 | name7 | 2 <--`id2` is still the same, so `ord` sort
a2 | null | name2 | 1 <--next group header
c1 | a2 | name8 | 0 <--`id2` is a2, so name2 is parent, `ord` sort
c2 | a2 | name9 | 1 <--same
d1 | a2 | name10 | 2 <--same
d2 | a2 | name11 | 3 <--same
d3 | a2 | name12 | 4 <--same
a3 | null | name3 | 2 <--next group header
d4 | a3 | name13 | 0 <--`id2` is a3, so name3 is parent
c3 | d4 | name14 | 0 <--`id2` is d3, so name13 is parent
c4 | c3 | name15 | 1 <--`id2` is c3, so name14 is parent
文字说明我需要什么
本质上,我需要 id2 成为按 ord 排序的“组 header ”。然后在它们每个下面,我需要以 id2 等于它上面一行的 id1 以及任何时候 id2 的方式对行进行排序> 与它上面一行的 id2 相同,它应该按 ord 排序。
当 id2 与最后一个 id1 匹配的行用完时,应该出现下一个组标题,并且应该从下一个 id2 重新开始排序 匹配新组头的 id1。
id2 为 null 的任何项目都是组标题,但在这些组中,ord 基于“父”行重新开始。其中一些行也是其他项目的父级,因此排序的复杂性
实际结果表中还有其他列,因此我将其简化为实际进行排序所需的列。
表格数据解释
实际的 id 列是 char(36),name 是 varchar(1024),ord 是 int(11),而表中的其他列运行色域...我还应该提到,这些结果来自 JOIN,这样做是为了限制找到该数据的主表的结果。
id1 是主键并且是唯一的,与排序顺序无关,除了具有 id2 的所有行等于给定的 id1 组标题应该出现在它下面。
id2 是对“parent line id”的引用,因此它对于组标题为空;它们是最父级的行,并且通过 ord 有自己的排序顺序。
name 实际上是组标题行的项目名称,它们是类别名称。
例如,假设组标题是食物组,而其中的其他条目是食物类型。因此,一个组标题将是“水果”,下一个是“蔬菜”等。它实际上是 A/V 设备,但水果可能更容易理解。
我做了什么
我在这方面的尝试无处不在。我尝试了各种order by、field()、if(),我也考虑过子查询,但我的技能还不够最多我自己解决这个问题。我认为,如果我能指出正确的方向,我就可以结合我所拥有的来获得正确的结果。不幸的是,出于沮丧,我删除了所有半工作代码,但它可能只不过是一个带有非工作 CASE 语句的 ORDER BY。
要点:
id2 为 null 时,它是一个组 header ,并且组 header 应按其 ord 字段从低到高(0、1、2)排序。id1 等于另一行的 id2 的任何行都是其父行。换句话说,如果发现一行的 id2 等于另一行的 id1,则应将其放在下面。id2 等于 id1,则排序应首先考虑 #2,然后按 ord 从低到高排序.最佳答案
您描述的数据是分层的(id2 指的是“父级”)并且在 MySQL 中(至少 5.7)没有特定的功能,例如递归 CTE 来处理层次结构。有变通办法,在这里,我们似乎知道我们可以为每个(级别 - 1)使用一个左连接的最大级别数(即,对于 4 个级别,添加 3 个左连接)。一旦通过连接建立了层次结构,然后在各个列中使用 COALESCE(),由于左连接,其中许多列现在可以为 NULL,我们可以安排数据以适合所需的排序顺序。 (好吧,“差不多”。如果您将想要的订单与下面查询显示的订单进行比较,就会发现一些细微差别。)
请引用这个SQL Fiddle还有。
CREATE TABLE Table1
(`id1` varchar(2), `id2` varchar(4), `name` varchar(6), `ord` int)
;
INSERT INTO Table1
(`id1`, `id2`, `name`, `ord`)
VALUES
('a1', NULL, 'name1', 0),
('a2', NULL, 'name2', 1),
('a3', NULL, 'name3', 2),
('b1', 'a1', 'name4', 0),
('b2', 'b1', 'name5', 0),
('b3', 'a1', 'name6', 1),
('b4', 'a1', 'name7', 2),
('c1', 'a2', 'name8', 0),
('c2', 'a2', 'name9', 1),
('d1', 'a2', 'name10', 2),
('d2', 'a2', 'name11', 3),
('d3', 'a2', 'name12', 4),
('d4', 'a3', 'name13', 0),
('c3', 'd4', 'name14', 0),
('c4', 'c3', 'name15', 1),
('d5', 'b2', 'name16', 0)
;
查询:
select
coalesce(p1.id1, p2.id1, p3.id1, p4.id1) id1s
, coalesce(p1.id2, p2.id2, p3.id2, p4.id2) id2s
, coalesce(p1.name, p2.name, p3.name, p4.name) names
, coalesce(p1.ord, p2.ord, p3.ord, p4.ord) ords
#, coalesce(p4.id1, p3.id1, p2.id1, p1.id1) ord1
#, coalesce(p4.id2, p3.id2, p2.id2, p1.id2) ord2
#, coalesce(p4.ord, p3.ord, p2.ord, p1.ord) ord3
from table1 p1
left join table1 p2 on p1.id2 = p2.id1
left join table1 p3 on p2.id2 = p3.id1
left join table1 p4 on p3.id2 = p4.id1
order by
coalesce(p4.id1, p3.id1, p2.id1, p1.id1)
, coalesce(p4.id2, p3.id2, p2.id2, p1.id2)
, coalesce(p4.ord, p3.ord, p2.ord, p1.ord)
, id2s
Result :
| id1s | id2s | names | ords |
|------|--------|--------|------|
| a1 | (null) | name1 | 0 |
| b3 | a1 | name6 | 1 |
| b4 | a1 | name7 | 2 |
| b1 | a1 | name4 | 0 |
| b2 | b1 | name5 | 0 |
| d5 | b2 | name16 | 0 |
| a2 | (null) | name2 | 1 |
| d3 | a2 | name12 | 4 |
| c2 | a2 | name9 | 1 |
| d1 | a2 | name10 | 2 |
| d2 | a2 | name11 | 3 |
| c1 | a2 | name8 | 0 |
| a3 | (null) | name3 | 2 |
| d4 | a3 | name13 | 0 |
| c4 | c3 | name15 | 1 |
| c3 | d4 | name14 | 0 |
想要:
# id1 | id2 | name | ord
# -----------------------------
# a1 | null | name1 | 0 <--group header, signified by `id2` is null
# b1 | a1 | name4 | 0 <--item that's parent to line below
# b2 | b1 | name5 | 0 <--item that's parent to line below
# d5 | b2 | name16 | 0 <--last child item
# b3 | a1 | name6 | 1 <--special case where `ord` takes over
# b4 | a1 | name7 | 2 <--`id2` is still the same, so `ord` sort
# a2 | null | name2 | 1 <--next group header
# c1 | a2 | name8 | 0 <--`id2` is a2, so name2 is parent, `ord` sort
# c2 | a2 | name9 | 1 <--same
# d1 | a2 | name10 | 2 <--same
# d2 | a2 | name11 | 3 <--same
# d3 | a2 | name12 | 4 <--same
# a3 | null | name3 | 2 <--next group header
# d4 | a3 | name13 | 0 <--`id2` is a3, so name3 is parent
# c3 | d4 | name14 | 0 <--`id2` is d3, so name13 is parent
# c4 | c3 | name15 | 1 <--`id2` is c3, so name14 is parent
关于mysql - 复杂的 MySQL 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620300/
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我在加密来self正在使用的第三方供应商的值时遇到问题。他们的指令如下:1)Converttheencryptionpasswordtoabytearray.2)Convertthevaluetobeencryptedtoabytearray.3)Theentirelengthofthearrayisinsertedasthefirstfourbytesontothefrontofthefirstblockoftheresultantbytearraybeforeencryption.4)EncryptthevalueusingAESwith:1.256-bitkeysize,2.25
我正在开发西洋跳棋实现,其中有许多易于测试的方法,但我不确定如何测试我的主要#play_game方法。我的大多数方法都可以很容易地确定输入和输出,因此也很容易测试,但这种方法是多方面的,实际上并没有容易辨别的输出。这是代码:defplay_gameputs@gui.introwhile(game_over?==false)message=nil@gui.render_board(@board)@gui.move_requestplayer_input=getscoordinates=UserInput.translate_move_request_to_coordinates(play
我需要用任何语言编写一个算法,根据3个因素对数组进行排序。我以度假村为例(如Hipmunk)。假设我想去度假。我想要最便宜的地方、最好的评论和最多的景点。但是,显然我找不到在所有3个中都排名第一的方法。Example(assumingthereare20importantattractions):ResortA:$150/night...98/100infavorablereviews...18of20attractionsResortB:$99/night...85/100infavorablereviews...12of20attractionsResortC:$120/night
我正在尝试按Rails相关模型中的字段进行排序。我研究的所有解决方案都没有解决如果相关模型被另一个参数过滤?元素模型classItem相关模型:classPriority我正在使用where子句检索项目:@items=Item.where('company_id=?andapproved=?',@company.id,true).all我需要按相关表格中的“位置”列进行排序。问题在于,在优先级模型中,一个项目可能会被多家公司列出。因此,这些职位取决于他们拥有的company_id。当我显示项目时,它是针对一个公司的,按公司内的职位排序。完成此任务的正确方法是什么?感谢您的帮助。PS-我
方法应返回-1,0或1分别表示“小于”、“等于”和“大于”。对于某些类型的可排序对象,通常将排序顺序基于多个属性。以下是可行的,但我认为它看起来很笨拙:classLeagueStatsattr_accessor:points,:goal_diffdefinitializepts,gd@points=pts@goal_diff=gdenddefothercompare_pts=pointsother.pointsreturncompare_ptsunlesscompare_pts==0goal_diffother.goal_diffendend尝试一下:[LeagueStats.new(
我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin
我正在构建一个小部件来显示奥运会的奖牌数。我有一个“国家”对象的集合,其中每个对象都有一个“名称”属性,以及奖牌计数的“金”、“银”、“铜”。列表应该排序:1.首先是奖牌总数2.如果奖牌相同,按类型分割(金>银>铜,即2金>1金+1银)3.如果奖牌和类型相同,则按字母顺序子排序我正在用ruby做这件事,但我想语言并不重要。我确实找到了一个解决方案,但如果感觉必须有更优雅的方法来实现它。这是我做的:使用加权奖牌总数创建一个虚拟属性。因此,如果他们有2个金牌和1个银牌,加权总数将为“3.020100”。1金1银1铜为“3.010101”由于我们希望将奖牌数排序为最高的,因此列表按降序排
我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi
例如,假设我有一个名为Products的模型,并且在ProductsController中,我有以下代码用于product_listView以显示已排序的产品。@products=Product.order(params[:order_by])让我们想象一下,在product_listView中,用户可以使用下拉菜单按价格、评级、重量等进行排序。数据库中的产品不会经常更改。我很难理解的是,每次用户选择新的order_by过滤器时,rails是否必须查询,或者rails是否能够以某种方式缓存事件记录以在服务器端重新排序?有没有一种方法可以编写它,以便在用户排序时rails不会重新查询结果