· 8 years ago · Jun 30, 2018, 01:38 AM
1SELECT * FROM story_category WHERE category_id NOT IN (
2SELECT DISTINCT category.id FROM category INNER JOIN story_category ON category_id=category.id);
3
4DELETE FROM story_category WHERE category_id NOT IN (
5SELECT DISTINCT category.id FROM category INNER JOIN story_category ON category_id=category.id);
6
7UPDATE tbl AS a
8 INNER JOIN tbl AS b ON ....
9 SET a.col = b.col
10
11UPDATE tbl SET col = (
12 SELECT ... FROM (SELECT.... FROM) AS x);
13
14DELETE FROM story_category WHERE category_id NOT IN (SELECT DISTINCT
15category.id FROM category INNER JOIN
16story_category ON
17category_id=category.id);
18
19DELETE FROM story_category WHERE category_id NOT IN (SELECT DISTINCT
20category.id FROM category);
21
22UPDATE My_Table
23SET Priority=Priority + 1
24WHERE Priority >= 1
25AND (SELECT TRUE FROM (SELECT * FROM My_Table WHERE Priority=1 LIMIT 1) as t);
26
27drop table if exists apples;
28create table if not exists apples(variety char(10) primary key, price int);
29
30insert into apples values('fuji', 5), ('gala', 6);
31
32drop table if exists apples_new;
33create table if not exists apples_new like apples;
34insert into apples_new select * from apples;
35
36update apples_new
37 set price = (select price from apples where variety = 'gala')
38 where variety = 'fuji';
39rename table apples to apples_orig;
40rename table apples_new to apples;
41drop table apples_orig;
42
43select duplicate_rows.*
44from test as bad_rows
45 inner join (
46 select day, MIN(id) as min_id from test group by day having count(*) > 1
47 ) as good_rows on good_rows.day = duplicate_rows.day and good_rows.min_id <> duplicate_rows.id;