mysql交換兩行或兩列的值

2021-09-05 12:17:59 字數 2998 閱讀 6219

參考:

mysql left join中where和on條件的區別

新建資料庫和表,並插入資料

create database test;

use test;

create table `product` (

`id` int(10) unsigned not null auto_increment

comment '產品id',

`name` varchar(50) not null

comment '產品名稱',

`original_price` decimal(5, 2) unsigned not null

comment '原價',

`price` decimal(5, 2) unsigned not null

comment '現價',

primary key (`id`)

) engine = innodb

default charset = utf8;

insert into `product` (`id`, `name`, `original_price`, `price`) values

(null, '雪糕', '5', '3.5'),

(null, '鮮花', '18', '15'),

(null, '甜點', '25', '12.5'),

(null, '玩具', '55', '45'),

(null, '錢包', '285', '195');

查詢:

按別名查詢:

交換兩列的值(列名不變):

# 交換mysql的兩列的值(列名不變)

update product as a, product as b

set a.original_price = b.price, a.price = b.original_price

where a.id = b.id;

再次查詢:

可看到已改變。

新建資料庫和表,並插入資料

create database test;

use test;

create table `rules` (

`rule_id` int(10) unsigned not null auto_increment,

`rule_name` varchar(255) not null,

`priority` int(11) default null,

primary key (`rule_id`)

);insert into rules

(rule_id, rule_name, priority)

values

(1, 'take one bread', 10),

(2, 'drink water', 20),

(3, 'eat candy', 30),

(4, 'wash hand', 40),

(5, 'give charity', 50);

查詢:

按照別名和id查詢:

交換兩行的值(除了id)

update

rules as rule1

join rules as rule2 on

(rule1.rule_id = 1 and rule2.rule_id = 4)

or (rule1.rule_id = 4 and rule2.rule_id = 1)

set rule1.priority = rule2.priority,

rule2.priority = rule1.priority,

rule1.rule_name = rule2.rule_name,

rule2.rule_name = rule1.rule_name;

再次查詢:

可看到兩行已完成了交換。

awk輸出相同列的前兩行和後兩行

要求列印,第一列相同的頭兩行和後兩行 file ax bx 1 ax bx 2 ax bx 1 ax bx 8 ax bx 1 ax bx 3 ax bx 5 cx bx 1 cx bx 0 cx bx 1 cx bx 6 cx bx 9 ex bx 1 ex bx a ex bx 1 ex bx ...

MySQL實現把兩行兩列資料合併為一行一列

最近在oa專案中使用acitiviti中,遇到乙個排他閘道器有多個判斷條件 並且可以多次執行,在顯示已辦任務的時候要歸屬為一條資料,利用group concat和concat加上group by 解決。詳細sql如下 select aht.id as id,ard.name as processna...

IE支援原生交換兩行的位置

在ie的table,tbody,thead,tfoot中,它們都支援乙個叫moverow indextomove,destinationindex 的api,第乙個引數要移去的行號,第二個引數為當前的行號,行號即為rowindex。有了,我們就可以簡捷地交換兩行位置,而不需要冗長的insertbef...