I have two tables table1 and table2. I want to delete from table1 based on a condition in table2.
I have the following mysql query:
DELETE FROM table1
INNER JOIN table2 ON table2.col1 = table1.col1
WHERE table2.col2 = '1'
This return a syntax error. Is there something wrong with the above syntax?
You need to specify the table you are deleting from:
DELETE table1
FROM table1 INNER JOIN
table2
USING (col1)
WHERE table2.col2 = '1';
Try this:
DELETE FROM table1
WHERE EXISTS(
SELECT 'C'
FROM table2
WHERE table2.col1 = table1.col1
AND table2.col2 = '1'
)
You could do something like:
DELETE FROM table1 WHERE col1 IN (select col1 from table2 WHERE table2.col2 = '1');
Related
I got 2 tables in same database in MYSQL and I want insert the columnA in table1 to columnA in table2 and my condition is if table1.name is equal to table2.name. I tried this but didnt work
INSERT INTO Table2 (solId, openTime, closingTime, guid)
SELECT solId, openTime, closingTime, guid
FROM Table1
WHERE Table2.name = Table1.name;
You may want an update:
update table2 t2 join
table1 t1
on t1.name = t2.name
set t2.solId = t1.solId,
t2.openTime = t1.openTime,
t2.closingTime = t1.closingTime,
t2.guid = t1.guid;
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id
SET t1.overview=t2.val
WHERE t1.table1_id=(SELECT table2_id
FROM table2
WHERE table2_id=1);
table2 has multiple id values which are 1, so it gives
#1242 - Subquery returns more than 1 row
You need a DISTINCT.
UPDATE table1 AS t1
INNER JOIN table2 AS t2
ON t1.table1_id = t2.table2_id
SET t1.overview = t2.val
WHERE t1.table1_id = (SELECT DISTINCT table2_id FROM table2 WHERE table2_id = 1);
And if table2_id is fixed, why don't you just use 1 like:
UPDATE table1 AS t1
INNER JOIN table2 AS t2
ON t1.table1_id = t2.table2_id
SET t1.overview = t2.val
WHERE t1.table1_id = 1;
Obviously you have more than 1 record in table2 with same ID.
But if it's OK, change your = operator to in operator.
So This will be your code:
UPDATE table1 AS t1 INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id SET t1.overview=t2.val where t1.table1_id in ( SELECT table2_id
FROM table2 WHERE table2_id=1);
Edit:
No need to subquery, it's redundant. Check this out:
UPDATE table1 AS t1 INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id SET t1.overview=t2.val where table2_id=1;
If you indeed want to compare t1.table1_id against multiple values, use:
in instead of =
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.table1_id=t2.table2_id
SET t1.overview=t2.val WHERE t1.table1_id
IN ( SELECT xxx FROM table2 WHERE table2_id=1);
BTW if you are only returning table2_id from the inner query, you can skip the inner query altogether.
I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )
I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;
You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'
Case:
How to update table1 with data from table2 where id is equal?
Problem:
When I run the following update statement, it updates all the records in table1 (even where the id field in table1 does not exist in table2).
How can I use the the multiple update table syntax, to update ONLY the records in table1 ONLY where the id is present in table2 and equal?
UPDATE table1,table2
SET table1.value=table2.value
WHERE table2.id=table1.id
Thanks in advance.
here's the correct syntax of UPDATE with join in MySQL
UPDATE table1 a
INNER JOIN table2 b
ON a.ID = b.ID
SET a.value = b.value
SQLFiddle Demo
EDIT
For MySql it'll be
UPDATE table1 t1 INNER JOIN
table2 t2 ON t2.id = t1.id
SET t1.value = t2.value
sqlfiddle
Original answer was for SQL Server
UPDATE table1
SET table1.value = table2.value
FROM table1 INNER JOIN
table2 ON table2.id=table1.id
sqlfiddle
You can try this:
UPDATE TABLE1
SET column_name = TABLE2.column_name
FROM TABLE1, TABLE2
WHERE TABLE1.id = TABLE2.id
UPDATE table1
SET table1.value = (select table2.value
WHERE table2.id=table1.id)
I have the following simple query which works and does what I want:
SELECT
Table1.*,
Table2.*
FROM
Table1, Table2
WHERE
Table1.Col1 != '1' AND
Table2.Col2 LIKE 'Miami' AND
(Table2.Col3 LIKE 'USA' OR Table2.Col2 LIKE 'US') AND
Table1.Col1 = Table2.Col1
I now need to sort those results by the SUM of the corresponding values in Table3 (SUM(Table3.Col2)) such that Table3.Col1 = Table1.Col1 and Table3.Col1 = Table2.Col1.
I thought that simply adding those two lines in the WHERE clause plus SUM(Table3.Col2) in the SELECT clause would do the trick, but that did not work and only returned one result with the total sum of all the Table3.Col2 values, and not just the ones corresponding the matching rows in the other 2 tables.
How would I do this?
Try this:
SELECT
Table1.*,
Table2.*
FROM
Table1 JOIN Table2 on Table1.Col1 = Table2.Col1
JOIN (SELECT Col1, SUM(Table3.Col2) as `SumCol` FROM Table3) as SumTable ON Table2.Col1 = SumTable.Col1
WHERE
Table1.Col1 != '1' AND
Table2.Col2 LIKE 'Miami' AND
(Table2.Col3 LIKE 'USA' OR Table2.Col2 LIKE 'US') AND
Table1.Col1 = Table2.Col1
ORDER BY SumTable.SumCol DESC