Awesome q2a theme
Ask us anything
Toggle navigation
Email or Username
Password
Remember
Login
Register
|
I forgot my password
Questions
Unanswered
Tags
Users
Ask a Question
Swap 2 rows in a mysql table
0
like
0
dislike
52
views
Hello!
There is a table, for example, with fields Id (int auto_increment) Name
How to use a sql query to swap the Id of the two lines?
PS have a Good weekend :)
php
mysql
asked
Mar 21, 2019
by
k0rzhik
|
52
views
answer
comment
Please
log in
or
register
to add a comment.
Please
log in
or
register
to answer this question.
7
Answers
0
like
0
dislike
UPDATE table SET priority=IF(priority=1, 2, 1) WHERE priority IN (1,2) — swaps the order 1 and 2. order must be unique, otherwise 3 of the request.
answered
Mar 23, 2019
by
VolCh
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
And nobody asked, but WHY need it? sys-id and auto-increment that is issued by the sequence generator once and for all. Why do you want to do this?
answered
Mar 23, 2019
by
k0rzhik
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
priorities should be put through a special field in the table and id and that id you want to change it
Just add another field to the table type priority and he puts the desired values, and the program itself, instead of ORDER BY id, place ORDER BY piority and your problem will be solved
answered
Mar 23, 2019
by
Zorkus
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
SQL does not know how to change from two lines simultaneously, using a third mean value.
answered
Mar 23, 2019
by
k0rzhik
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
ld if you do not have a primary key but the same value does not contain, it should in theory work like this (may need to swap the numbers 0 and 1)
UPDATE table SET ld=IF(ld=0, 1, IF(ld=1, 0, ld));
answered
Mar 23, 2019
by
Zorkus
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
First change the id of one of the rows to something else, then change id of the second row on the old id of the first row, and then change the first string id to the old id of the second row.
\r
UPDATE table SET ld=9999999 WHERE id = 1;
UPDATE table SET ld=1 WHERE id = 2;
UPDATE table SET ld=2 WHERE id = 9999999;
\r
When this will knock the value of the auto increment for the id but can be handles to change the desired via ALTER TABLE.
\r
Or are you fundamentally the same query? I think not.
answered
Mar 23, 2019
by
Zorkus
ask related question
comment
Please
log in
or
register
to add a comment.
0
like
0
dislike
A good occasion to discover the transaction in the DBMS.
answered
Mar 23, 2019
by
k0rzhik
ask related question
comment
Please
log in
or
register
to add a comment.
Related questions
0
like
0
dislike
1
answer
How to conclude all rows in the table MySQL database into one field textarea?
asked
May 20, 2019
by
fromdns
php
mysql
html
0
like
0
dislike
4
answers
MySQL. How to remove a limited number of rows from a table?
asked
Jun 14, 2019
by
TechNOIR
mysql
0
like
0
dislike
1
answer
How to add rows to a mysql table?
asked
Jun 1, 2019
by
aaalllsss
mysql
0
like
0
dislike
1
answer
How to get rows from table by ID from JSON array in MySQL?
asked
May 21, 2019
by
TTATPuOT
mysql
0
like
0
dislike
2
answers
How to change the contents of one column for all rows in a single MySQL table?
asked
Apr 27, 2019
by
irain
mysql
Most popular tags
javascript
php
css
html
jquery
wordpress
python
linux
web-development
mysql
android
windows
java
layout
c#
computer-networks
node.js
cpp
iron
yii
vue.js
1C-Bitrix
react
laravel
django
nginx
system-administration
search-engine-optimization
api
ubuntu
the-it-education.
ajax
sql
programming
hosting
cms
design
apache
google-chrome
bootstrap
Vkontakte
macos
google
network-administration
git
laptops
algorithms
regular-expressions
unity-game-engine
email
angular
database
network-equipment
software
wooсommerce
debian
.net
ios
information-security
video
law-in-it
browsers
books
parsing
wi-fi
game-development
career
htaccess
postgresql
telegram
mikrotik
mobile-development
ruby-on-rails
the-domain-name-system
modx
Yandex
c
json
opencart
Habr
freelance
vpn
asp.net
windows-server
symfony
bots
hard-drives
math
qt
DIY
audio
frontend
payment-system
bash
electronics
gulp.js
user-interface
docker
online-shopping
110,608
questions
257,187
answers
0
comments
40,796
users