Subquery fails in mysql - mysql

I have a subquery as follows : which will select the id's according to the condition first and delete
the records ,
Delete from post_master_user_map WHERE id IN
(SELECT id FROM `post_master_user_map` WHERE posted_by_user_id=110);
But it gives me the following error :
You can't specify target table 'post_master_user_map' for update in FROM clause
What is wrong with this ? thanks in advance .
UPDATE
This also fails , I dont why
DELETE FROM `post_master_user_map` WHERE `reshare_id` in (SELECT id FROM `post_master_user_map` WHERE
posted_by_user_id=110);

This error occurs when you try to modify a table and select from the same table in sub query.
Anyway to solve that error change your query as follows
Delete from post_master_user_map WHERE posted_by_user_id=110;
For the updated query(in your question) use following
DELETE t1 FROM post_master_user_map as t1 INNER JOIN
post_master_user_map as t2 ON t1.reshare_id=t2.id and t2.posted_by_user_id=110

By this MySQL DELETE FROM with subquery as condition : MySQL does not allow the table you're deleting from be used in a subquery for the condition.

Related

How to resolve ambiguous error in the query

I am using laravel framework for developing API's ,i have one query that is executed without where condition without any error i need to execute with where condition but it's throwing an error
query
select count(*) as aggregate
from `users`
left join `books` on `books`.`book_id` = `books`.`id`
where `access_id` = 5054
SQL Error [1052] [23000]: Column 'access_id' in where clause is
ambiguous
after searching google i got something we have to specify reference of a table name , i added reference name like this where users.access_id =5054 but it's throwing an error like unknown column but in my db i have that column in both users and books table
The problem is its consider as a column so that's why syntax error is coming,try following way it will resolve your problem
select count(*) as aggregate
from `users`
left join `books` on `books`.`book_id` = `books`.`id`
where `users`.`access_id` = 5054

INSERT with GROUP BY in VALUE SELECT Statement - ERROR 1111 (HY000): Invalid use of group function

I am trying to count distinct values after Grouping by id then inserting them into another table with the following query:
INSERT INTO table_aggregate
(id_aggregate, aggregate_column)
(SELECT id_detail, COUNT(DISTINCT(detail_column))
FROM table_detail
GROUP BY id_detail)
ON DUPLICATE KEY UPDATE
aggregate_column = COUNT(DISTINCT(detail_column));
When run I get the error:
ERROR 1111 (HY000): Invalid use of group function
If I run the SELECT statement portion of the query it works fine. Why is it throwing this error?
You can not use COUNT in the UPDATE part. Use VALUES(aggregate_column) instead:
INSERT INTO table_aggregate
(id_aggregate, aggregate_column)
(SELECT id_detail, COUNT(DISTINCT(detail_column))
FROM table_detail
GROUP BY id_detail)
ON DUPLICATE KEY UPDATE
aggregate_column = VALUES(aggregate_column);
http://rextester.com/KTEDM89215
You can put the aggregation inside a subquery. Then you can refer to the computed value in the update
INSERT INTO
table_aggregate (id_aggregate, aggregate_column)
SELECT
*
FROM
(
SELECT
id_detail,
COUNT(DISTINCT(detail_column)) AS count
FROM
table_detail
GROUP BY
id_detail
) AS aggr
ON DUPLICATE KEY
UPDATE
aggregate_column = aggr.count

Error Code: 1093. You can't specify target table

I have written this query
set sql_safe_updates=0;
delete from t1 where id < (select avg(id) from t1);
set sql_safe_updates=1;
but it giving an error
Error Code: 1093. You can't specify target table 't1' for update in FROM clause
please resolve.
You can't specify the same table used in the delete in the rest of the query. One way to solve this is using a join:
delete t
from t1 t join
(select avg(id) as avgid
from t1
)
on id < (select avg(id);
This is a limitation in MySQL. Another way to get around it is to use an additional level of subqueries:
delete from t1 where id < (select avgid from (select avg(id) as avgid from t1));
The reason this works is because MySQL will materialize the subquery. The compiler sort of "forgets" that it is querying from the table bing modified, because of the extra level of subqueries.

Update Table using values from another table

I am trying to update a row called ctcode in a table called partnumber copying values from the row ctcode in a table called families. My SQL statement however returns and "Unknown Column 'families.parent' in 'where clause'" error.
Here is my SQL Statement
UPDATE `partnumber`
SET `partnumber`.`ctcode`=`families`.`ctcode`
WHERE `partnumber`.`partnumber`=`families`.`parent`;
What is wrong with my statement? Is there any more efficient way of doing this?
You can basically join both tables even in UPDATE statements,
UPDATE `partnumber` a INNER JOIN `families` b
ON a.`partnumber` = b.`parent`
SET a.`ctcode`= b.`ctcode`
Works in MySQL 5.5.24-0ubuntu0.12.04.1
UPDATE `partnumber`, `families`
SET `partnumber`.`ctcode`=`families`.`ctcode`
WHERE `partnumber`.`partnumber`=`families`.`parent`;
Try this query
UPDATE partnumber
SET ctcode = f.ctcode
FROM partnumber p
INNER JOIN families f ON p.partnumber = f.parent

MySQL LIMIT on DELETE statement

I put together a test table for a error I recently came across. It involves the use of LIMIT when attempting to delete a single record from a MySQL table.
The error I speak of is "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 'LIMIT 1' at line 1"
The table I put together is called test; it has 3 columns, id, name and created. I populated the table with several records and then attempted to delete one. Below is the statement I used to try and accomplish this.
DELETE t FROM test t WHERE t.name = 'foo' LIMIT 1
Without the use of LIMIT 1, the statement executes just fine, but of course I wouldn't be using LIMIT if there wasn't a need for it.
I'm fully aware that I can use another statement to accomplish this DELETE successfully. See below:
DELETE FROM test WHERE name = 'foo' LIMIT 1
However my question is centered on why the first statement isn't working with LIMIT.
So my question is, what I have done incorrectly with respect to the first statement to generate this error?
simply use
DELETE FROM test WHERE 1= 1 LIMIT 10
the delete query only allows for modifiers after the DELETE 'command' to tell the database what/how do handle things.
see this page
From the documentation:
You cannot use ORDER BY or LIMIT in a multiple-table DELETE.
DELETE t.* FROM test t WHERE t.name = 'foo' LIMIT 1
#Andre If I understood what you are asking, I think the only thing missing is the t.* before FROM.
Use row_count - your_desired_offset
So if we had 10 rows and want to offset 3
10 - 3 = 7
Now the query delete from table where this = that order asc limit 7 keeps the last 3, and order desc to keep the first 3:
$row_count - $offset = $limit
Delete from table where entry = criteria order by ts asc limit $limit
There is a workaround to solve this problem by using a derived table.
DELETE t1 FROM test t1 JOIN (SELECT t.id FROM test LIMIT 1) t2 ON t1.id = t2.id
Because the LIMIT is inside the derived table the join will match only 1 row and thus the query will delete only this row.
First I struggled a bit with a
DELETE FROM ... USING ... WHERE query,...
Since i wanted to test first
so i tried with SELECT FROM ... USING... WHERE ...
and this caused an error , ...
Then i wanted to reduce the number of deletions adding
LIMIT 10
which also produced an error
Then i removed the "LIMIT" and - hurray - it worked:
"1867 rows deleted. (Query took 1.3025 seconds.)"
The query was:
DELETE FROM tableX
USING tableX , tableX as Dup
WHERE NOT tableX .id = Dup.id
AND tableX .id > Dup.id
AND tableX .email= Dup.email
AND tableX .mobil = Dup.mobil
This worked.