sql-delete

MySQL LIMIT on DELETE statement

吃可爱长大的小学妹 提交于 2019-11-26 20:51:25
问题 I put together a test table for a error I recently came across. It involves the use of LIMIT when attempting to delete a single record from a MySQL table. The error I speak of is " 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 1' at line 1 " The table I put together is called test ; it has 3 columns, id , name and created . I populated the table with several records and then attempted to delete one.

SQL DELETE with JOIN another table for WHERE condition

▼魔方 西西 提交于 2019-11-26 20:28:14
I have to delete rows from guide_category that have no relation with guide table (dead relations). Here is what I want to do, but it of course does not work. DELETE FROM guide_category AS pgc WHERE pgc.id_guide_category IN (SELECT id_guide_category FROM guide_category AS gc LEFT JOIN guide AS g ON g.id_guide = gc.id_guide WHERE g.title IS NULL) Error: You can't specify target table 'guide_category' for update in FROM clause Due to the locking implementation issues, MySQL does not allow referencing the affected table with DELETE or UPDATE . You need to make a JOIN here instead: DELETE gc.* FROM

How do I delete all the duplicate records in a MySQL table without temp tables

萝らか妹 提交于 2019-11-26 18:44:29
I've seen a number of variations on this but nothing quite matches what I'm trying to accomplish. I have a table, TableA , which contain the answers given by users to configurable questionnaires. The columns are member_id, quiz_num, question_num, answer_num . Somehow a few members got their answers submitted twice. So I need to remove the duplicated records, but make sure that one row is left behind. There is no primary column so there could be two or three rows all with the exact same data. Is there a query to remove all the duplicates? Saharsh Shah Add Unique Index on your table: ALTER

Mysql delete statement with limit

旧巷老猫 提交于 2019-11-26 17:49:50
问题 I'm trying to delete rows from a table but I get an error. DELETE FROM `chat_messages` ORDER BY `timestamp` DESC LIMIT 20, 50; I get this error at 50: 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 ' 50' at line 1 No idea what's wrong. 回答1: You cannot specify offset in DELETE 's LIMIT clause. So the only way to do that is to rewrite your query to something like: DELETE FROM `chat_messages` WHERE id IN

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

末鹿安然 提交于 2019-11-26 15:48:45
问题 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

MySQL DELETE FROM with subquery as condition

假如想象 提交于 2019-11-26 14:13:02
I am trying to do a query like this: DELETE FROM term_hierarchy AS th WHERE th.parent = 1015 AND th.tid IN ( SELECT DISTINCT(th1.tid) FROM term_hierarchy AS th1 INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015) WHERE th1.parent = 1015 ); As you can probably tell, I want to delete the parent relation to 1015 if the same tid has other parents. However, that yields me a syntax 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 'AS th WHERE th.parent = 1015 AND th.tid IN ( SELECT

Best way to delete millions of rows by ID

被刻印的时光 ゝ 提交于 2019-11-26 12:22:09
问题 I need to delete about 2 million rows from my PG database. I have a list of IDs that I need to delete. However, any way I try to do this is taking days. I tried putting them in a table and doing it in batches of 100. 4 days later, this is still running with only 297268 rows deleted. (I had to select 100 id\'s from an ID table, delete where IN that list, delete from ids table the 100 I selected). I tried: DELETE FROM tbl WHERE id IN (select * from ids) That\'s taking forever, too. Hard to

In SQL, is UPDATE always faster than DELETE+INSERT?

♀尐吖头ヾ 提交于 2019-11-26 12:04:54
问题 Say I have a simple table that has the following fields: ID: int, autoincremental (identity), primary key Name: varchar(50), unique, has unique index Tag: int I never use the ID field for lookup, because my application is always based on working with the Name field. I need to change the Tag value from time to time. I\'m using the following trivial SQL code: UPDATE Table SET Tag = XX WHERE Name = YY; I wondered if anyone knows whether the above is always faster than: DELETE FROM Table WHERE

delete rows from multiple tables

大城市里の小女人 提交于 2019-11-26 11:24:57
问题 I\'m trying to use SQL to delete multiple rows from multiple tables that are joined together. Table A is joined to Table B Table B is joined to Table C I want to delete all rows in table B & C that correspond to a row in Table A CREATE TABLE `boards` ( `boardid` int(2) NOT NULL AUTO_INCREMENT, `boardname` varchar(255) NOT NULL DEFAULT \'\', PRIMARY KEY (`boardid`) ); -- -------------------------------------------------------- -- -- Table structure for table `messages` -- CREATE TABLE

How do you enable LIMIT for DELETE in SQLite?

♀尐吖头ヾ 提交于 2019-11-26 11:23:45
问题 Using PHP, I have a simple database that may store multiple items with the same content. I want to delete the first occurrence of an instance when I use DELETE. How do you enable LIMIT for DELETE in SQLite using PHP? 回答1: You cannot enable these options from within PHP, you need to compile SQLite yourself in order to enable these options. Importantly, you need to download the full version, not the amalgamation source release from SQLite download page. If you're on Unix, get the sqlite-3.6.20