I'm having difficulty deleting the desired rows from my table in MySQL. I'm using a rather complex subquery to select the rows, but for some reason I'm unable to delete them using a similar syntax.
delete * from table1 as t1
where t1.col1 in
(select y.col1
from table2 x
join
(select col1, col2
from table2
where col2 like "%- 2%") y
on x.col2 = replace(y.col2, "- 2", ""));
Again, I can select the exact rows I wanted deleted, but when I change the query to delete I get the following error:
ERROR 1064 (42000): 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...
Any help is greatly appreciated.
Remove the * after DELETE. You don't usually delete individual columns, you delete entire rows.
yep, you should use
delete from table1
or if there are more than one table in the query, you need to enter the name of the table you want to delete from.
delete table1 from table1 inner join table2 on table1.id = table2.t1ID
Drop the *. A DELETE is an all or nothing kind of action.
Try DELETE FROM instead of DELETE * FROM which is not valid.
Related
I have 2 tables with phone numbers.
In Table A I have to delete all phone numbers thats not in Table B.
In Table B the phonenumbers are with conterycode, but in Table A they are without conterycode.
I therefor try to use a command similar to LIKE %Phonenumber%
I have tried this code, but that is giving me a syntax error
DELETE FROM temp_table
WHERE NOT EXISTS (
SELECT *
FROM TableB
WHERE WorkTelephoneNumber LIKE temp_table.%PhoneNumber%
);
#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 '%PhoneNumber%
)' at line 5
You can try below -
DELETE FROM temp_table
WHERE NOT EXISTS (
SELECT *
FROM TableB
WHERE WorkTelephoneNumber LIKE concat('%',temp_table.PhoneNumber,'%')
);
A subquery will be very slow. you can use join instead
Delete t1 from temp_table t1
join TableB on tableB.WorkTelephoneNumber like concat('%',t1.PhoneNumber,'%')
I have been trying to execute the below query in Mysql 5.7.
DELETE FROM tablename1 T1
WHERE EXISTS (
SELECT *
FROM tablename1 T2
WHERE T2.code = T1.code
AND T2.dname = T2.dname
AND T2.created_date > T1.created_date
);
But it throws me an error :
Reason: liquibase.exception.DatabaseException: 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 'T1
This query works fine in Oracle but for some reason 'alias' doesnt work in MySQL 5.7
Is there anyway I can fix this issue within this Mysql 5.7 version.
Thanks in advance.
I am fairly sure the issue is not the alias, but subquerying the table you are deleting from, try this instead.
DELETE T1
FROM tablename1 AS T1
INNER JOIN tablename1 AS T2
ON T2.code = T1.code
AND T2.dname = T2.dname
AND T2.created_date > T1.created_date
;
I'm not positive it will work either, since it is still referencing the delete subject twice; but it is worth trying.
I have the following SQL query:
DELETE FROM table_b b WHERE NOT EXISTS (SELECT * FROM table_a a WHERE a.some_id = b.some_id)
and am getting the following error:
[Err] 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 'b WHERE NOT EXISTS(SELECT * FROM table_a a WHERE a.some_id =
b.some_id)' at line 1
this seems to suggest that aliases cannot be used with SQL delete statements (?)
Yes you can use alias in DELETE query. Just you have use that alias after DELETE keyword than it will work. It specifies that from which table you have delete the records.
Try this:
DELETE b
FROM table_b b
WHERE NOT EXISTS (SELECT * FROM table_a a WHERE a.some_id = b.some_id)
If I'm understanding your query correctly and your wanting to delete all records that are not in table a from table b. A cleaner way to write it might be
DELETE FROM table_b WHERE id NOT IN (SELECT id FROM table_a)
I'm trying to understand how MYSQL determines single-table and multiple table syntax but I can't find this information. The documentation explains how MYSQL handles the two, but it doesn't explain what determines them.
Documentation says:
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.
Take this query for example:
DELETE table FROM table
INNER JOIN other ON other.column = table.column
WHERE other.column2 = ?
LIMIT 1
Is this single or multiple table syntax? There's a JOIN, so you could lean toward multiple, but it's only deleting from one table. My other suspicion is that it's determined when multiple WHERE clauses are used for multiple tables. If you could include examples of both in your answer it would be much appreciated!
EDIT :
I'm asking this question because when performing certain DELETE queries with a LIMIT I get an error that you cannot use LIMIT with multiple-table syntax.
EDIT #2:
In a nutshell, you cannot use ORDER BY or LIMIT if you are joining tables in a DELETE query.
MySQL's documentation states the following
Regarding the "single-table" synatax:
If the ORDER BY clause is specified, the rows are deleted in the order that is specified. The LIMIT clause places a limit on the number of rows that can be deleted.
Regarding the "multi-table" syntax:
For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.
Some testing is revealing in the matter of the ORDER BY limitations.
This is a valid single-table DELETE statement with an ORDER BY:
DELETE FROM table
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
A similar query with an explicit joining condition against one other table results in a syntax error on the ORDER BY, despite only one of the tables being targeted for deletion:
DELETE table1
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY timestamp LIMIT 2' at line 1
Specifying the same query with an implicit join fails the same way
DELETE table1
FROM table1, table2
WHERE
table1.id = table2.id
AND somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY timestamp LIMIT 2' at line 1
Specifying no table after DELETE for deletion in a joined (multi-table) statement so it looks more like a single-table syntax is also a syntax error
DELETE /* no table named here */
FROM table1 JOIN table2 ON table1.id = table2.id
WHERE somecol = 'someval'
ORDER BY timestamp LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE somecol
It's the table named in the DELETE clause:
Finally, using only one table, but with the mult-table syntax (naming a table after the DELETE keyword) does not permit an ORDER BY, so the true identifying difference here appears to be tables named in the DELETE clause to distinguish multi-table from single-table:
This query only involves one table (without a join), but produces a syntax error:
/* name table1 in the DELETE clause */
DELETE table1
/* but not other table is joined */
FROM table1
WHERE somecol = 'someval'
ORDER BY id LIMIT 2
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY id LIMIT 1
If you specify that you DELETE from only 1 table and there are no JOINs involved in your DELETE statement, then it's a single-table syntax. Otherwise, it's a multi-table syntax. I am pretty sure that's what the doc page means.
I think a proof for this interpretation is that down there the page says:
The table_references clause lists the tables involved in the join. Its syntax is described in Section 13.2.8.2, "JOIN Syntax".
Note that in the single-table syntax there's no table_references element.
I'm trying to cross-check a row that exists in two tables using a MySQL query in phpmyadmin and then, if a userID is found in both tables, insert their userID and user name into another table. Here's my code:
INSERT INTO userswithoutmeetings
SELECT user.userID
IF('user.userID'='meeting.userID');
I keep getting plagued by this error:
#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
'IF('user.userID'='meeting.userID')' at line 3
Other statements I've tried have worked but not deposited the values in the table.
Something like
INSERT INTO userswithoutmeetings(userId,userName)
SELECT DISTINCT a.userId, a.userName
FROM table1 a
INNER JOIN table2 b ON (a.userId = b.userId)