sql-delete

Keep only N last records in SQLite database, sorted by date

孤街醉人 提交于 2019-11-27 20:25:18
问题 I have an SQLite database that I need to do the following: Keep only last N records, sorted by date. How do you do that? 回答1: To delete all but the latest 10 records. delete from test where id not in ( select id from test order by date desc limit 10 ) 回答2: According to the SQLite documentation: If SQLite is compiled with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option, then the syntax of the DELETE statement is extended by the addition of optional ORDER BY and LIMIT clauses. (...)

How to delete multiple rows in SQL where id = (x to y)

雨燕双飞 提交于 2019-11-27 17:22:49
I am trying to run a SQL query to delete rows with id's 163 to 265 in a table I tried this to delete less number of rows DELETE FROM `table` WHERE id IN (264, 265) But when it comes to delete 100's of rows at a time, Is there any query similar to above method I am also trying to use this kind of query but failed to execute it DELETE FROM `table` WHERE id IN (SELECT * FROM table WHERE id = ) Please tell me the query to do the above action... If you need to delete based on a list, you can use IN : delete from your_table where id in (value1, value2, ...); If you need to delete based on the result

Remove all table rows from SQLite database table

馋奶兔 提交于 2019-11-27 17:18:02
问题 I want to remove all rows that i entered from my SQLite database table. The table name is tbltask . I tried to drop the table and delete * from table, but those are giving me runtime errors. I want to trigger this event in Button OnClickListner event. Following code is what I tried: String delete = "DELETE FROM "+DATABASE_TABLE; db.rawQuery(delete, null); db.delete(DATABASE_TABLE, null, null); LogCat: 11-15 17:45:04.660: DEBUG/AndroidRuntime(300): Shutting down VM 11-15 17:45:04.660: WARN

Deleting a row based on the max value

感情迁移 提交于 2019-11-27 15:37:58
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? 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 jobPositonId = (SELECT x.id FROM (SELECT MAX(t.jobPostionId) AS id FROM TABLE t) x) Explanation MySQL is only

How to delete a mysql record with jquery [closed]

白昼怎懂夜的黑 提交于 2019-11-27 14:16:19
问题 I have this following code that delete a mysql record and I would like to transform it so It would be a ajax/jquery code so I can stay in the same page after the record has been deleted from the table. Now, it working fine but it not on the same page and I need to refresh the page to see the result. This is only a part of the full code. All the code in on one page. //get the mysql results //this is a repeated region <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['title']; ?></td>

Delete from one table with join

怎甘沉沦 提交于 2019-11-27 12:38:48
I'm trying to delete records from one database based on a selection criteria of another. We have two tables, emailNotification which stores a list of jobs and emails. Then we have jobs. I want to clear out emailNotifications for jobs that have been closed. I found some earlier examples on Stackoverflow that lead me to this type of syntax (I was previously trying to do the join before the where). DELETE FROM emailNotification WHERE notificationId IN ( SELECT notificationId FROM emailNotification e LEFT JOIN jobs j ON j.jobId = e.jobId WHERE j.active = 1 ) I'm getting the error, you can't

SQL DELETE with INNER JOIN

僤鯓⒐⒋嵵緔 提交于 2019-11-27 12:15:42
There are 2 tables, spawnlist and npc , and I need to delete data from spawnlsit . npc_templateid = n.idTemplate is the only thing that "connect" the tables. I have tried this script but it doesn't work. I have tried this: DELETE s FROM spawnlist s INNER JOIN npc n ON s.npc_templateid = n.idTemplate WHERE (n.type = "monster"); Add .* to s in your first line. Try: DELETE s.* FROM spawnlist s INNER JOIN npc n ON s.npc_templateid = n.idTemplate WHERE (n.type = "monster"); If the database is InnoDB then it might be a better idea to use foreign keys and cascade on delete, this would do what you

TSQL Try / Catch within Transaction or vice versa?

大城市里の小女人 提交于 2019-11-27 12:08:36
I'm writing a script that will delete records from a number of tables, but before it deletes it must return a count for a user to confirm before committing. This is a summary of the script. BEGIN TRANSACTION SCHEDULEDELETE BEGIN TRY DELETE -- delete commands full SQL cut out DELETE -- delete commands full SQL cut out DELETE -- delete commands full SQL cut out PRINT 'X rows deleted. Please commit or rollback.' --calculation cut out. END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR

How to write a SQL DELETE statement with a SELECT statement in the WHERE clause?

这一生的挚爱 提交于 2019-11-27 11:52:47
Database: Sybase Advantage 11 On my quest to normalize data, I am trying to delete the results I get from this SELECT statement: SELECT tableA.entitynum FROM tableA q INNER JOIN tableB u on (u.qlabel = q.entityrole AND u.fieldnum = q.fieldnum) WHERE (LENGTH(q.memotext) NOT IN (8,9,10) OR q.memotext NOT LIKE '%/%/%') AND (u.FldFormat = 'Date') ; This is the DELETE statement I have come up with: DELETE FROM tableA WHERE (SELECT q.entitynum FROM tableA q INNER JOIN tableB u on (u.qlabel = q.entityrole AND u.fieldnum = q.fieldnum) WHERE (LENGTH(q.memotext) NOT IN (8,9,10) OR q.memotext NOT LIKE '%

How do I delete multiple rows with different IDs?

妖精的绣舞 提交于 2019-11-27 11:06:16
问题 I want to do something like this: DELETE FROM table WHERE id IN (SELECT ....) How can I do that? 回答1: If you have to select the id: DELETE FROM table WHERE id IN (SELECT id FROM somewhere_else) If you already know them (and they are not in the thousands): DELETE FROM table WHERE id IN (?,?,?,?,?,?,?,?) 回答2: Delete from BA_CITY_MASTER where CITY_NAME in (select CITY_NAME from BA_CITY_MASTER group by CITY_NAME having count(CITY_NAME)>1); 回答3: Disclaim: the following suggestion could be an