I have two tables:
Table1: group_name, item_name, status
Table2: group_name, geo
I want to update table1. The default status is 0. I want to update the status of table1 to 1 using a single UPDATE statement.
I want to check for each row in table1 if the group_name exists in table2. If so, I will update status to 1.
I tried this but was not able to get the correct result.
UPDATE table1
SET table1.`STATUS`=1
WHERE table2.group_name=table1.group_name
How can I achieve my desired result?
you can use multiple update table syntax, so your query would be:
UPDATE table1,table2
SET table1.`STATUS`=1
WHERE table2.group_name=table1.group_name
You could use a multi-table update as others have shown. But you can also do it in a slightly simpler way with a single table update statement with a subselect:
UPDATE table1
SET STATUS = 1
WHERE group_name IN (SELECT group_name FROM table2)
Note also that you don't even need the precondition that status in all rows is initially set to zero. You can update all rows to their correct values in a single UPDATE statement:
UPDATE table1
SET STATUS = group_name IN (SELECT group_name FROM table2)
the SET is his command... the actual format is :
update "tablename"
set "columnname" =
"newvalue"
[,"nextcolumn" =
"newvalue2"...]
where "columnname"
OPERATOR "value"
[and|or "column"
OPERATOR "value"];
don't have a SQL database up and running, but I believe the UPDATE command is similar to the FROM command, so i think you have to do
UPDATE table1, table2 SET table1.'status' = 1
WHERE table2.group_name=table1.group_name
Related
I'm trying to fill a certain column of a SQL table with data from another table. I have a column named "size" in my table which should return the number of rows in the 2nd table where the id of both rows is the same. Is there a way to populate a SQL column based on a certain command? I would love to be able to fill the column based on this command:
SELECT count(*)
FROM second_table
WHERE id = "row_id";
Here is a sample database with the two tables:
Table 1
Name
id
tiger
1
lion
2
gazelle
1
Here is the desired output for Table 2:
id
Number of Animals
1
2
2
1
I am trying to fill the Number of Animals column but do it automatically and dynamically when another row is added or deleted to Table 1, which is why I want the Select count(*) SQL statement as the code for the column.
One method is a correlated subquery:
update table1 t1
set size = (select count(*)
from table2 t2
where t2.id = t1.id
);
If you need to do this dynamically (as data is inserted), then you would need to use a trigger. However, I would suggest that you calculate the value as needed, unless there is a specific reason why you need to store it.
I guess you need something like this:
CREATE TRIGGER UpdateAnimalCountTable2
AFTER INSERT ON `Table1` FOR EACH ROW
begin
DECLARE NewCount int;
SELECT count(1)
INTO #NewCount
FROM Table1
WHERE Table1.id= NEW.id;
UPDATE Table2
SET NoOfAnimals = #NewCount
WHERE id = NEW.id;
END;
Above is the trigger which will be executed after every insert in Table1 and will update the count in Table 2 for ID which just got inserted in Table1.
I'm trying to update my table from another table
I need to update the IP in table1 from the new_IP in table changeip according to the SIM number (whcih is the same on both tables)
I have try to do it from what it say here :
mysql update column with value from another table
I get error "IP can't be null"
this is what I wrote in the command line
UPDATE table1
SET table1.IP = (
SELECT changeip.New_IP
FROM changeip
WHERE table1.SIM_NEW = changeip.SIM_Number
);
what am I doing wrong ?
****************update
this is table1
'10.226.202.169', '8997250000031944123'
'10.226.202.170', '8997250000031944131'
'10.226.202.173', '8997250000031944164'
'10.136.136.101', '8997250400019201597'
'10.136.136.102', '8997250400019201589'
'10.136.136.103', '8997250400019201571'
'10.136.136.104', '8997250400019201563'
and so on........
this is changeip table
'10.226.202.169', '8997250000031944123', '10.136.137.221'
'10.226.202.170', '8997250000031944131', '10.136.137.222'
'10.226.202.173', '8997250000031944164', '10.136.137.223'
'10.226.202.174', '8997250000031944172', '10.136.137.224'
'10.226.202.175', '8997250000031944180', '10.136.137.225'
'10.226.202.177', '8997250000031944206', '10.136.137.226'
Thanks ,
you need join and update
UPDATE table1 t
inner join changeip p
on t.SIM_NEW= p.SIM_Number
and t.IP=p.old_ip
SET t.IP =p.New_IP
ALTER table1 MODIFY IP varchar(20) null;
alter the column from not null to null if the other table consist null values
This should work if you want to update those that have an IP on the changeip table and leaves those that don't have a new IP with the old IP:
UPDATE table1
SET table1.IP = (
SELECT changeip.New_IP
FROM changeip
WHERE table1.SIM_NEW = hangeip.SIM_Number
AND hangeip.New_IP IS NOT NULL
);
An easy way to achieve the required result is as follows:
Based on your data the start tables are
and
If you run the query
UPDATE table1, changeip
SET table1.IP = changeIP.old_IP
WHERE table1.SIM = changeip.SIM_Number
The outcome is as follows:
Which I believe is the desired result. Most of the suggestions above are missing the WHERE clause in the UPDATE statement and that is why they fail.
When I select a record from a table using SELECT how can I UPDATE one of that record's fields in the same statement? It's basically the combination of the following two statements:
SELECT value FROM items WHERE = id ='123'
UPDATE items SET found=1 WHERE id='123'
To mark that record as found.
Edit...
Would SELECT FOR UPDATE be appropriate in this instance?
i am trying to add incremented values to a new column in table.
Here is a sample structure of table
---------------------
Name - class - id
---------------------
abbc - 2 - null
efg - 4 - null
ggh - 6 - null
---------------------
i want to write a query that will generate unique id's for all records in table
Here is the query i have tried but show null
set #i=0;
update table1 set id =(#i:=#i+1);
What you have shown should work; the id column should be getting assigned values.
I tested your statement; I verified it works on my database. Here's the test case I ran:
CREATE TABLE table1 (`name` VARCHAR(4), class TINYINT, id INT);
INSERT INTO table1 (`name`,class) VALUES ('abbc',2),('efg',4),('ggh',6);
SET #i=0;
UPDATE table1 SET id =(#i:=#i+1);
SELECT * FROM table1;
Note that MySQL user variables are specific to a database session. If the SET is running in one session, and the UPDATE is running another session, that would explain the behavior you are seeing. (You didn't mention what client you ran the statements from; most clients reuse the same connection, and don't churn connections for each statement, I'm just throwing that out as a possibility.)
To insure that #i variable is actually initialized when the UPDATE statement runs, you can do the initialization in the UPDATE statement by doing something like this:
UPDATE table1 t
CROSS
JOIN (SELECT #i := 0) s
SET t.id =(#i:=#i+1);
I tested that, and that also works on my database.
try this query my friend:
set #i=0;
update table1 set id =(select #i:=#i+1);
SQL Fiddle
SET #a = 0;
UPDATE table_name SET id = #a:=#a+1;
Use AUTOINCREMENT parameter for the respective column instead. This parameter will put an unique auto incremented value in the respective column.
I want to do all these update in one statement.
update table set ts=ts_1 where id=1
update table set ts=ts_2 where id=2
...
update table set ts=ts_n where id=n
Is it?
Use this:
UPDATE `table` SET `ts`=CONCAT('ts_', `id`);
Yes you can but that would require a table (if only virtual/temporary), where you's store the id + ts value pairs, and then run an UPDATE with the FROM syntax.
Assuming tmpList is a table with an id and a ts_value column, filled with the pairs of id value, ts value you wish to apply.
UPDATE table, tmpList
SET table.ts = tmpList.ts_value
WHERE table.id = tmpList.id
-- AND table.id IN (1, 2, 3, .. n)
-- above "AND" is only needed if somehow you wish to limit it, i.e
-- if tmpTbl has more idsthan you wish to update
A possibly table-less (but similar) approach would involve a CASE statement, as in:
UPDATE table
SET ts = CASE id
WHEN 1 THEN 'ts_1'
WHEN 2 THEN 'ts_2'
-- ..
WHEN n THEN 'ts_n'
END
WHERE id in (1, 2, ... n) -- here this is necessary I believe
Well, without knowing what data, I'm not sure whether the answer is yes or no.
It certainly is possible to update multiple rows at once:
update table table1 set field1='value' where field2='bar'
This will update every row in table2 whose field2 value is 'bar'.
update table1 set field1='value' where field2 in (1, 2, 3, 4)
This will update every row in the table whose field2 value is 1, 2, 3 or 4.
update table1 set field1='value' where field2 > 5
This will update every row in the table whose field2 value is greater than 5.
update table1 set field1=concat('value', id)
This will update every row in the table, setting the field1 value to 'value' plus the value of that row's id field.
You could do it with a case statement, but it wouldn't be pretty:
UPDATE table
SET ts = CASE id WHEN 1 THEN ts_1 WHEN 2 THEN ts_2 ... WHEN n THEN ts_n END
I think that you should expand the context of the problem. Why do you want/need all the updates to be done in one statement? What benefit does that give you? Perhaps there's another way to get that benefit.
Presumably you are interacting with sql via some code, so certainly you can simply make sure that the three updates all happen atomically by creating a function that performs all three of the updates.
e.g. pseudocode:
function update_all_three(val){
// all the updates in one function
}
The difference between a single function update and some kind of update that performs multiple updates at once is probably not a very useful distinction.
generate the statements:
select concat('update table set ts = ts_', id, ' where id = ', id, '; ')
from table
or generate the case conditions, then connect it to your update statement:
select concat('when ', id, ' then ts_', id) from table
You can use INSERT ... ON DUPLICATE KEY UPDATE. See this quesion: Multiple Updates in MySQL
ts_1, ts_2, ts_3, etc. are different fields on the same table? There's no way to do that with a single statement.