mysql-error-1093

Using EXISTS with MySQL

半世苍凉 提交于 2020-01-05 10:16:14
问题 I have this simple query that works on all other database systems, but fails with MySQL: UPDATE points p SET p.userid = 5224 WHERE p.userid = 2532 AND NOT EXISTS ( SELECT 1 FROM points q WHERE q.userid = 5224 AND q.game = p.game ) I get the following error message: #1093 - You can't specify target table 'p' for update in FROM clause Is there any workaround? 回答1: You can't alias the main table in an UPDATE clause. This should work: UPDATE points SET userid = 5224 WHERE userid = 2532 AND NOT

annual change of values with subquery

拥有回忆 提交于 2019-12-25 03:50:39
问题 This query gives a 1093 error: UPDATE values_table as outer_select SET annual_change = sample_value - ( SELECT sample_value FROM values_table WHERE date_sampled = DATE_SUB(outer_select.date_sampled, INTERVAL 1 YEAR) ); I'm trying to set annual_change for every row equal to the current row's sample_value less last year's sample_value . The data doesn't go back to the beginning time, so how can the lack of historical values also be handled? 回答1: Try this UPDATE values_table as a join values

DELETING doubled users (MySQL)

混江龙づ霸主 提交于 2019-12-25 02:46:35
问题 I have two tables. There are users informations from two sites: p_users p_users2 There are 3726 users in first and 13717 in second. Some users in p_users2 are in p_users . I want merge this two tables to the one big table - but rows with same usernames can't be doubled. How can I do this? I tried something like this: DELETE FROM p_users2 WHERE user_id IN ( select p.user_id from p_users p join p_users2 p2 on p.username=p2.username ) After that I should receive a table with unique usernames,

Insert and set value with max()+1 problems

杀马特。学长 韩版系。学妹 提交于 2019-12-17 15:39:22
问题 I am trying to insert a new row and set the customer_id with max()+1. The reason for this is the table already has a auto_increatment on another column named id and the table will have multiple rows with the same customer_id. With this: INSERT INTO customers ( customer_id, firstname, surname ) VALUES ((SELECT MAX( customer_id ) FROM customers) +1, 'jim', 'sock') ...I keep getting the following error: #1093 - You can't specify target table 'customers' for update in FROM clause Also how would I

Deleting a row based on the max value

喜欢而已 提交于 2019-12-17 14:03:53
问题 How can I structure a mySQL query to delete a row based on the max value. I tried WHERE jobPositonId = max(jobPostionId) but got an error? 回答1: Use: DELETE FROM TABLE t1 JOIN (SELECT MAX(jobPositonId) AS max_id FROM TABLE) t2 WHERE t1.jobPositonId = t2.max_id Mind that all the rows with that jobPositonId value will be removed, if there are duplicates. The stupid part about the 1093 error is that you can get around it by placing a subquery between the self reference: DELETE FROM TABLE WHERE

How do I rewrite this MySQL query so it doesn't throw this error: You can't specify target table 'crawlLog' for update in FROM clause?

两盒软妹~` 提交于 2019-12-10 19:46:16
问题 I'm trying to get an id from a companies table where the id is not yet in the crawlLog table. Then I need to insert that companyId into the crawlLog table. I need to do this in one call so that parallel crawlers don't pull the same url after some other crawler has selected a url, but hasn't inserted it into the crawl log yet. I don't want to lock tables because of other problems that generates. I get this error from both queries below: You can't specify target table 'crawlLog' for update in

MySQL DELETE With a Sub-Query using Having and Count

梦想与她 提交于 2019-12-10 15:19:46
问题 Am trying to DELETE several entries using the following Query: First i find the entries that i want to delete using this query: SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1); Then i add this query to the DELETE statement: DELETE FROM account WHERE guid IN (SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1); But i get this error: You can't specify target table 'account' for update in FROM clause 回答1: I think you need to use temporary table to achieve your

SQL : Using the target table in an UPDATE statement in a nested FROM clause

◇◆丶佛笑我妖孽 提交于 2019-12-08 07:34:06
问题 I have a (mysql) database table with the following columns: NAME | String (Unique) STATUS | int UPDATE_COUNT | int (Unique) I want the value of Max(UPDATE_COUNT) to reflect the cumulative number of updates performed on rows in the table. For example, starting with an empty table: Insert - (Name=John, Status=0) - // update count on that row is set to 1 Insert - (Name=Mary, Status=0) - // update count on that row is set to 2 Update - (Name=John, Status=1) - // update count on that row is set to

How to update a table using a select group by in a second one and itself as the data source in MySQL?

和自甴很熟 提交于 2019-12-06 12:49:24
问题 I can do this: SELECT t2.value + sum(t3.value) FROM tableA t2, tableB t3 WHERE t2.somekey = t3.somekey GROUP BY t3.somekey But how to do this? UPDATE tableA t1 SET speed = ( SELECT t2.value + sum(t3.value) FROM tableA t2, tableB t3 WHERE t2.somekey = t3.somekey AND t1.somekey = t3.somekey GROUP BY t3.somekey ) ; MySQL says it's illegal since you can't specify target table t1 for update in FROM clause. 回答1: You can do it by rewriting your query: UPDATE tableA t1, ( SELECT somekey, SUM(value)

How to update a table using a select group by in a second one and itself as the data source in MySQL?

那年仲夏 提交于 2019-12-04 19:28:26
I can do this: SELECT t2.value + sum(t3.value) FROM tableA t2, tableB t3 WHERE t2.somekey = t3.somekey GROUP BY t3.somekey But how to do this? UPDATE tableA t1 SET speed = ( SELECT t2.value + sum(t3.value) FROM tableA t2, tableB t3 WHERE t2.somekey = t3.somekey AND t1.somekey = t3.somekey GROUP BY t3.somekey ) ; MySQL says it's illegal since you can't specify target table t1 for update in FROM clause. You can do it by rewriting your query: UPDATE tableA t1, ( SELECT somekey, SUM(value) value FROM tableB t3 GROUP BY somekey ) t2 SET speed = t1.value + t2.value WHERE t1.somekey = t2.somekey; You