sql-delete

Trying to delete from multiple tables using SQL

被刻印的时光 ゝ 提交于 2019-12-09 21:14:17
问题 I have 4 tables in our application: User usession upklist projshare The last three tables contain a field called session_id . In the code below, the section in parenthesis works to get all session_id values from usession table for user "awpeople". The problem is how do I read this result set into an array and delete from all three tables where session_id is in the array results. Code: DELETE FROM usession, upklist, projshar WHERE session_id = (SELECT session_id FROM usession WHERE delete

Deleting duplicates rows from redshift

99封情书 提交于 2019-12-09 14:49:56
问题 I am trying to delete some duplicate data in my redshift table. Below is my query:- With duplicates As (Select *, ROW_NUMBER() Over (PARTITION by record_indicator Order by record_indicator) as Duplicate From table_name) delete from duplicates Where Duplicate > 1 ; This query is giving me an error. Amazon Invalid operation: syntax error at or near "delete"; Not sure what the issue is as the syntax for with clause seems to be correct. Has anybody faced this situation before? 回答1: Redshift being

Counting the number of deleted rows in a SQL Server stored procedure

こ雲淡風輕ζ 提交于 2019-12-09 07:23:08
问题 In SQL Server 2005, is there a way of deleting rows and being told how many were actually deleted? I could do a select count(*) with the same conditions, but I need this to be utterly trustworthy. My first guess was to use the @@ROWCOUNT variables - but that isn't set, e.g. delete from mytable where datefield = '5-Oct-2008' select @@ROWCOUNT always returns a 0. MSDN suggests the OUTPUT construction, e.g. delete from mytable where datefield = '5-Oct-2008' output datefield into #doomed select

Delete N number of old records from table in mysql

此生再无相见时 提交于 2019-12-09 06:28:42
问题 I have a LoginTime table like this: id | user_id | datetime 1 | 1 | 2011-01-19 18:51:01 2 | 1 | 2011-01-19 18:51:02 3 | 1 | 2011-01-19 18:51:03 4 | 1 | 2011-01-19 18:51:04 5 | 1 | 2011-01-19 18:51:05 6 | 1 | 2011-01-19 18:51:06 7 | 1 | 2011-01-19 18:51:07 8 | 1 | 2011-01-19 18:51:08 9 | 1 | 2011-01-19 18:51:09 10 | 2 | 2011-01-19 18:51:10 I want to keep only 5 latest(by 'datetime' column) records and delete all previous records where user_id=1 Is it possible to achieve this with one mysql

How to delete data from multiple tables?

旧城冷巷雨未停 提交于 2019-12-09 02:09:06
问题 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? 回答1: 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

DELETE SQL with correlated subquery for table with 42 million rows?

一笑奈何 提交于 2019-12-08 20:19:59
问题 I have a table cats with 42,795,120 rows. Apparently this is a lot of rows. So when I do: /* owner_cats is a many-to-many join table */ DELETE FROM cats WHERE cats.id_cat IN ( SELECT owner_cats.id_cat FROM owner_cats WHERE owner_cats.id_owner = 1) the query times out :( (edit: I need to increase my CommandTimeout value, default is only 30 seconds) I can't use TRUNCATE TABLE cats because I don't want to blow away cats from other owners. I'm using SQL Server 2005 with "Recovery model" set to

MySQL: Why is DELETE more CPU intensive than INSERT?

冷暖自知 提交于 2019-12-08 19:25:39
问题 I'm currently taking the course "Performance Evaluation" at university, and we're now doing an assignment where we are testing the CPU usage on a PHP and MySQL-database server. We use httperf to create custom traffic, and vmstat to track the server load. We are running 3000 connections to the PHP-server, for both INSERT and DELETE (run separately). Numbers show that the DELETE operation is a lot more CPU intensive than INSERT — and I'm just wondering why? I initially thought INSERT required

Delete from table if the id doesn't exists in another table

我怕爱的太早我们不能终老 提交于 2019-12-08 18:01:06
问题 I want to delete the id's from types that can't be found in types_photos but I don't know how I can accomplish this. id_type in types_photos are the same as id in types . Here's how the table's structure looks like: CREATE TABLE IF NOT EXISTS `types` ( `id` int(11) NOT NULL AUTO_INCREMENT, `id_user_added` int(11) DEFAULT '0', `id_user_edited` int(11) DEFAULT '0', `data_name` text NOT NULL, `data_name_seo` text NOT NULL, `data_type` enum('tag','equipment','search') NOT NULL, `datetime_added`

Mass delete unpopular Tags

别来无恙 提交于 2019-12-08 17:34:27
Mass delete unpopular Tags I have 1000's of tags and I would like to simply delete all tags that aren't used more than X times... i.e. 5 times. Does anyone know of a simple way to do this? Even straight SQL would totally ROCK! $x = 5; // set this to any number $sql = "SELECT `name` FROM `wp_terms`"; $result = mysql_query($sql); $count = array(); while($row = mysql_fetch_assoc($result)) { $count[$name]++; } foreach($count as $key = $value) { if($value < $x) { $sql2 = "DELETE FROM `wp_terms` WHERE `name` = '". $key ."'"; $result2 = mysql_query($sql2); } } There's more efficient ways of doing it

sql delete result from query

纵饮孤独 提交于 2019-12-08 13:50:06
问题 I have two tables, same structure clients contains row 1+2 clients3 only row 2 I want to delete row 2 in the clients table SELECT * FROM clients WHERE EXISTS (SELECT * FROM clients3 WHERE clients3.id = clients.id)) gives me the row 2. But I do not know how to delete. DELETE * FROM clients WHERE (SELECT * FROM clients WHERE EXISTS (SELECT * FROM clients3 WHERE clients3.id = clients.id)) does not work. 回答1: You need to create temporary table for the subquery, DELETE FROM clients WHERE ID IN (