sql-delete

Keep only last 5 search results of user in a table

喜夏-厌秋 提交于 2019-12-01 09:25:53
问题 I need to keep the last 5 search results of a user in a table. I wrote a script to remove other rows, but it didn't work: DELETE FROM SELECT ROW_NUMBER () OVER (ORDER BY search_time DESC) AS row_number; FROM history_user WHERE user_id = 188 WHERE row_number>5 What did I do wrong? 回答1: Proper syntax as detailed in the manual: DELETE FROM history_user h USING ( SELECT pk_id, row_number() OVER (ORDER BY search_time DESC) AS rn; FROM history_user WHERE user_id = 188 ) sub WHERE sub.rn > 5 AND h

I can´t use alias in sql delete

依然范特西╮ 提交于 2019-12-01 08:50:30
I tried execute this sql sentence delete from reclamo r where exists ( select 1 from reclamo r join cliente c on r.cod_cliente = c.cod_cliente join localidad l on c.cod_localidad = l.cod_localidad where l.descripcion = 'San Justo'); for remove all the claims made by customers of the town of 'San justo' but it says "Error Code: 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 'r where exists ( select 1 from reclamo r join cliente c on r.cod_cliente' at line 2" anyone know how I can fix these errors?

Leave only first 50 records in SQL database and delete the rest

岁酱吖の 提交于 2019-12-01 07:31:12
I have a table of scores, which is made of 2 fields: name and high score . Something like this: ----------------------- | name | score | ----------------------- | John | 2450 | ----------------------- | Alice | 2420 | ----------------------- ... etc I need to delete all the rows till the top 50 scores. Is it possible without creating another temporary table ? please try this delete from scores_tbl Where id not in (select * from (select id from scores_tbl order by score desc limit 50) as temp) Create an auto increment field alter table scores add id int unique auto_increment not null; This will

Remove duplicate rows from table with join

自闭症网瘾萝莉.ら 提交于 2019-12-01 06:52:59
I have two table to contain state (state_table) and city (city_table) of countries The city table is having state_id to relate it with state_table Both the tables are already having data in it. Now the problem City table contains multiple entries of a city within one state. And another cities may or may not have the same city name as well e.g.: cityone will have 5 occurrence in the city table with stateone and 2 occurrence with statetwo So how will I write a query to keep one city for each state and delete the rest? Schema follows CREATE TABLE IF NOT EXISTS `city_table` ( `id` int(11) NOT NULL

mysql delete on join?

喜欢而已 提交于 2019-12-01 05:16:32
I can use select * from sent_txts s LEFT JOIN received_txts r ON s.msg_link_id = r.id WHERE r.action_id = 6; top select matching rows, How can I write a query to delete the matching rows on both sides? Something like delete sent_txts s LEFT JOIN received_txts r ON s.msg_link_id = r.id WHERE r.action_id = 6; Disclaimer: I do not have access to a mysql database to test at the moment, but I think you can use: delete s, r from send_txts s left join received_txts r on s.msg_link_id = r.id where r.action_id = 6; See mysql's delete documentation for more information. A better method might be to put a

How to Delete Records NOT IN

半世苍凉 提交于 2019-12-01 04:07:26
Hi I have the following SQL Query which gives me Scheme_Id which exist both in ProjectSchemes and Schemes table. I want to delete all records from Schemes table which have no record to ProjectSchemes table. How can I do so? Please help. I'm using MSSQL select scheme_id from Schemes where Scheme_Id in(select s.Scheme_Id from Projects p inner join ProjectSchemes ps on ps.Project_Id=p.Project_Id inner join Schemes s on s.Scheme_Id=ps.Scheme_Id) I'm trying to do the following but it's not working. Not working means no records affected but as I checked my Schemes table there are so many records

How to delete data from multiple tables?

浪尽此生 提交于 2019-12-01 02:13:34
I have these tables: event (evt_id, evt_code, reg_id) magnitude (mag_id, evt_id, value) trace (trace_id, pt_id) point (pt_id, evt_id) I want to delete all rows from all tables related to evt_id=1139 . How do I do it? Justin Pihony If you have control over your schema, I would make the schema use cascading deletes . From the article (the more pertinent portion translated for your example) CREATE TABLE point ( pt_id integer PRIMARY KEY, evt_id integer REFERENCES event ON DELETE CASCADE ) If you have cascades set up, then you can just delete from the main event table and all the other tables will

mysql delete on join?

走远了吗. 提交于 2019-12-01 02:09:18
问题 I can use select * from sent_txts s LEFT JOIN received_txts r ON s.msg_link_id = r.id WHERE r.action_id = 6; top select matching rows, How can I write a query to delete the matching rows on both sides? Something like delete sent_txts s LEFT JOIN received_txts r ON s.msg_link_id = r.id WHERE r.action_id = 6; 回答1: Disclaimer: I do not have access to a mysql database to test at the moment, but I think you can use: delete s, r from send_txts s left join received_txts r on s.msg_link_id = r.id

DELETE all duplicate topics with few conditions

喜你入骨 提交于 2019-11-30 22:46:36
I'm trying to make sql who will delete all duplicate titles BUT must delete duplicates with these conditions: must delete only duplicates with same object_id must keep only the newest record (biggest topic_id ) (topic_id is the unique id for every topic AI) So far I've done that (testing with select...) SELECT topic_id,object_id,title,url,date FROM topics GROUP BY title HAVING ( COUNT(title) > 1) ORDER BY topic_id DESC But doesn't meet the conditions. I'm using mysql. In MySQL , you cannot specify the target table to a DML operation in a subquery (unless you nest it more than one level deep,

Can I do a mysql Select, Update and Delete in one query?

被刻印的时光 ゝ 提交于 2019-11-30 21:35:05
Can I say that one of many ways to optimize mysql is to reduce the number of queries? If that so, can I do this: - Select "data" => $A from table X - Update $A from table Y - Delete $A from table X in one query? You can't reduce the number of queries - they all do different things - but you could reduce the number of round trips to the database and the number of parses by wrapping it all as a PLSQL function. However you can't select the data after you've deleted it.....but consider: CREATE PROCEDURE s_u_d(a) BEGIN UPDATE tab_x SET tab_x.avalue=1 WHERE tab_x.another=a; DELETE FROM tab_y WHERE