I'm trying to delete duplicate rows according to some fileds.
When I'm running the query below:
delete
from slowmo_vid as sv1, slowmo_vid as sv2
where sv1.video_id = '2luh6g3ni5ex'
and sv1.slowmo_end_t<=sv2.slowmo_end_t;
I'm getting the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as sv1, slowmo_vid as sv2
where sv1.video_id = '2luh6g3ni5ex'
and sv1.slowmo_end' at line 2
The fields of the tables are : id, video_id internal_uri, slowmo_end_t
You seem to be trying to do an ANSI-92 style inner join inside a DELETE statement. But the WHERE clause cannot simultaneously be used to enforce the join and enforce a restriction on a result set. Instead, do the following explicit INNER JOIN to remove the records you want. Notice that it is clear what role the WHERE clause is playing.
Update: If you want to delete all records except for the one containing the max video_id then you can add a nested subquery to the WHERE clause.
DELETE sv1.*
FROM slowmo_vid sv1
INNER JOIN slowmo_vid sv2 ON sv1.slowmo_end_t <= sv2.slowmo_end_t
WHERE sv1.video_id = '2luh6g3ni5ex' AND
sv1.video_id <> (SELECT x.id
FROM (SELECT MAX(t.video_id) AS id
FROM slowmo_vid t) x)
You can specify multiple tables in a DELETE statement to delete rows
from one or more tables depending on the particular condition in the
WHERE clause. However, you cannot use ORDER BY or LIMIT in a
multiple-table DELETE. The table_references clause lists the tables
involved in the join. Its syntax is described in Section 12.2.8.1,
“JOIN Syntax”.
http://dev.mysql.com/doc/refman/5.6/en/delete.html
The example in the manual is:
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
You cannot use aliases on DELETE statement. You can try with:
DELETE FROM myAlias USING `my_table` AS myAlias
Or try without any aliases
Related
How come this query:
SELECT *
FROM `store_catalog_product_option`
JOIN `store_catalog_product_option_type_value`
WHERE `product_id`=15676
AND `store_catalog_product_option_type_value`.`sku` LIKE '%UNIT_%'
retrieve data.
But replacing
select *
with
delete
as such
DELETE
FROM `store_catalog_product_option`
JOIN `store_catalog_product_option_type_value`
WHERE `product_id`=15676
AND `store_catalog_product_option_type_value`.`sku` LIKE '%UNIT_%'
give syntax error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN store_catalog_product_option_type_value WHERE product_id=15676 AND `sto' at line 1
For multi-table deletes,
For the first multiple-table syntax, only matching rows from the tables listed before the FROM clause are deleted. the second multiple-table syntax, only matching rows from the tables listed in the FROM clause (before the USING clause) are deleted.
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Or:
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
And for LEFT JOIN, you should use something like
DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;
Your JOIN should have an ON part
You can try somthing like this:-
DELETE FROM `store_catalog_product_option` A
JOIN `store_catalog_product_option_type_value' B
ON A.ID = B.ID
WHERE A.`product_id`=15676
AND B.`sku` LIKE '%UNIT_%'
Alternatively you can use:-
DELETE A
FROM `store_catalog_product_option` A
JOIN `store_catalog_product_option_type_value' B
ON A.ID = B.ID
WHERE A.`product_id`=15676
AND B.`sku` LIKE '%UNIT_%'
...to delete only from store_catalog_product_option
I needed indeed to use a multiple table syntax, including also a ON condition for the JOIN.
Since i wanted to delete rows in both tables , here is my working query, after your help:
DELETE O, V FROM `store_catalog_product_option` O JOIN `store_catalog_product_option_type_value` V on O.option_id=V.option_id WHERE `product_id`=15676 AND V.`sku` LIKE '%UNIT_%'
I have just studied FROM clause and derived tables in mysql and most of the websites provided the examples using SELECT command
Example SELECT * FROM (SELECT * FROM usrs) as u WHERE u.name = 'john'
But when I have tried using delete or update command it does not seem to work.
Example DELETE FROM (SELECT * FROM usrs) as u WHERE u.name = 'john'
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near (SELECT * FROM usrs) as u WHERE u.name = 'john' at line
UPDATE (SELECT * FROM usrs) as u SET u.lname ='smith' WHERE u.name = 'john'
1288 The target table e of the UPDATE is not updatable
So derived tables does not work with delete or update commands? or is there a way to make it work.
Instead of writing the table name for update and delete I want to write a subquery that gets the records and perform the delete operation on that records? Is that possible in mysql?
UPDATED I have to delete a record and i have three tables, the record may exist in any of the table
My approach delete from first table rows effected? quit: else check second table rows effected? quit : else check third table
But if I use UNION ALL I can do this way
Delete from (select * from tb1 union all select * from tb2 union all select * from tb3) e as e.uname = 'john'
but this query does not seem to work , now could anyone tell me how do i delete or update a record when i have more than one table to search. Any help is greatly appreciated.
You can't directly delete from the subquery, but you can still use it if you'd like, you'll just need to use it in a JOIN:
DELETE usrs
FROM usrs
INNER JOIN (
SELECT * FROM usrs WHERE name = 'john'
) t ON usrs.Id = t.Id
Or you could use IN:
DELETE usrs
WHERE ID IN (
SELECT ID
FROM usrs
WHERE name = 'John'
)
With this said, for this example, I don't know why you'd want a subquery:
DELETE usrs WHERE name = 'John'
Edit base on comments. To delete from multiple tables at the same time, you can either have multiple DELETE statements, or you can use something like the following:
delete t1, t2, t3
from (select 'john' as usr) t
left join t1 on t.usr=t1.usr
left join t2 on t.usr=t2.usr
left join t3 on t.usr=t3.usr
SQL Fiddle Demo
Derived tables exist only for the duration of the parent query they're a member of. Assuming that this syntax and the operations were allowed by MySQL, consider what happens:
a) Your main query starts executing
b) the sub-query executes and returns its results as a temporary table
c) the parent update changes that temporary table
d) the parent query finishes
e) temporary tables are cleaned up and deleted
Essentially you'll have done nothing except waste a bunch of cpu cycles and disk bandwidth.
UPDATE queries DO allow you to join against other tables to use in the WHERE clause, e.g..
UPDATE maintable
LEFT JOIN othertable ON maintable.pk = othertable.fk
SET maintable.somefield='foo'
WHERE othertable.otherfield = 'bar'
I want to update a row based on the existence of an id in other table that is also in that table. In other words that id is a primary key in other table and just a column in the table I want to update.
I want to update based in the same id.
I have this query but it doesn't work because of the SQL syntax.
UPDATE
transaction
SET
DaysRented = 3,
Cost = 3,
TotalCost= 5
FROM
transaction
INNER JOIN
rentals
ON
transaction.idRentals = rentals.idRentals;
syntax for mysql
http://www.mysqltutorial.org/mysql-update-join/
First, you specify the main table ( T1) and the table that you want
the main table to join to ( T2) after the UPDATE clause. Notice that
you must specify at least one table after the UPDATE clause. The
data in the table that is not specified after the UPDATE clause is
not updated.
Second, you specify a kind of join you want to use i.e., either
INNER JOIN or LEFT JOIN and a join condition. Notice that the JOIN
clause must appear right after the UPDATE clause.
Third, you assign new values to the columns in T1 and/or T2 tables
that you want to update.
UPDATE transaction
INNER JOIN rentals ON transaction.idRentals = rentals.idRentals
SET DaysRented = 3,
Cost = 3,
TotalCost = 5;
You are using SQL Server update/join syntax. The proper MySQL syntax is:
UPDATE transaction INNER JOIN
rentals
ON transaction.idRentals = rentals.idRentals
SET DaysRented = 3,
Cost = 3,
TotalCost = 5;
I've done a similar JOIN in a UPDATE script, but I used the same table in the SET and WHERE clause. In this DELETE script I need to delete from one table where a condition is true in another table. For example:
DELETE FROM `db_A`.`table_A`
JOIN `db_B`.`table_B`
ON `table_A`.`id` = `table_B`.`id`
WHERE `table_B`.`name` = 'Remove Me'
Can I do something like this?
The MYSQL documentation makes it very clear that yes you can do this
You can specify multiple tables in a
DELETE statement to delete rows from
one or more tables depending on the
particular condition in the WHERE
clause. However, you cannot use ORDER
BY or LIMIT in a multiple-table
DELETE. The table_references clause
lists the tables involved in the join.
Its syntax is described in Section
12.2.8.1, “JOIN Syntax”.
For the first multiple-table syntax,
only matching rows from the tables
listed before the FROM clause are
deleted. For the second multiple-table
syntax, only matching rows from the
tables listed in the FROM clause
(before the USING clause) are deleted.
The effect is that you can delete rows
from many tables at the same time and
have additional tables that are used
only for searching:
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Or:
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
These statements use all three tables
when searching for rows to delete, but
delete matching rows only from tables
t1 and t2.
The preceding examples use INNER JOIN,
but multiple-table DELETE statements
can use other types of join permitted
in SELECT statements, such as LEFT
JOIN. For example, to delete rows that
exist in t1 that have no match in t2,
use a LEFT JOIN:
DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;
The syntax permits .* after each tbl_name for compatibility with Access.
From Delete Manual, the table you want to delete goes between DELETE and FROM. Unless you want to delete from all tables involved in the join
DELETE `db_A`.`table_A` FROM `db_A`.`table_A`
JOIN `db_B`.`table_B`
ON `table_A`.`id` = `table_B`.`id`
WHERE `table_B`.`name` = 'Remove Me'
As a general answer, yes. You can join to the second table with the condition, as you have stated, or pull the ids in a subcommand, like:
DELETE FROM T1
WHERE id in (SELECT id FROM T1 JOIN T2 ON X = Y WHERE T2.Val = "X")
This is a bit heavy weight, but if T2 is the child, you can reduce this to:
(SELECT Y from T2 WHERE Val = "X")
Make sense?
I'm looking to delete information in two different tables in 1 query, based on an ID.
I've tried several solutions on here to accomplish this task but still have not accomplished what I'm trying to do.
Table 1 - Content
---------- ---------
ContentID | Content
--------------------
Table 2 - Votes
---------------------------
VoteID | ContentID | Vote
---------------------------
I want to delete the content row based on its ID and any or all votes (there could be 0 vote records). I do NOT want to use transactions, cascading deletes, or use 2 different queries.
What is best here - a LEFT JOIN? INNER JOIN?
Any help here would be greatly appreciated.
DELETE Content, Votes
FROM Content
LEFT JOIN Votes
ON Votes.ContentID = Content.ContentID
WHERE Content.ContentID = ?
If you have a relation you can try with this ON DELETE CASCADE option.
Another option is to create a stored procedure and do the delete in 2 steps but with only one server call.
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Although this is for 3 tables, I'm sure you'll be able to adapt it to your needs.
You can specify multiple tables in a DELETE statement to delete rows from one or more tables depending on the particular condition in the WHERE clause. However, you cannot use ORDER BY or LIMIT in a multiple-table DELETE. The table_references clause lists the tables involved in the join. Its syntax is described in Section 12.2.7.1, “JOIN Syntax”.
The first multiple-table DELETE syntax is supported starting from MySQL 4.0.0. The second is supported starting from MySQL 4.0.2.
For the first multiple-table syntax, only matching rows from the tables listed before the FROM clause are deleted. For the second multiple-table syntax, only matching rows from the tables listed in the FROM clause (before the USING clause) are deleted. The effect is that you can delete rows from many tables at the same time and have additional tables that are used only for searching:
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;
Or:
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;
These statements use all three tables when searching for rows to delete, but delete matching rows only from tables t1 and t2.
This link can be useful "http://dev.mysql.com/doc/refman/4.1/en/delete.html"
Without using "JOINS" the simplest I could come up with while I was searching for a solution was DELETE a.*,b.* FROM table1 AS a, table2 AS b WHERE a.id = b.id AND a.field = 1