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);
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 have a table with 10 rows which contains a column named "age" and holds int(3). Is there a way to update all the rows with everyone's respective ages with one SQL statement?
I've been doing
update table_name set age = age_int where first_name = "John";
over and over again. But I'm wondering if I can condense all 10 statements into one.
You could use a case expression:
UPDATE table_name
SET age = CASE first_name
WHEN 'abc' THEN 10
WHEN 'xyz' THEN 11
END
I'm trying to update a certain column of certain row WHERE id is certain value. The thing is, the number/names of columns are variable, and so are their respective ids.
For example:
UPDATE table SET column1="hello" WHERE id = 5
UPDATE table SET column2="cucumber" WHERE id = 6
How can I do a single mysql query in PDO to do this?
First thing I tried is...
UPDATE table SET column1="hello", column4="bye" WHERE id IN(5, 6)
But that query will update BOTH of those columns in rows where it finds BOTH of those ids, and that's not what I'm looking for. Is it only possible to do this query by query?
Keep in mind that the argument after SET is variable, so the columns to be updated, their values and their respective ids are also variable.
A solution where you can just purely bind values would be great, but if I have to build the query string with escaped variables, then that's OK too.
Thank you.
You can do this
UPDATE table t1 JOIN table t2
ON t1.id= 5 AND t2.id= 6
SET t1.column1= 'hello',
t2.column2 = 'cucumber';
Or if you want to do this on a single column
UPDATE table
SET column2 = CASE id
WHEN 5 THEN 'hello'
WHEN 6 THEN ''
END
WHERE id IN(5, 6);
Maybe I'm totally wrong and it's not pssible to do in MySQL. But, what I wanted to do is to fill out column row base in a select query:
For example:
Table 1:
IdNode NodeName NodeContact NodeStatus Nodegroup
1 Machine1 IT 1 IT
2 Machine2 IT 0 IT
3 Machine3 IT 1 IT
4 Machine4 IT 1 Others
4 Machine5 IT 1 Others
Table 2
IdGroup GroupName NodesManagedbyGroup
1 IT ??
2 others ??
Having those two tables, I would like to fill out (automatically) all rows in column NodesManagedbyGroup in the table2.
Manually it would be:
SELECT COUNT(*) FROM table1 where MemberOfGroup='IT';
result value Int = 3
Then
update table2 NodesManagedbyGroup = 3 where GroupName='IT';
There is any way that MySQL do it for me automatically
You can use triggers to do this - you'd create triggers for insert, update and delete on table 1 to update table 2.
This is generally a bad idea though - it's duplicating information around the database, denormalizing the schema.
Alternatively, you can create a view which calculates this data on the fly and behaves like a table when querying.
Use multiple UPDATE syntax with selecting counts in subquery as a sample:
UPDATE
table2
INNER JOIN
(SELECT COUNT(1) AS gcount, Nodegroup FROM table1 GROUP BY Nodegroup) AS counts
ON table2.GroupName=counts.Nodegroup
SET
table2.NodesManagedbyGroup=table1.gcount
You could set a job that performs a stored procedure every 15 minutes or so?
First we start with empty table
rows = 0
Second we insert random rows let say 3400
rows = 3400
For the third time i count how many rows are in the table, then insert the new rows and after that delete rows <= from the count.
This logic only work for the first time. If that repeat the count will always be 3400 but the id will increase so it will not delete the rows
I cant use last inserted ID since the rows are random and I dont how many it will load.
// Update
"SELECT count(*) from table" - the total count so far
"INSERT INTO tab_videos_watched (id , name) values (id , name)" - this is random can be 3400 or 5060 or 1200
"DELETE FROM table WHERE idtable <= $table_count"
If id is auto incremented, then you should use like:
select max(id) from my_table;
Read this maxId into a variable and then use when issued a delete query like:
delete from my_table where id <= ?;
Replace query parameter with the last found maxId value.
Alternatively you can define a column last_inserted as datetime type.
Before next insertions, select it into a local variable.
select max(last_inserted) as 'last_inserted' from my_table;
And after insertions are made, use the last_inserted to delete records.
delete from my_table where last_inserted <= ?;
Replace query parameter with the last found last_inserted value.