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 have a table named as mytable where it has column named a_column.currently it is null valued and it has 100 rows. I just need to know how to set the value of first 50 rows as ABC and rest 50 rows as XYZ. I have tried to use below queries but i was unsuccessful. kindly suggest me the query.
UPDATE mytable
SET a_column= 'ABC';
INSERT INTO table (mytable) VALUES ("ABC/XYZ")
chamath
You can use following query to do that.
UPDATE mytable
SET a_column= (CASE WHEN id > 50 THEN 'XYZ' ELSE 'ABC' END);
I'd like to
UPDATE table SET column = 1 where column = 0;
INSERT (rows i just updated) INTO history_table;
Can I somehow store the ids from a select query, and then use those to UPDATE and subsequently INSERT rows matching those ids into the history table?
INSERT INTO history_table(id)
(SELECT id from table WHERE column = 0);
UPDATE table SET column = 1 where column = 0;
This way you are only getting the ID's that will be updated for the history_table and then you can update them to the correct values.
(I can't comment yet) Is there a specific reason to do it one query?
If not then you might use temporary table to store ids and fetch them for your update and insert using subquery.
I'm just getting into SQL and need a hand with some of the basics.
I'm trying to write a query that will allow me to find values between range in one column of a table and replace all values in that range with a single value that I would specify with an UPDATE or REPLACE function.
I've gotten as far as
SELECT *
FROM (Table_A)
WHERE Column BETWEEN #a and #b
ORDER by Column desc
but I get back 0 results even though I know there are tons of values between #a and #b.
Select with BETWEEN
SELECT * FROM Table_A WHERE column BETWEEN 10 AND 30
Update
update Table_A set column = 'NEW_VALUE' where column BETWEEN 10 AND 30
Replace
// string hallo2 -> will be change to hallo3
update Table_A set column = replace(column, "2", "3") where column BETWEEN 10 AND 30
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.