I am using postgres as my database and have 2 tables
accounts
id | name | status
accountOwner
account_id | user_id
I have user_id(accountOwner table) and status(accounts table) to be updated based on accountid I have. Is it possible to update both the tables in 1 query? I tried the following
UPDATE accounts,accountOwner SET accounts.status='active', accountOwner.user_id=3 WHERE accounts.id=accountOwner.account_id AND accountOwner.account_id = 1;
No, you cannot update two tables at once. The documentation says:
UPDATE -- update rows of a table
However, you can use a FROM clause, for example:
UPDATE accounts SET status = 'active'
FROM accountOwners
WHERE accounts.id = accountOwners.account_id
AND accountOwners.account_id = 1;
Related
I apologize for the title, I couldn't find a good way to word my question. I'm very new to SQL.
Essentially, I'm creating this table (the ids actually reference the students table, but I've simplified it here):
CREATE TABLE followers (student_id int not null,
followee_id int not null,
followsback boolean,
PRIMARY KEY(student_id, followee_id)
SET followsback = IF(SELECT from followers
WHERE student_id = followee_id AND
followee_id = student_id, 1, 0)
My problem lies with the IF statement. Say I ran this INSERT query:
INSERT into followers(student_id, followee_id) values(001,002)
This is supposed to store that student 001 is following student 002.
I need to select the followee (002) and check if they are following the student (001) back. To do this, I need to check the followers table for a user with student_id = followee_id (e.g student_id = 002), then check to see if that user (002) is following the original student_id (001).
The problem is that I don't know how to reference the student_id as specified within the INSERT query vs referencing the value within my SELECT query.
Then if the two students are following each other then I need to set followsback to 1.
Hopefully this makes sense, I am having a ridiculously hard time explaining this.
There's no syntax in MySQL's CREATE TABLE statement to do what you show. It could be done by a rare feature in the SQL specification called an "assertion"—except there is no SQL database on the market that implements this feature.
You can try to implement it as a trigger:
CREATE TRIGGER followback_ins BEFORE INSERT ON followers
FOR EACH ROW
SET NEW.followsback = EXISTS (
SELECT * from followers
WHERE student_id = NEW.followee_id AND followee_id = NEW.student_id);
But this has a problem. It only updates the followsback for the new record, not the original record.
mysql> insert into followers set student_id = 123, followee_id = 456;
mysql> insert into followers set student_id = 456, followee_id = 123;
mysql> select * from followers;
+------------+-------------+-------------+
| student_id | followee_id | followsback |
+------------+-------------+-------------+
| 123 | 456 | 0 |
| 456 | 123 | 1 |
+------------+-------------+-------------+
This is called an anomaly because when you try to store the same fact in two places, these two rows can contradict each other.
We could try to make a trigger that updates the original row too:
CREATE TRIGGER followback_ins AFTER INSERT ON followers
FOR EACH ROW
UPDATE followers AS f1 JOIN followers AS f2
ON f1.student_id=f2.followee_id AND f1.followee_id=f2.student_id
SET f1.followsback=true, f2.followsback=true;
But this is illegal. You can't update a table from a trigger on that same table (too much risk of infinite recursion).
ERROR 1442 (HY000): Can't update table 'followers' in stored function/trigger
because it is already used by statement which invoked this stored
function/trigger.
I'd suggest to forget about storing followsback at all. Instead, just store the following relationships as two rows, without a followsback column. If you want to know if they follow each other, you have to join two rows together:
SELECT COUNT(*)
FROM followers AS f1 JOIN followers AS f2
ON f1.student_id=f2.followee_id AND f1.followee_id=f2.student_id
WHERE f1.student_id = 123 AND f1.followee_id = 456.
This query will return 0 if there is no mutual following, and 2 if there is.
I am trying to update rows in mysql but I have to use for loop for multiple update for single value mysql query is
update table set column1='100' where id =1
update table set column1='100' where id =6
update table set column1='100' where id =14
I am using for loop for running query multiple times with different id, I want to run single query for update all rows. Is that possible?
i want to do something like that
update table set column1='100' where id=1,6,14;
Use IN()
update table
set column1='100'
where id in (1,6,14)
or OR
update table
set column1='100'
where id = 1 OR id = 6 OR id = 14
Use IN() Operator
update table_name SET field_name='101'
where id IN(1,2,7,9)
I have the following situation.
In a MySql database I have a posts table. This table contains the following 2 fields: post_modified and post_date
For each record present in the posts table I need set the value of post_modified field with the value into the post_date
How can I do a query that do this work on all the table records?
Tnx
That's an easy one:
UPDATE posts SET post_modified = post_date;
This query should do the job :
UPDATE posts SET post_modified=post_date;
You would just use an update without a condition to update all records:
update posts set post_modified = post_date
Depending on the settings in the database an update without a condition might not be allowed. Then you would add a dummy condition just to tell the database that you actually want to change every record:
update posts set post_modified = post_date where 1 = 1
I need some help with an MySQL update. I have 2 tables as follows:
**Inventory**
vmvcenter
vmid
hostname
**Guest_import**
vmvcenter
vmid
discrepancy
I need to put a 'YES' in the guest_import.discrepancy column if the record does not exist in the inventory table. The key between the tables should be CONCAT(vmvcenter,vmid).
update guest_import g
left join Inventory i on i.vmvcenter = g.vmvcenter and i.vmid = g.vmid
set discrepancy = 'YES'
where i.vmvcenter is null
I'm currently running more database queries of update, like the following:
UPDATE table SET status = 1 WHERE id = 3
UPDATE table SET status = 1 WHERE id = 7
UPDATE table SET status = 1 WHERE id = 9
UPDATE table SET status = 1 WHERE id = 18
etc...
Question:
How is it possible to run these queries in one?
UPDATE table SET status = 1 WHERE id in (3,7,9,18,...)
If you need to update some rows on a given list you can use IN()
UPDATE table SET status = 1 WHERE id IN (3, 7, 18);
If instead you need to update all rows just don't add any WHERE conditions
UPDATE table SET status = 1;
Your question is a bit general if you mean how to update multiple rows in one command in general it depends on your queries but if your question is more specific and you need to run 1 single query instead of all above queries you can try this :
UPDATE table SET status = 1 WHERE id IN (3,7,9,18)