sql-delete

Data deleted with IN clause even if the column doesn't exist

十年热恋 提交于 2019-12-02 07:04:15
问题 So, here is the test query you could play with: select top 10 * into #tmp FROM A delete from #tmp WHERE xxx_id in (select xxx_id FROM B) Actually, all these 10 records are deleted. The question is why are these 10 records deleted? Note: xxx_id is one column in table A only, it doesn't exist in table B . But the delete statement "works" anyway. Here's a demonstration of this behaviour: http://sqlfiddle.com/#!6/963f9/1/1 Update I found the answer in MSDN: http://social.msdn.microsoft.com/Forums

MySQL Before Delete trigger to avoid deleting multiple rows

断了今生、忘了曾经 提交于 2019-12-02 06:18:02
I am trying to avoid deletion of more than 1 row at a time in MySQL by using a BEFORE DELETE trigger. The sample table and trigger are as below. Table test: DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `a` int(11) NOT NULL, `b` int(11) NOT NULL, PRIMARY KEY (`id`)); INSERT INTO `test` (`id`, `a`, `b`) VALUES (1, 1, 2); INSERT INTO `test` (`id`, `a`, `b`) VALUES (2, 3, 4); Trigger: DELIMITER // DROP TRIGGER IF EXISTS prevent_multiple_deletion; CREATE TRIGGER prevent_multiple_deletion BEFORE DELETE ON test FOR EACH STATEMENT BEGIN IF(ROW_COUNT()>=2)

MySQL DELETE FROM with UNION subquery by IN condition

£可爱£侵袭症+ 提交于 2019-12-02 04:56:09
问题 I have tripped up on a curious SQL error. The last query doesn't work. Of course I can just split that DELETE into three queries, but I really wonder why MySQL doesn't let me do it this way. A little example: (SELECT id FROM stairs WHERE building = 123) UNION (SELECT id FROM lift WHERE building = 123) UNION (SELECT id FROM qrcodeid WHERE building = 123) works! DELETE FROM startpoint WHERE id IN (SELECT id FROM stairs WHERE building = 123) works, too! Whereas DELETE FROM startpoint WHERE id IN

Deleting Records

僤鯓⒐⒋嵵緔 提交于 2019-12-02 04:22:07
I have a table [user_logs] with the following fields [username], [datetimelog] Sample Data ============== user1 2011-06-28 08:49:01 user2 2011-06-28 08:59:38 user3 2011-06-28 09:04:31 user4 2011-06-28 10:00:15 user2 2011-06-28 10:28:54 user1 2011-06-29 08:31:22 user9 2011-06-29 08:32:32 user2 2011-06-29 10:13:53 user1 2011-06-29 13:11:15 I want to know how to create an SQL Delete query to delete all user logs EXCEPT their last log so that the above example will produce the following after a DELETE query user1 2011-06-29 13:11:15 user2 2011-06-29 10:13:53 user3 2011-06-28 09:04:31 user4 2011-06

Data deleted with IN clause even if the column doesn't exist

心已入冬 提交于 2019-12-02 04:14:13
So, here is the test query you could play with: select top 10 * into #tmp FROM A delete from #tmp WHERE xxx_id in (select xxx_id FROM B) Actually, all these 10 records are deleted. The question is why are these 10 records deleted? Note: xxx_id is one column in table A only, it doesn't exist in table B . But the delete statement "works" anyway. Here's a demonstration of this behaviour: http://sqlfiddle.com/#!6/963f9/1/1 Update I found the answer in MSDN: http://social.msdn.microsoft.com/Forums/en-US/418722dc-a7bf-44c5-a2f6-e8d1cd00dbdc/in-clause-ignores-error-in-subquery-possible-bug?forum

Delete all rows and keep latest x left

霸气de小男生 提交于 2019-12-02 03:38:29
问题 I have a table like entryid, roomid 1 1 2 55 3 1 4 12 5 1 6 44 7 1 8 3 9 1 Now I would like to delete ALL entries where roomid = 1 and keep the latest 3 from roomid = 1 left (best with just one command) So finally entryid: 1 & 3 came deleted and entryid 6, 7, 9 keeps staying (for sure all other roomid will still stay) EDIT: Thanks for help. Below I added my own solution, for everyone interested I started a new Question how to bring that into ONE command. You may help me there. 回答1: DELETE

Delete duplicated records from a table without pk or id or unique columns in mysql

↘锁芯ラ 提交于 2019-12-02 00:40:18
问题 I need to delete all the duplicated records from one of my tables the problem is that there isn't any id or unique or key column so I can't make something like this: delete from tbl using tbl,tbl t2 where tbl.locationID=t2.locationID and tbl.linkID=t2.linkID and tbl.ID>t2.ID because it needs an id column or unique or key column and I can't make an ALTER IGNORE TABLE 'mytable' ADD UNIQUE INDEX because there is information that will be always necessary duplicated but others don't and I can't

Delete duplicated records from a table without pk or id or unique columns in mysql

做~自己de王妃 提交于 2019-12-01 21:24:32
I need to delete all the duplicated records from one of my tables the problem is that there isn't any id or unique or key column so I can't make something like this: delete from tbl using tbl,tbl t2 where tbl.locationID=t2.locationID and tbl.linkID=t2.linkID and tbl.ID>t2.ID because it needs an id column or unique or key column and I can't make an ALTER IGNORE TABLE 'mytable' ADD UNIQUE INDEX because there is information that will be always necessary duplicated but others don't and I can't make this: DELETE FROM 'table' WHERE 'field' IN (SELECT 'field' FROM 'table' GROUP BY 'field'HAVING

PDO query is always returning 1 or true

老子叫甜甜 提交于 2019-12-01 20:59:48
I am trying to check if a row exists before I delete it. The row in my table doesn't exist but it always returns 1 : $orders = $this->db->prepare("SELECT * FROM orders WHERE id=? AND user=?"); $check = $orders->execute(array($message,$this->model->checkapi($data,$message))); echo $check; if($check){ $deleteorder = $this->db->prepare("DELETE FROM orders WHERE id=? AND user=?"); $deleteorder->execute(array($message,$this->model->checkapi($data,$message))); array_push($result, array('success' => true, 'deleted' => $message)); echo json_encode(array("result" => $result)); die(); }else{ $this-

MySQL delete statement optimization

南楼画角 提交于 2019-12-01 19:41:46
问题 i have some delete queries to run against some pretty huge table (~100 GB), and i want to optimize them as much as possible: delete from table1 where column1 < date_sub(now(), interval 100 hour); column1 is a datetime column, i assume making an index for this column will speed up the deletions. besides that, anything i can do here? will using the date_sub() function slow down the query? should i calculate that value before running the query? delete from table2 where column2 = x; column2 is