Mysql 连接记录[SQL]
我有一张桌子“桌子” 具有相同合并的记录,需要按下 合并顺序是特定顺序(在一次合并中) 等等 select中的输出将是,例如:Mysql 连接记录[SQL],mysql,sql,Mysql,Sql,我有一张桌子“桌子” 具有相同合并的记录,需要按下 合并顺序是特定顺序(在一次合并中) 等等 select中的输出将是,例如: ID654323 , 200 , 1 , Maggie ID654321 , 200 , 2 , Deril ID123456 , 400 , 1 , Coral ID654322 , 400 , 2 , Rick 这是我的选择 SELECT MERGE_ORDER,ID_DOCUMENT,NAME,MERGE FROM(select * from Table
ID654323 , 200 , 1 , Maggie
ID654321 , 200 , 2 , Deril
ID123456 , 400 , 1 , Coral
ID654322 , 400 , 2 , Rick
这是我的选择
SELECT
MERGE_ORDER,ID_DOCUMENT,NAME,MERGE
FROM(select * from Table order by name)-trying to order table before main select
WHERE MERGE IS NOT NULL
CONNECT BY NOCYCLE PRIOR MERGE = MERGE
order SIBLINGS by MERGE_ORDER,NAME)
select可以工作,但现在我需要按名称对结果进行排序,但需要保存记录连接(合并)
我需要对merge\u order=1
的行进行排序。但在有序行下,放置具有相同合并的第二条记录,其中merge\u order=2
ID654323 , 200 , 1 , Maggie
ID123456 , 400 , 1 , Coral
我将通过一个简单的自连接来实现这一点:
select t1.*
from table t1
inner join table t2 on t1.merge=t2.merge --self join
where t2.merge_order=1 --relate the 1st of each merge to each record in the merge
order by t2.name ASC, t1.merge_order ASC --the name in t2 will be the name from the 1st record, see where clause
您也可以使用CONNECTBY来完成,但是,您还没有更新产品标签,mysql也没有CONNECTBY子句。因此,我认为connect by子句不是有效的解决方案。您确定这个问题与mysql有关,而与oracle或其他rdbms产品无关吗?我不知道mysql中使用了connect by子句。只需按2,3或列名排序即可
ID654323 , 200 , 1 , Maggie
ID123456 , 400 , 1 , Coral
select t1.*
from table t1
inner join table t2 on t1.merge=t2.merge --self join
where t2.merge_order=1 --relate the 1st of each merge to each record in the merge
order by t2.name ASC, t1.merge_order ASC --the name in t2 will be the name from the 1st record, see where clause