Check table_A before updating table_B - mysql

I'm trying to make an query that checks if the field 'field_A' in 'Table_A' is '0' and if it is, update 'field_B' in 'Table_B' to '10'.
I have no idea if this is even possible. I have very little experience with mysql and I'm just trying some things out. But if it is, I'd like to know how this is done.

update table_B
set field_B = 10
where exists (select 1 from table_A where field_A = 0)

Related

Selecting and storing values, using those values in IN clause

Is there a way to select a set of values from one table and then use those values in an IN clause?
I want to select IDs from one table and then update data for those IDs in another table.
So something like
<some var> = SELECT id from tableA WHERE <something>;
INSERT INTO tableB <stuff> where id IN (<some var>);
I release the variable syntax isn't real. just want to display my intent. I have read about SET a little but am still new to MySQL so it doesnt make perfect sense. Also it mentioned that SET could only set variables of certain types and they all seemed to simple.
Thanks!
You can use insert . . . select:
INSERT INTO tableB (id)
SELECT id
FROM tableA
WHERE <something>;
I'm not sure what IN has to do with this.
EDIT:
Oh, you want an update:
update tableb b join
tablea a
on b.id = a.id
set b.col = ??
where <conditions on a>;
You can also do this using in:
update tableb b
set col = ??
where b.id in (select a.id from tablea a where <conditions>);
The biggest difference is whether or not you want to use information from tablea. If you do, then you need the join version.

mySQL update a value

Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)

switching column data in mysql? [duplicate]

This question already has answers here:
Swapping column values in MySQL
(23 answers)
Closed 8 years ago.
i am wanting to try and run a query which will update my table ptb_messages and switch the data of two columns around, for instance heres my table:
id | to_user_id | from_user_id |
1 4 5
2 5 6
3 7 9
so what i want to try and do is switch the value of from_user_id over to to_user_id and vice versa.
i was going to use this before i realised that once i copied the value from one table to the other the the original data from the other column would then have been overwritten,
$sql = mysql_query("UPDATE ptb_messages SET ptb_messages.from_user_id=ptb_messages.to_user_id");
what i really need is a swap function,
im not really sure how i might do this but im imagining its something like this:
$sql = mysql_query("UPDATE ptb_messages SET to_user_id=from_user_id, from_user_id=to_user_id");
hope someone can help me please.
Well, mysql has a concept called user variable. You can take advantage of this to store the value and set it on the column for swapping,
UPDATE Table1
SET to_user_id = from_user_id,
from_user_id = #r1
WHERE #r1 := to_user_id
see here: http://www.sqlfiddle.com/#!2/8cd6a/1
how about joining the table with itself?
UPDATE Table1 a
INNER JOIN Table1 b
ON a.to_user_id = b.to_user_id AND
a.from_user_id = b.from_user_id
SET a.to_user_id = b.from_user_id,
a.from_user_id = b.to_user_id
see here: http://www.sqlfiddle.com/#!2/d6b4f/1
I'm going to throw an answer out there, although I think Skinny Pipes answer is a little more elegant.
You can create a temporary table to store the values in.
http://www.sqlfiddle.com/#!2/3631d/1
create table ptb_messages_temp (
id int,
tempid int);
insert into ptb_messages_temp (id, tempid) (select id, to_user_id from ptb_messages);
-- DO THE UPDATE
UPDATE ptb_messages
LEFT OUTER JOIN ptb_messages_temp on ptb_messages.id=ptb_messages_temp.id
SET
ptb_messages.to_user_id=ptb_messages.from_user_id,
ptb_messages.from_user_id=ptb_messages_temp.tempid;

Issue with the TOP 1 query

Is it possible to achieve next thing without using views, but just one single query? I have two tables:
TableA->TanbleB (1-many) ON TableA.Id = TableB.TableAId
I need to update one field in Table A (TableA.Field1) for records in TableA that satisfy condition on one field in tableA (WHERE TableA.Field2=SomeValue)
.
TableA.Field1 will be updated from TableB with value that is last inserted (last inserted value in related records to TableA).
I will put an example:
UPDATE TableA a SET Field1 = (SELECT TOP 1 b.Feild1 * b.Field2 FROM TableB b WHERE b.TableAId = a.id) WHERE field2 = 1
I know Above example doesn't work, but I have many ways tried using INNER JOIN and failed. I had an idea to use something like this:
UPDATE TableA INNDER JOIN ( SELECT ... FROM TABLE B) ON TABLEA.Id= TableB.TableAId SET ....
But the 2ns query should return 1 record for each DISTINCT TableAId, but only the last inserted.
I hope I am making some sense here.
Thanks in advance.
Here is some SQL that will do what you want
UPDATE T1 INNER JOIN T2 ON T1.ID = T2.T1ID SET T1.F2 = [T2].[F2]*[T2].[F3] WHERE (((T1.F1)="ABC") AND ((T2.ID)=DMax("[ID]","[T2]","[T1ID]=" & [T1].[ID])));
This predicated on T1.ID being the primary key for T1 and T2.T1ID being a index field in T2
One of the flaws in Access is that you can't run an "UPDATE" query based on a "SELECT" query, it will usually give the error:
Operation must use an updateable query
The only way around is as you say to create a view of the "SELECT" query and then inner join this on your table, Access is then working with a static recordset and can handle the "UPDATE" query ok
Alternatively you could write a VBA procedure to step through line by line with the Recordset.
Best of luck : )
UPDATE:
SELECT b.TableAId, b.Feild1 * b.Field2 INTO tblView FROM TableB As b WHERE b.field2 = 1

Deleting with Max

This is based on my previous question.
I have the following table
Table1
JobPositionId | JobPositionName
1 | Sound
2 | Lights
3 | Sound
4 | Ground
How can I delete row three (Name = sound, and max position)
Use:
DELETE FROM TABLE t1
JOIN (SELECT x.jobpositionname,
MAX(jobPositonId) AS max_id
FROM TABLE x
GROUP BY x.jobpositionname) t2
WHERE t1.jobPositonId = t2.max_id
AND t1.jobpositionname = t2.jobpositionname
AND t2.jobpositionname = 'Sound'
As I mentioned in your previous question, using this won't trigger a MySQL 1093 error:
DELETE FROM TABLE
WHERE JobPositionId = SELECT x.id
FROM (SELECT MAX(JobPositionId) AS id
FROM TABLE
WHERE JobPositionName = 'Sound') x
DELETE FROM
Table1
WHERE
JobPositionId = (
SELECT
MAX(JobPositionId)
FROM
Table1
WHERE
JobPositionName = 'Sound'
)
Sorry if this doesn't take into account your "previous question" thought I'd just look at this one.
DELETE FROM Table1 WHERE jobpositionid = (SELECT MAX(jobpositionid) FROM table1 WHERE name = 'Sound');
It seems like what you are trying to do is to delete all duplicate JobPositionNames, leaving only the one with the lowest JobPositionId.
I had to do something very similar recently and found the SQL statements getting so complicated, it was much much easier (if much less efficient) to do it in SQL.
So in case this is a poluted database you're trying to clean, just write a script that does this and be done with it (and set some unique indexes to prevent it from happening again).
If this happens all the time, and needs to be done periodicaly, fix the code that does this.