如何写成MYSQL的语句
谢谢 有示例
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Sys_SecurityMenu]') AND type in (N'U'))
DROP TABLE [dbo].[Sys_SecurityMenu]
PRINT '添加菜单表'
CREATE TABLE [dbo].[Sys_SecurityMenu](
[Guid] [nvarchar](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
[Version] [int] NOT NULL,
[Status] [int] NOT NULL,
[SortNumber] [int] NOT NULL,
[Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[PageUrl] [nvarchar](200) COLLATE Chinese_PRC_CI_AS NULL,
[ImageUrl] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Type] [int] NOT NULL,
[SecurityMenuGuid] [nvarchar](32) COLLATE Chinese_PRC_CI_AS NULL,
[Depth] [int] NOT NULL,
[Description] [nvarchar](500) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_SYS_SECURITYMENU] PRIMARY KEY CLUSTERED
(
[Guid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
PRINT '插入菜单数据'
INSERT INTO Sys_SecurityMenu ([Guid], [Version], [Status], [SortNumber], [Name], [PageUrl], [ImageUrl], [Type], [SecurityMenuGuid], [Depth], [Description]) VALUES (N'1c8e7b550fe54787b28cf52c56506223', 9, 1, 100, N'录制', null, null, 1, null, 1, null)
INSERT INTO Sys_SecurityMenu ([Guid], [Version], [Status], [SortNumber], [Name], [PageUrl], [ImageUrl], [Type], [SecurityMenuGuid], [Depth], [Description]) VALUES (N'2f867dcc7b954a61bd23f9144fabf20d', 7, 1, 2, N'用户', null, null, 1, null, 1, null)
INSERT INTO Sys_SecurityMenu ([Guid], [Version], [Status], [SortNumber], [Name], [PageUrl], [ImageUrl], [Type], [SecurityMenuGuid], [Depth], [Description]) VALUES (N'6383916a899644b2a11f110ef6b95ab3', 8, 1, 1, N'系统工具', null, null, 1, null, 1, null)
INSERT INTO Sys_SecurityMenu ([Guid], [Version], [Status], [SortNumber], [Name], [PageUrl], [ImageUrl], [Type], [SecurityMenuGuid], [Depth], [Description]) VALUES (N'7c5741c874ad46a28084bc470a97cb88', 6, 1, 5, N'视频', null, null, 1, null, 1, null)
INSERT INTO Sys_SecurityMenu ([Guid], [Version], [Status], [SortNumber], [Name], [PageUrl], [ImageUrl], [Type], [SecurityMenuGuid], [Depth], [Description]) VALUES (N'8e33e51d8234423bb9bc8f467f03498d', 7, 1, 5, N'会议', null, null, 1, null, 1, null)
GO
PRINT '查询出当前列表按SortNumber升序排序'
SELECT * FROM Sys_SecurityMenu ORDER BY SortNumber ASC,Guid
GO
DECLARE @t TABLE(rk int);
WITH cte
AS (SELECT rank()
OVER(ORDER BY SortNumber ASC,Guid) rk,
*
FROM Sys_SecurityMenu
WHERE 1 = 1
AND (Guid <> '2f867dcc7b954a61bd23f9144fabf20d' /* ?p0 */))
--SELECT * FROM cte --Select 了的话后面不能用cte不知道为什么,这里就是建立一个排序后的临时表
--更新记录的SourtNumber为排序号,如果排序号大于2f867dcc7b954a61bd23f9144fabf20d的排序号的话(这里为2),那么排序号+1
UPDATE t
SET SortNumber = (CASE WHEN r.rk >= 2 THEN r.rk + 1 ELSE r.rk END)
output inserted.SortNumber into @t
FROM Sys_SecurityMenu t
INNER JOIN cte r
ON t.Guid = r.Guid
WHERE 1 = 1
AND (t.Guid <> '2f867dcc7b954a61bd23f9144fabf20d' /* ?p1 */)
SELECT TOP 1 rk
FROM @t
ORDER BY rk DESC
GO
PRINT '将更新后的表按SortNumber升序排序'
SELECT * FROM Sys_SecurityMenu ORDER BY SortNumber ASC
GO
9 个解决方案
#1
自己动手做一下吧,有问题再问,
贴建立MYSQL表及插入记录的SQL语句、要求结果出来看看
贴建立MYSQL表及插入记录的SQL语句、要求结果出来看看
#2
以下是MYSQL的建表和插入数据的语句,
但后面的批量排序更新就不知道怎么写了
谢谢
要实现的是和MSSQL一样的排序更新
谢谢
但后面的批量排序更新就不知道怎么写了
谢谢
要实现的是和MSSQL一样的排序更新
谢谢
DROP TABLE IF EXISTS `sys_securitymenu`;
CREATE TABLE `sys_securitymenu` (
`Guid` varchar(32) NOT NULL,
`Version` int(11) NOT NULL,
`Status` int(11) NOT NULL,
`SortNumber` int(11) NOT NULL,
`Name` varchar(50) NOT NULL,
`PageUrl` varchar(200) DEFAULT NULL,
`ImageUrl` varchar(50) DEFAULT NULL,
`Type` int(11) NOT NULL,
`SecurityMenuGuid` varchar(32) DEFAULT NULL,
`Depth` int(11) NOT NULL,
`Description` varchar(500) DEFAULT NULL,
PRIMARY KEY (`Guid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'1c8e7b550fe54787b28cf52c56506223', 9, 1, 100, N'录制', null, null, 1, null, 1, null) ;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'2f867dcc7b954a61bd23f9144fabf20d', 7, 1, 2, N'用户', null, null, 1, null, 1, null) ;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'6383916a899644b2a11f110ef6b95ab3', 8, 1, 1, N'系统工具', null, null, 1, null, 1, null) ;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'7c5741c874ad46a28084bc470a97cb88', 6, 1, 5, N'视频', null, null, 1, null, 1, null) ;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'8e33e51d8234423bb9bc8f467f03498d', 7, 1, 5, N'会议', null, null, 1, null, 1, null);
SELECT * FROM Sys_SecurityMenu ORDER BY SortNumber ASC,Guid
#3
用你上述数据,要求结果是什么
#4
mysql没有递归查询的函数 只能自己写相应的存储过程
#5
没有用过MYSQL,麻烦说一下怎么写可以吗?
谢谢
#6
要求的结果是和MSSQL一样
要对表sys_securitymenu的SortNumber按1,2,3,4,5,6,..顺序进行排序
条件为Guid不为2f867dcc7b954a61bd23f9144fabf20d,当排序号大于2的时候自动加1
结果为
谢谢
#7
排序顺序是否固定,即系统工具、用户、视频、会议、录制这样排序?
#8
按
SELECT * FROM Sys_SecurityMenu ORDER BY SortNumber ASC,Guid
先排出来
如:
上面的图是未排序的
下面的图为要求的排序结果
SELECT * FROM Sys_SecurityMenu ORDER BY SortNumber ASC,Guid
先排出来
如:
上面的图是未排序的
下面的图为要求的排序结果
#9
SET @a=0;
UPDATE `sys_securitymenu` a INNER JOIN
(SELECT *,@a:=@a+1 AS na FROM `sys_securitymenu` ORDER BY sortnumber) b
ON a.`Guid`=b.guid
SET a.`SortNumber`=b.na
;
UPDATE `sys_securitymenu` a INNER JOIN
(SELECT *,@a:=@a+1 AS na FROM `sys_securitymenu` ORDER BY sortnumber) b
ON a.`Guid`=b.guid
SET a.`SortNumber`=b.na
;
#1
自己动手做一下吧,有问题再问,
贴建立MYSQL表及插入记录的SQL语句、要求结果出来看看
贴建立MYSQL表及插入记录的SQL语句、要求结果出来看看
#2
以下是MYSQL的建表和插入数据的语句,
但后面的批量排序更新就不知道怎么写了
谢谢
要实现的是和MSSQL一样的排序更新
谢谢
但后面的批量排序更新就不知道怎么写了
谢谢
要实现的是和MSSQL一样的排序更新
谢谢
DROP TABLE IF EXISTS `sys_securitymenu`;
CREATE TABLE `sys_securitymenu` (
`Guid` varchar(32) NOT NULL,
`Version` int(11) NOT NULL,
`Status` int(11) NOT NULL,
`SortNumber` int(11) NOT NULL,
`Name` varchar(50) NOT NULL,
`PageUrl` varchar(200) DEFAULT NULL,
`ImageUrl` varchar(50) DEFAULT NULL,
`Type` int(11) NOT NULL,
`SecurityMenuGuid` varchar(32) DEFAULT NULL,
`Depth` int(11) NOT NULL,
`Description` varchar(500) DEFAULT NULL,
PRIMARY KEY (`Guid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'1c8e7b550fe54787b28cf52c56506223', 9, 1, 100, N'录制', null, null, 1, null, 1, null) ;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'2f867dcc7b954a61bd23f9144fabf20d', 7, 1, 2, N'用户', null, null, 1, null, 1, null) ;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'6383916a899644b2a11f110ef6b95ab3', 8, 1, 1, N'系统工具', null, null, 1, null, 1, null) ;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'7c5741c874ad46a28084bc470a97cb88', 6, 1, 5, N'视频', null, null, 1, null, 1, null) ;
INSERT INTO Sys_SecurityMenu (Guid, Version, Status, SortNumber, Name, PageUrl, ImageUrl, Type, SecurityMenuGuid, Depth, Description) VALUES (N'8e33e51d8234423bb9bc8f467f03498d', 7, 1, 5, N'会议', null, null, 1, null, 1, null);
SELECT * FROM Sys_SecurityMenu ORDER BY SortNumber ASC,Guid
#3
用你上述数据,要求结果是什么
#4
mysql没有递归查询的函数 只能自己写相应的存储过程
#5
没有用过MYSQL,麻烦说一下怎么写可以吗?
谢谢
#6
要求的结果是和MSSQL一样
要对表sys_securitymenu的SortNumber按1,2,3,4,5,6,..顺序进行排序
条件为Guid不为2f867dcc7b954a61bd23f9144fabf20d,当排序号大于2的时候自动加1
结果为
谢谢
#7
排序顺序是否固定,即系统工具、用户、视频、会议、录制这样排序?
#8
按
SELECT * FROM Sys_SecurityMenu ORDER BY SortNumber ASC,Guid
先排出来
如:
上面的图是未排序的
下面的图为要求的排序结果
SELECT * FROM Sys_SecurityMenu ORDER BY SortNumber ASC,Guid
先排出来
如:
上面的图是未排序的
下面的图为要求的排序结果
#9
SET @a=0;
UPDATE `sys_securitymenu` a INNER JOIN
(SELECT *,@a:=@a+1 AS na FROM `sys_securitymenu` ORDER BY sortnumber) b
ON a.`Guid`=b.guid
SET a.`SortNumber`=b.na
;
UPDATE `sys_securitymenu` a INNER JOIN
(SELECT *,@a:=@a+1 AS na FROM `sys_securitymenu` ORDER BY sortnumber) b
ON a.`Guid`=b.guid
SET a.`SortNumber`=b.na
;