I have a table like this:
Tb_server
ID
5
6
7
Tb_upload
ID
1
2
3
I need a query which can update all the ID of Tb_server to max(Tb_upload.ID) + 1
So, the result on the Tb_server should be like this
Tb_server
ID
4
5
6
I am doing this in a shell script, so I can get the max(Tb_upload.ID) as a variable.
But what will be the query, using MySQL?
Try this:
UPDATE Tb_server, (SELECT #auto:=0) a
SET ID = (SELECT MAX(ID) FROM Tb_upload) + (#auto:= #auto+1);
Take the max+1 of Tb_upload which is : 4
Make the update as :
update Tb_server set Tb_serverID= (Tb_serverID+4); // 4 is the difference here
Related
Is it possible to run an update query on multiple columns with multiple conditions in MySQL?
id name value price instock pp_flag
1 xyz 23 27 1 9
2 abc 28 12 0 8
For example above is the structure of a table myTable, where I want to run a query like:
UPDATE TABLE myTable
set value = 25
where id = 1
and price = 12
where pp_flag = 8
Just wondering if I can do this in the same query in MYSQL.
Thanks
Use and in where clause:
UPDATE TABLE myTable
set value = 25
where id = 1
and price = 12 and pp_flag = 8
What I understand is that you would like to do this
UPDATE TABLE myTable set value = 25 where id = 1
and
UPDATE TABLE myTable set price = 12 where pp_flag = 8
in a single statement.
You cannot do this as these are two independent WHERE-conditions.
You can use multiple condition to update column of a table .
As per your requirement you can use below query:
UPDATE TABLE myTable set value = 25
where id = 1 or (price = 12 and pp_flag = 8);
Hope it will help you!
Yes, it is possible by using the inbuilt IF function in MySQL (https://dev.mysql.com/doc/refman/8.0/en/if.html).
Query for your example would be:
UPDATE myTable SET
value = if(id=1, 25, value),
price = if(pp_flag=8, 12, price)
I have table like that,
id name count
1 rrr 2
2 www 3
3 qqq 4
4 aaa 5
5 gyhhh 4
6 dfgdfg 5
I want to write the query which find the name in table and if it find then increment the count in count column for that name. The count maintain the no of time name used by the user.If user used the name , then I am check the name in db , if it found then I want to update row with increment in count.
A simple update query required:
If you want to increase count only if the input parameter exactly matches the name then use this:
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` = ?
And if you want to increase count if the input parameter is a substring of name then you can use LIKE
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` LIKE CONCAT('%',?,'%')
Note: Replace the question mark (?) by your input parameter.
Try this:
select id,name, id + 1 from
(Select id,name from table_name where name in('sa','da','ba','ca')) as a;
hope it helps..
I wondered if I can get a query like this to work:
"DELETE FROM table WHERE id (BETWEEN 1 AND 5) OR = 8;"
The thing I want to achieve: I want to say; get me the results between some values or also the value if my id = 8.
Unfortunately I only found queries and questions how to search for
things like this.
"DELETE FROM table WHERE id BETWEEN 1 AND 5 AND `email`='sarah#sarah.com';"
Or would I have to do it likewise:
"DELETE FROM table WHERE id BETWEEN 1 AND 5 OR id = 8;"
Thanks for a short input on that...
Cheers
To DELETE:
DELETE FROM table WHERE (id BETWEEN 1 AND 5) OR id = 8
To SELECT:
SELECT * FROM table WHERE (id BETWEEN 1 AND 5) OR id = 8
I have a table like
ID Value
A2424 1
A5355 2
A6363 3
A4634 4
AA_A2424 5
AA_A6363 6
I would like to select only those ID's that show up after AA_
so my output should be
ID Value
AA2424 1
AA5355 2
I tried this but it isn't giving me the right output
SELECT *
FROM table
WHERE ID LIKE SUBSTRING( 'AA_', 4, 8 )
Can anyout suggest??
Thanks
Try:
SELECT ID
, Value
FROM table
WHERE SUBSTRING(ID, 1, 3) LIKE 'AA_%'
I guess your output isn't correct for this example table. It should be:
ID Value
A2424 1
A6363 3
Here is a query to get what you need:
select * from AATable where id in
(select substring(id,4,100) from AATable where id like 'AA_%')
how to update column value of specific id and shift after to right.
id track
1 3
2 5
3 8
4 9
want to update id 3 track column value to 10, result like this
id track
1 3
2 5
3 10
4 8
5 9
id column is auto_increment
or any suggestion it's my pleasure.
thank you.
You should avoid tweaking auto_increments. Auto increment keys are usually supposed to be used internally (e.g. for linking purposes). If you want to order tracks, i suggest you add a seperate numeric field "ordernro" to the table and update that
To add a column order nro to a table named album, do like this:
alter table album add ordernro int(2) after id;
Then copy the current value for id into this new column:
update album set ordernro=id;
(do this only once after adding the column)
To insert track 10 at position 3 first shift the rows:
update album set ordernro = ordernro + 1 where ordernro >= 3;
And then insert track 10:
insert into album (ordernro, track) values (3, 10);
Remember to update your existing insert/update/select statements accordingly.
The result can be checked by:
select * from album order by ordernro;
(The id will now be "mixed up", but that doesn't matter)
UPDATE table SET id = id + 1 WHERE id >= x;
x being the id where you place your current track.
The problem with JK 's answer is that MySQL returns error saying that is can't UPDATE because the index at x+1 would be duplicate.
What I did is
UPDATE table SET id = id + 100 WHERE id >= x;
UPDATE table SET id = id - 99 WHERE id >= x;
And then INSERT my row at index x