我想交换表中两行的值。我有两行的行ID。有没有查询呢?这是一个例子。在查询之前我有这个:
row1 : 1,2,3 row2 : 5,6,7
交换后我想要这个:
row1 : 5,6,7 row2 : 1,2,3
答案 0 :(得分:8)
如果要将两个已知ID的值从一行交换到另一行,请尝试以下操作:
--need to store the original values
SELECT
*,CASE WHEN id=123 then 987 ELSE 123 END AS JoinId
INTO #Temp
FROM YourTable
WHERE ID in (123,987)
--swap values
UPDATE y
SET col1=t.col1
,col2=t.col2
FROM YourTable y
INNER JOIN #Temp t ON y.id =t.JoinId
WHERE ID in (123,987)
答案 1 :(得分:5)
简单更新有效:
UPDATE myTable
SET
col1 = CASE WHEN col1 = 1 THEN 5 ELSE 1 END,
col2 = CASE WHEN col2 = 2 THEN 6 ELSE 2 END,
col3 = CASE WHEN col3 = 3 THEN 7 ELSE 3 END
结果:交换行值。
答案 2 :(得分:3)
if (this.props.resetOnSubmit !== false) {
this.refs.form.reset();
}
您不一定需要使用表格的ID列,我相信您可以使用正确的连接逻辑搜索任何列。将表连接到自身就是诀窍。
答案 3 :(得分:1)
我最近有类似的问题我有一个用于订购输出的专栏,并希望允许移动订单。我正在寻找答案并遇到了这个问题。这并没有充分回答我的特定查询,但也许我的解决方案会帮助其他人。
我的数据库看起来像这样
表:Order_Table
Index_Column,Order_Column,Text
1 ,1 ,"Second Test text"
2 ,2 ,"First Test text"
我希望能够在php中使用pdo交换它们。 最终我找到了一种方法来使用一个SQL查询
UPDATE `Order_Table` AS o
INNER JOIN (SELECT `Index_Column`, `Order_Column` FROM `Order_Table`
WHERE `Index_Column` IN (:Index1,:Index2))
AS t ON o.`Index_Column` <> t.`Index_Column`
SET o.`Order_Column` = t.`Order_Column`
WHERE o.`Index_Column` IN (:Index1,:Index2)
答案 4 :(得分:1)
您需要按&#34; WHERE&#34;选择所有记录。条件, 然后&#34; SET&#34;更新&#34; CASE&#34;条件。
UPDATE tbl_Temp SET
fk_userType = CASE fk_userType WHEN 1 THEN 2 WHEN 2 THEN 1 END,
fk_userRole = CASE fk_userRole WHEN 1 THEN 2 WHEN 2 THEN 1 END
WHERE (fk_userType = 1 AND fk_userRole = 1) OR (fk_userType = 2 AND fk_userRole = 2);
答案 5 :(得分:1)
上述示例均不实用......它应如下所示:
/*******************************************************************************/
/* DATA TABLE IS PREPARING */
/*******************************************************************************/
IF EXISTS (SELECT TOP 1 * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TEST' AND TABLE_SCHEMA = 'dbo')
DROP TABLE [dbo].[TEST];
CREATE TABLE [dbo].[TEST](
[ID] int IDENTITY(1,1) NOT NULL,
[Name] varchar(50) NULL,
[Surname] varchar(50) NULL,
[AGE] int NULL,
CONSTRAINT [PK_TEST] PRIMARY KEY CLUSTERED
( [ID] ASC ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/*******************************************************************************/
/* INSERTING TEST VALUES */
/*******************************************************************************/
INSERT INTO dbo.TEST (Name, Surname, AGE)
SELECT 'Sevim' , 'PARLAYAN' , 36 UNION ALL
SELECT 'Uğur' , 'PARLAYAN' , 41 UNION ALL
SELECT 'Berkan Cahit' , 'PARLAYAN' , 17 UNION ALL
SELECT 'Miray Çağla' , 'PARLAYAN' , 6 ;
SELECT * FROM dbo.TEST ORDER BY ID;
-- At this point maybe the trigger can be disabled...
/*******************************************************************************/
/* I'm swapping Uğur and Sevim rows (So, rows into 1 and 2 do swapping )... */
/*******************************************************************************/
UPDATE TT
SET TT.Name = ZZZ.Name
, TT.Surname = ZZZ.Surname
, TT.AGE = ZZZ.AGE
FROM dbo.TEST as TT
JOIN (
SELECT TOP 1 * FROM dbo.TEST WHERE ID = 2 /* Big key value first */ UNION ALL
SELECT TOP 1 * FROM dbo.TEST WHERE ID = 1 /* Then small key value... */
) as ZZZ on ZZZ.ID in (1, 2)
WHERE TT.ID in (1, 2) ;
-- At this point maybe the trigger can be activated...
SELECT * FROM dbo.TEST ORDER BY ID
答案 6 :(得分:1)
如果只需要交换几行,那么是的,您可以使用量身定制的case语句和其他语句中的join语句来强行使用它。但是,如果您需要在许多行上进行操作,那将会很痛苦。
WITH map AS (
SELECT *
FROM (VALUES
(1, 2), -- Here's an example of swapping two rows:
(2, 1), -- 1 <- 2, 2 <- 1
(3, 4), -- Here's an example of rotating three rows:
(4, 5), -- 3 <- 4, 4 <- 5, 5 <- 3
(5, 3),
(6, 7) -- Here's an example of just copying one row to another: 3 <- 5
) AS a (destID, srcID)
)
UPDATE destination
SET
ColumnA = source.ColumnA,
ColumnB = source.ColumnB,
ColumnC = source.ColumnC
FROM
SomeTable AS destination
JOIN map ON map.destID = destination.ID
JOIN SomeTable AS source ON source.ID = map.srcID
注释
答案 7 :(得分:0)
我创建了一个包含列名和性别的表。我在其中插入了值。现在我想将性别列中的值替换为M = F,F = M.
答案 8 :(得分:0)
如果只有一个值必须在两行之间交换:
flatmap