How to delete rows using inner join in mysql - mysql

DELETE *
FROM ((disease
INNER JOIN dishead ON disease.heading = dishead.hid)
INNER JOIN disdes ON disease.description = disdes.did)
where disease.id = 9;
basically i have inserted the data using inner join and the data is stored against same product in multiple tables
Now i am trying to write query for deleting that inserted rows from all tables

You have to specify the tables to delete from
DELETE d, dh, dd
FROM disease AS d
JOIN dishead AS dh ON d.heading = dh.hid
JOIN disdes AS dd ON d.description = dd.did
WHERE d.id = 9
Note also that if these are foreign keys, you can configure them with ON DELETE CASCADE. Then you only have to delete from the parent table, and the rows that reference them will be deleted automatically.

Related

Delete data from table without creating temporary table

I have three tables, that match the following diagram:
And I need to delete some data from join_table, where a label column(table_right) and name column(table_left) match some criteria.
My solution is to use a temporary table:
create temporary table if not exists data_for_deletion
(select jt.id
from join_table jt
left join table_left tableLeft on jt.table_left_id = tableLeft.id
left join table_right tableRight on jt.table_right_id = tableRight.id
where tableLeft.name = 'name' and tableRight.label = 'label');
delete from join_table where id in (select id from data_for_deletion);
My question is: is there any other way to do such deletion without creating a temporary table?
You should be able to use MySQL's multi-table DELETE syntax:
DELETE jt
FROM join_table jt
JOIN table_left tl ON jt.table_left_id = tl.id
JOIN table_right tr ON jt.table_right_id = tr.id
WHERE tl.name = 'name' AND tr.label = 'label'
Note that since you have a WHERE clause which is dependent on columns in table_left and table_right there is no point in using a LEFT JOIN as it will be converted to an INNER JOIN anyway (see the manual).

PHP MySQL Multiple Update

I have 2 tables that need updating based on 2 where clauses
i included a 3rd table which would join the other 2 tables together. i cant get either working.
UPDATE (list INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID) INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET
Players.Players_Team_ID = 6, Users.users_bank = users_bank-15000000, list.transfers = list.transfers+1
WHERE Users.User_ID=14 AND Players.Players_ID=3;
Without the 3rd table having an update it would be
UPDATE (list INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID) INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET
Players.Players_Team_ID = 6, Users.users_bank = users_bank-15000000
WHERE Users.User_ID=14 AND Players.Players_ID=3;
Can anyone help me get this working?
You can change your query to be like below using update-join syntax but I don't see why you need to JOIN with other tables. Your UPDATE statements could be single or individual update as well
UPDATE list,Players,users
INNER JOIN Players ON list.Team_ID = Players.Players_Team_ID
INNER JOIN Users ON list.Team_ID = Users.User_Team_ID
SET Players.Players_Team_ID = 6,
Users.users_bank = users_bank - 15000000,
list.transfers = list.transfers + 1
WHERE Users.User_ID=14
AND Players.Players_ID=3;

Can we write condition in ON clause of left join which may or may not satisfy?

I have a table for audit trail which saves all actions performed records through out the project like add update and delete.
there I maintain a column which saves primary keys of multiple tables on which action is performed. This is integer column
my query is like this
select * from
user usr1
left join activity_history
on activity_history.userID= usr1.sequenceID
left join candidate can1 on can1.userID = usr1.sequenceID
and can1.userID = activity_history.activity_sequenceID
left join institute ins1 on ins1.userID= usr1.sequenceID
and ins1.userID = activity_history.activity_sequenceID
left join candidate_institutes caninst on caninst.candidateID = can1.candidateID and caninst.instituteID= ins1.instituteID
left join exam exam1 on exam1.instituteID = ins1.instituteID
and exam1.examID = activity_history.activity_sequenceID
left join proctor pro1 on pro1.userID = usr1.sequenceID
and pro1.proctorID = activity_history.activity_sequenceID
left join appointment appt1 on appt1.examID = exam1.examID
and appt1.sequenceID = activity_history.activity_sequenceID
/* COMMENTED CODE-----
on( activity_history.activity_sequenceID=can1.userID
OR activity_history.activity_sequenceID=ins1.userID
OR activity_history.activity_sequenceID=exam1.examID and activity_history.userID= usr1.sequenceID
OR activity_history.activity_sequenceID=pro1.userID
OR activity_history.activity_sequenceID=appt1.sequenceID
)
*/
order by activity_history.sequenceID desc
activity_sequenceID is the column where I am storing keys of other tables
I need to map that. Is this a right way to join these tables or Commented part could be the rite way ?
Or is there any other way to join these tables.
I am confused because I am writing a condition in but activity_histry table may or may not have records of that particular table .

Delete row from table conditional on a related table

I have two tables: Store and Company. I want to be able to delete a store based on the information of its company.
Initially I tried:
DELETE FROM Store WHERE (SELECT * FROM Company WHERE companyValueA > 5).company_id = store.company_id AND store.wf = 0;
But this isnt working. I was thinking about doing a join of the two and deleting from the join like this:
DELETE FROM (SELECT * FROM Store LEFT JOIN Company ON Store.company_id = Company.company_id) WHERE companyValueA > 5 AND wf = 0;
but that isnt working either. Does anyone know how I can accomplish this?
Just a guess... obviously, make a backup before attempting this!!!
DELETE s
FROM store s
JOIN company c
ON c.company_id = s.company_id
WHERE c.companyValueA > 5
AND s.wf = 0;
You could use the MYSQL's DELETE with JOIN syntax (i haven't used it) or just collect all the store id's to be deleted with subquery if you need a more compatible statement:
DELETE
FROM store
WHERE store_id IN (
SELECT S.store_id
FROM store S, company C
WHERE C.company_id = S.company_id
AND C.companyValueA > 5
AND S.wf = 0
)

sql Inner join delete

I have tow tables
tests_sub ,tests_sub_sub
and tests_sub.id = tests_sub_sub.id
so i want to delete data from both tables with one
sql query
I used the following inner join
DELETE tests_sub, tests_sub_sub FROM tests_sub
INNER JOIN tests_sub_sub ON tests_sub_sub.id = tests_sub.id
WHERE tests_sub.id = 10
the query works ok only if both tables have entries for the tests_sub.id...
( if tests_sub_sub has no entry for test_sub.id = 10 ... although the table tests_sub has entries for that id no rows are affected ...
please suggest some tips....
use LEFT JOIN.
DELETE tests_sub, tests_sub_sub
FROM tests_sub
LEFT JOIN tests_sub_sub
ON tests_sub_sub.id = tests_sub.id
WHERE tests_sub.id = 10