Joining on deletion - mysql

I'm working with mysql:
btableA has tableB_id column
tableB has some_interestring_column_on_TableB
I want (pseudo sql below):
delete from tableA
where the associated row in tableB (via tableB_id) has
some_interestring_column_on_TableB = 'interestingValue'
Please help me to translate the pseudo sql into real sql.

Try this:
DELETE TableA
WHERE tableB_id IN (
SELECT id FROM TableB
WHERE interestring_column='pizza');

MySQL supports JOINs in the DELETE statement, as well as deleting from multiple tables in a single statement. The following will only delete from TABLEA:
DELETE ta
FROM TABLEA ta
JOIN TABLEB tb ON b.id = a.tableb_id
AND b.col = 'some value'
If you wanted to delete from both tables, use:
DELETE ta, tb
FROM TABLEA ta
JOIN TABLEB tb ON b.id = a.tableb_id
AND b.col = 'some value'
That said, this support is very uncommon in other databases -- you'd have to use IN or EXISTS in most cases.

I don't know mysql syntax but I'm thinking something like (from mssql):
delete from tableA
where tableA.tableB_id in
(select tableB.id from tableB
where tableB.some_interesting_column_on_TableB = 'interestingValue')

Related

How to use update together with select...for update in mysql

I have two MySql tables tableA and tableB, primary key "id" in tableA is used as a foreign key "parent_id" in tableB. I would like to update single row in tableB using select...for update so that other users can not access it while transaction is not over. My question is - how to correctly update selected row in one query? Here is my sample code:
START TRANSACTION;
SELECT b.reserved, b.owner FROM tableB b, tableA a
WHERE b.parent_id = a.id AND a.guid ='5344a990-fedf-4deb-a114-0d5d6a3ba180' FOR UPDATE;
UPDATE tableB SET...;
COMMIT;
Thank you!
Yes, this is possible.
Please take a look at:
MySQL - UPDATE query based on SELECT Query
MySQL UPDATE
Here is an example of what your query may look like:
START TRANSACTION;
# Lock table using `FOR UPDATE`
SELECT
b.reserved,
b.owner
FROM
tableB b,
tableA a
WHERE
b.parent_id = a.id
AND a.guid ='5344a990-fedf-4deb-a114-0d5d6a3ba180'
FOR UPDATE;
# Update query
UPDATE
tableA
SET
tableA.column1=(
SELECT
b.reserved
FROM
tableB b,
LEFT JOIN tableA a ON a.id=b.id
WHERE
b.parent_id = a.id
AND a.guid ='5344a990-fedf-4deb-a114-0d5d6a3ba180'
)
WHERE ...
LIMIT 1;
COMMIT;
Hope this helps,

SQL Query to find missing records between two tables and then update the second with the missing records from the first

Two tables. 8 fields in each. Both tables have the same data, one with
137,002 record (tablea) and one with 135,759 records (tableb). Both tables share a common primary field if three columns (qid, sid, aid).
Is there a single query that will.
1) compare tablea to tableb on the primary field
and
2) if the record is in tablea and not tableb copy the record from tablea to tableb
I would rather be able to update tableb with an sql query rather than writing a php loop to go through the 137,002 and do a compare on each one.
Thanks
That should be smth looking like:
insert into table2 (qid, sid ...)
select
t1.qid,
t1.sid,
...
from table1 t1
where
not exist (select t2.qid, t2.sid, ... from table2 t2 where t2.qid = t1.qid and t2.sid = t1.sid...)
INSERT INTO tableb AS b
(SELECT * FROM tablea AS a WHERE NOT EXISTS (SELECT * FROM tableb AS b2 WHERE b2.id = a.id))
Use merge...and use insert only....not update.
So, the following worked.
insert into f_step_ans (`qid`, `assid`, `sid`, `sas`, `cas`, `etim`, `stim`, `endtim`, `fil`)
select
t1.qid,
t1.assid,
t1.sid,
t1.sas,
t1.cas,
t1.etim,
t1.stim,
t1.endtim,
t1.fil
from f_step_ans_back t1
where
not exists (select t2.qid, t2.sid,t2.assid from f_step_ans as t2 where t2.qid = t1.qid and t2.assid = t1.assid and t2.sid = t1.sid)
1,588 records were moved from the f_step_ans_back table (old backup table) to the f_step_ans table (partially recovered backup + new data). Reporting shows that everything is working like it should be. Thank you all for the help.

Replace a column of data with another column from another table

I would like to replace a column of data in a table.
TableA
Uid - int
AnotherUid - int
TableB
Uid - int
TableA.uid = Table.B uid
And I am trying to replace the TableB.Uid with TableA.AnotherUid
Select * from TableB a, TableA b where a.uid=b.uid
update TableB set a.uid=b.AnotherUid
I got a SQL syntax error from MySQL at TableB set a.uid=b.AnotherUid.
Please kindly help.
UPDATE TableB T
SET T.uid =
(SELECT AnotherUid
FROM TableA A
WHERE A.uid = T.uid)
UPDATE TableB SET TableB.Uid = (SELECT AnotherUid FROM TableA WHERE TableA.Uid = TableB.Uid)
Try this query:
Update TableB, TableA
Set TableB.uid = TableA.AnotherUid
Where TableB.uid = TableA.uid;
For MySQL manual on join in the Update query please refer: http://dev.mysql.com/doc/refman/5.0/en/update.html and see this example in their doc:
UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;

Update TableA with values from TableB?

If I have 2 tables, each have a product_stat DECIMAL and product_id INT column
I want to run a query that will append the product_stat from TableA to TableB on product_id. Then truncate TableA
Basically I am collecting data and temporarily storing it in TableA, and once a day I want to move the data to TableB. So that TableB only has the data shifted once a day.
The quich solution is to use a subquery
UPDATE tableB SET product_stat = (
SELECT product_stat FROM tableA
WHERE tableB.product_id = tableA.product_id
)
But you can use UPDATE in conjunction with JOIN, which will have a better performance
UPDATE tableB
INNER JOIN tableA ON tableB.product_id = tableA.product_id
SET tableB.product_stat = tableA.product_stat
UPDATE Authors AS A, Books AS B SET AuthorLastName = 'Wats' WHERE B.AuthID = A.AuthID AND AND ArticleTitle='Something';

Inserting distinct entries into the database

I have two tables with exactly the same fields. Table A contains 7160 records and table B 7130 records.Now I want to insert distinct records from table A into table B such that B should not have any duplicate entry in it. How should I go about doing this?
This basically selects records that are in A that are not in B. It would work, but you might have to tweak the field you use to uniquely identify a record. In this example I used field 'ID' but you might have to change that to A.field1 = B.field1 AND A.field2 = B.field2 etc.
INSERT INTO TABLEB
(
SELECT A.*
FROM TABLEA A
LEFT JOIN TABLEB B ON A.ID = B.ID
WHERE B.ID IS NULL
)
You can use a "union" query to combine the results from multiple tables into a single result set. "union" will only return distinct rows from all tables.
See this page for more info:
http://www.tutorialspoint.com/mysql/mysql-union-keyword.htm
insert into tableB (id)
select t1.id from tableA t1
where t1.id not in (select t2.id from tableB t2)