Mysql multi table delete effect all but one table - mysql

I used the following sql syntax to delete from several tables. The result is that the data is erased from table 2-4 but not in table 1 (no errors)
DELETE table1, table2, table3, table4 FROM table2
JOIN table1 ON table1.id = table2.deviceid
JOIN table3 ON table3.deviceid = table2.deviceid
JOIN table4 ON table4.device_id = table2.deviceid
WHERE table2.deviceid = 1;
What am I doing wrong?

Related

Combining two Access Queries

I three tables. Table1, Table2, Table3. They all have a field called KB. Can I have one query to accomplish: Get all the information from table2, when Table1 KB = Table2 KB But only if table1 KB doesn't exist in Table3. I currently have 2 Queries. One to determine which KBs in Table1 don't exist in Table3. I then run a query against those results showing me all the records in Table2 that have matching KB. Can I do that all in 1 query, or 2 queries the best way?
You can do it with a combination of EXISTS and NOT EXISTS:
SELECT t2.*
FROM table2 AS t2
WHERE EXISTS (SELECT 1 FROM table1 AS t1 WHERE t1.KB = t2.KB)
AND NOT EXISTS (SELECT 1 FROM table3 AS t3 WHERE t3.KB = t2.KB)
or with an INNER join of table2 to table1 and a LEFT join of table2 to table1 from which you will return only the non matching rows:
SELECT t2.*
FROM (table2 AS t2 INNER JOIN Table1 ON t2.KB = Table1.KB)
LEFT JOIN table3 ON t2.KB = table3.KB
WHERE table3.KB IS NULL
This may return duplicate rows of table2 if the relationship of table2 and table1 is 1:n, so in this case you can use DISTINCT:
SELECT DISTINCT t2.*
....................

MySQL JOIN from one of the two table based on IF condition

SELECT Table1.Filter, Table1.Condition, Combined.Data FROM Table1
LEFT JOIN
(SELECT Key, Data FROM IF(Table1.Filter, Table2, Table3))) AS Combined
ON Table1.Condition = Combined.Key
I want to create a MySQL View that shows all columns of Table1, and a column from either Table2 or Table3 depending on the field on Table1.Filter.
One simple solution is to LEFT JOIN both Table2 and Table3, with NULL on the column that is not applicable. Is there a way to avoid creating 2 columns?
I cannot UNION Table2 and Table3 as they might contain the same Key.
The following should do what you want:
SELECT t1.Filter, t1.Condition,
COALESCE(t2.Data, t3.Data) as Data
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.Filter AND t2.Key = t1.Condition LEFT JOIN
Table3 t3
ON (NOT t1.Filter) AND t3.key = t1.condition;
You cannot have conditionals choosing tables in the FROM. But, you can have conditions in the ON conditions.

Delete all row who are not in an UPDATE

I have a table that i wanna UPDATE with another table so i have something like :
UPDATE table1 JOIN table2 ON table1.id = table2.fk_table1 SET table1.field1 = table2.field1
But i also want to delate all the row from this table who are not in this UPDATE for have something like :
DELETE FROM table1 WHERE id not IN (UPDATE ...)
Their is a way to do that in one optimize sql request or i have to do it in two request?
Thanks
You have to do it in two request as they are two different operation DML operations:
First fire your update statement:
UPDATE table1 JOIN table2 ON table1.id = table2.fk_table1 SET table1.field1 = table2.field1
Then fire your delete request by converting update in select:
DELETE FROM table1 WHERE id not IN (Select...)
Note: Use select with same condition in update command to get the list of records which are updated in first statement.
You can use below query-
DELETE FROM tbl1.* FROM
table1 AS tbl1
LEFT JOIN table2 AS tbl2 ON table1.id = table2.fk_table1
WHERE tbl2.id IS NULL
But it will be slow as per table size and create locking so you should do that first fetch all id from table1 and then delete them.
SELECT tbl1.id FROM
table1 AS tbl1
LEFT JOIN table2 AS tbl2 ON table1.id = table2.fk_table1
WHERE tbl2.id IS NULL
Now delete these ids either put in clause or multiple chunks as per no of records.
DELETE FROM table1 WHERE id IN ();

Linking 3 tables together in MySQL

I'm trying to link 3 database table together through one MySQL request.
Database structure :
Table1 :
table1_id (exemple: 1)
table1_name (exemple: hello world)
Table2 :
table2_id (exemple: empty)
table2_name (exemple: empty)
Table3 :
table3_id (exemple: 1)
table3_name (exemple: random_name
MySQL Request
SELECT * FROM table1 AS a, table2 AS b, table3 AS c
WHERE a.table1_id = b.table2_id
AND a.table1_id = c.table3_id AND table3_name = "random-name"
Problem
The previous request won't display any result because table2 is empty. Do you have an idea how I could get the data coming from table 1 & 2, letting table3's fields empty without using two requests ?
You should change your request to use a LEFT JOIN instead of an INNER JOIN:
select *
from table1 t1
left join table2 t2
on t1.table1_id = t2.table2_id
left join table3 t3
on t1.table1_id = t3.table2_id
and t3.table3_name = 'random-name'
The INNER JOIN produces a set of data if the id exists in all tables. The LEFT JOIN will return records from table1 even if there are no records in table2 or table3.
If you need help learning about join syntax, here is a great visual explanation of joins
Couple thoughts:
Don't use SELECT *, name the columns explicitly
Don't use implicit join syntax; use ON clauses
To preserve rows that do not "match" on a join condition, use a LEFT OUTER JOIN
Therefore, try this:
SELECT a.table1_id, a.table1_name
, b.table2_id, b.table2_name
, c.table3_id, c.table3_name
FROM table1 AS a
LEFT OUTER JOIN table2 AS b
ON b.table2_id = a.table1_id
JOIN table3 AS c
ON c.table3_id = a.table1_id
WHERE c.table3_name = "random-name"

MySQL Appending between 2 tables

I need to do a Appending on MySql between two tables, my first table1 get all data but for the table2 i have only EMAIL :
table1 (ID;FIRSTNAME;LASTNAME;EMAIL;BIRTH;CP) 100 000 Rows
table2 (ID;FIRSTNAME;LASTNAME;EMAIL;BIRTH;CP) 1 000 Rows
Exemple
Table1 :
1;JOHN;DOE;john.doe#gmail.com;1981-06-06 00:00:00;92220
Table2 :
NULL;NULL;NULL;john.doe#gmail.com;NULL;NULL
and I want to UPDATE on table2 all the columns to have this match :
1;JOHN;DOE;john.doe#gmail.com;1981-06-06 00:00:00;92220
Use the multiple-table UPDATE syntax to join the tables on EMAIL and set the fields appropriately:
UPDATE table2 JOIN table1 USING (EMAIL) SET
table2.ID = table1.ID,
table2.FIRSTNAME = table1.FIRSTNAME,
table2.LASTNAME = table1.LASTNAME,
table2.BIRTH = table1.BIRTH
table2.CP = table1.CP