I'm having a problem on using this SQL Query
DELETE FROM `acc_reg_num_db` t
WHERE EXISTS
(SELECT 1 FROM `acc_reg_num_db` tt
WHERE t.account_id = tt.account_id
AND t.key = tt.key
AND tt.value > t.value)
AND t.key IN ('#betaminutes', '#online_minute')
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 't WHERE EXISTS (SELECT 1 FROM acc_reg_num_db tt WHERE t.acco' at
line 1
Can anyone help me with this please? Thanks
MySQL requires that you list the table being deleted from. And, you cannot re-use the table being deleted from. So, try this:
DELETE arn
FROM `acc_reg_num_db` arn JOIN
(SELECT account_id, key, MAX(value) as max_value
FROM acc_reg_num_db arn2
GROUP BY account_id, key
) arn2
USING (account_id, key)
WHERE arn.value < arn2.max_value AND
arn.key IN ('#betaminutes', '#online_minute');
You have a couple issues here:
You cannot alias the table acc_reg_num_db you are deleting from
You cannot delete from the table you are selecting from in the subquery -> you need a derived table
Assuming there is an id column, this is what you need:
DELETE FROM `acc_reg_num_db`
WHERE id IN (
SELECT id
FROM (
SELECT id
FROM `acc_reg_num_db` t
WHERE EXISTS
(SELECT 1 FROM `acc_reg_num_db` tt
WHERE t.account_id = tt.account_id
AND t.key = tt.key
AND tt.value > t.value)
AND t.key IN ('#betaminutes', '#online_minute')
) as x
)
;
Also, key and value are reserved SQL keywords and you should avoid using them in column names
Related
I am using the following query, which I saw from another stackoverflow question but I am getting error.
delete from mytable
where myid not in (
select max(myid)
from mytable
group by myid2)
Error:
#1093 - Table 'mytable' is specified twice, both as a target for 'DELETE' and as a separate source for data
Edit 2:
I also tried this query:
delete from mytable
where myid in (
SELECT
myid, COUNT(*)
FROM
mytable
GROUP BY
myid2
HAVING
COUNT(*) > 1)
And got this error:
#1241 - Operand should contain 1 column(s)
In MySQL, you need to use a JOIN for this purpose. I assume you mean something like this:
delete t
from mytable t left join
(select max(myid) as myid
from mytable
group by myid2
) tt
on t.myid = tt.myid
where tt.myid is null;
Where ? is whatever you really want to group by. Your version will not delete anything because the group by and max() use the same column.
I have a table with following columns
CommentId
ParentCommentId
ChildCommentCount
Update PostComment t1
set ChildCommentCount = (select count(*) from PostComment t2 where t2.CommentId = t1.ParentCommentId);
Trying to update ChildCommentCount with total count of where current CommentId shows up as a parent
but this sql above gvies me error like
Error Code: 1093. You can't specify target table 't1' for update in
FROM clause
any ideas how to rewrite this sql?
You can't select same table while you are updating it this can be done by using join
Update PostComment t1
join (
select CommentId ,count(*) total from PostComment group by CommentId
) t2 on(t2.CommentId = t1.ParentCommentId)
set t1.ChildCommentCount = t2.total
I have a MySql table like this,
Session_Id Subscriber_Id Status
-------------------------------
abc 1234 Started
bcd 1235 Started
bcd 1235 Finished
And I need to delete rows with status 'Started' only if followed by a 'Finished'. This is a huge table, so I would be deleting 1000 records at a time.
I read up a few similar threads,
Multiple-table DELETE LIMIT,
DELETE FROM `table` AS `alias` ... WHERE `alias`.`column` ... why syntax error?
and tried the below queries,
mysql> delete a.* from FSESSION as a, FSESSION as b where a.status='Started' and b.status='Finished' and a.session_id=b.session_id limit 1000;
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 'limit 1000' at line 1 ----> If I remove 'limit' this works
mysql> DELETE FROM v USING `FSESSION` AS v WHERE status = 'Started' and exists(select 0 from FSESSION t where t.status='Finished' and v.session_id=t.session_id) limit 1000;
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 'limit 1000' at line 1 ---->
Does'nt work even when limit is removed.
I use MySql version 5.1.56-ndb-7.1.15. Please suggest a way to do this. Thanks!
UPDATED based on your comments
DELETE f
FROM fsession f JOIN
(
SELECT session_id, subscriber_id
FROM fsession
WHERE status IN('Started', 'Finished')
GROUP BY Session_Id, Subscriber_Id
HAVING MAX(status = 'Started') > 0
AND MAX(status = 'Finished') > 0
LIMIT 1000 -- limit here means how many pairs of rows, so it's effectively 2x rows
) q ON f.session_id = q.session_id
AND f.subscriber_id = q.subscriber_id
To know how many rows were deleted use ROW_COUNT()
SELECT ROW_COUNT();
Here is SQLFiddle demo
Try these queries:
CREATE TEMPORARY TABLE FSESSION_TEMP
(
Temp_Session_Id varchar(255)
);
INSERT INTO FSESSION_TEMP
(
SELECT a.Session_Id FROM
FSESSION a,
FSESSION b
WHERE
a.Status = 'Started' and
b.Status = 'Finished' and
a.Session_ID = b.Session_ID
LIMIT 2
);
DELETE FROM
FSESSION
WHERE
Session_ID in (SELECT * FROM FSESSION_TEMP)
;
DROP TABLE FSESSION_TEMP;
Here is the SQL Fiddle. I've set LIMIT at 2 to show that it works. You still need to change it to 1000.
It seems that you need to use a temporary table for this for 2 reasons:
1) MySQL doesn't let you delete from a table when you use the same table in a subquery
2) 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 (Taken straight out of the MYSQL DELETE manual)
There is one way to circumvent the restrictions by using the DELETE syntax with JOIN and joining the original table (where rows will be deleted from) with a derived table (identical to the above temporary one):
DELETE fdel
FROM
FSESSION fdel
JOIN
(
SELECT a.Session_Id
FROM
FSESSION a
JOIN
FSESSION b
ON a.Session_ID = b.Session_ID
WHERE
a.Status = 'Started' and
b.Status = 'Finished'
LIMIT 2
) ftemp
ON ftemp.Session_ID = fdel.Session_ID
;
can i use aggregation function (LAST) in mysql??
if yes then why give me error for following query::
SELECT `user_id`,last(`value`)
FROM `My_TABLE`
group by `user_id`
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 '(value) FROM My_TABLE group by user_id' at line 1
EDIT:: I got answer "last" is not used in MySql. then How to perform it in MySql??
No, There is nothing called LAST in mysql
See the list of aggregated function
EDIT
You can perform the same something like this
select f.user_id, f.value
from (
select MAX(value) as maxval
from my_table group by user_id
) as x inner join my_table as f on f.value = x.maxval
Something like this -
SELECT * FROM table1 t1
JOIN (SELECT depno, MAX(id) max_id FROM table1 GROUP BY depno) t2
ON t1.depno = t2.depno AND t1.id = t2.max_id
There is no "last" function defined in MySQL. Are you just trying to get the last (newest) record?
If so:
SELECT `user_id`, `value`
FROM `My_TABLE`
ORDER BY `user_id` DESC
LIMIT 1;
or
SELECT `user_id`, `value`
FROM `My_TABLE`
WHERE `user_id` = (SELECT MAX(`user_id`));
Because there is no such function called as last() in mysql..
Try to use group, order by clause in mysql
The best way I found how to handle this:
SELECT user_id, json_unquote(json_extract(json_objectagg('value', value), '$.value'))
FROM my_table
GROUP BY user_id
I'm trying to delete all records which aren't the latest version under their name but apparently you can't reference access a table you are modifying in the same query.
I tried this but it doesn't work for the reasons above:
DELETE FROM table
WHERE CONCAT(name, version ) NOT IN (
SELECT CONCAT( name, MAX( version ) )
FROM table
GROUP name
)
How can I get around this?
Cheers
Wrap the inner reference in a derived table.
DELETE FROM table
WHERE Concat(name, version) NOT IN (SELECT nv
FROM (SELECT Concat(name, Max(version))
AS nv
FROM table
GROUP BY name) AS derived)
delete t1
from table_name1 t1, table_name1 t2
where t1.version < t2.version
and t1.name = t2.name;
//creating alias is the need here