1. 程式人生 > >MySQL語句綜合整理

MySQL語句綜合整理

進入bin,啟動資料庫 mysqld

mysql -u root –p            進入資料庫

*****                               密碼

show databases;           檢視資料庫

create databaseaaa;         建立資料庫

use aaa;                        進入資料庫

show tables;                        查看錶

create tableAAA(id int not null auto_increment ,欄位 資料型別 約束,....,primarykey(id))default charset=utf8

(支援中文);                     建立表

desc AAA;                               查詢屬性欄位

insert into AAA(欄位1,欄位2.....) values (值1,值2....);           插入資料

delete from AAAwhere id =1;                        刪除資料

alter table AAAadd (column) name varchar(255);     增加列       

update AAA setname='lisi' where id = 2;              修改列

alter table AAAdrop column name;                     刪除列屬性

alter table AAAadd constraint primary key id;             設定主鍵

alter table AAAchange id idd int;                       改列名

alter table AAArename to BBB;                    表名重新命名

alter table AAAmodify name text;                       修改列屬性

alter table AAAdrop index name;                      刪除索引

drop index nameon AAA;                                   刪除索引

alter table AAAadd index name約束名(name列名);                增加索引(約束)

select * fromAAA;                                      查詢全部

select * fromAAA where name='John';                查詢名字是約翰的所有資料

select * fromAAA where name like '%n%';              查詢名字含有n的人

select * fromAAA where name regexp 'n';               查詢名字含有n的人

regexp '^N' (N開頭) regexp 'B$' B結尾) 

*(前面欄位、資料0次或以上)   +(一次或多次)   |(或者)          

name regexp(Amy|John)          名字叫****

name in(Amy,John)                   名字叫****

selectgender,count(*) from AAA group by gender havinggander = 'male'只查男生性別分組計數

select * from AAAorder by (age,height,id) desc;      查詢所有資訊(年齡,身高,id依次降序

子查詢eg:AAA   BBB     id    gender='male'    擬的兩張表

select name fromAAA where id in (select id from BBB where gender='male');

=any()

=some()

select (selectid from AAA) from BBB;

連線查詢join   inner join       

and

select name fromAAA join BBB onAAA.id=BBB.id where socre=96;

select * fromAAA left join BBB onAAA.id=BBB.id;

select * fromAAA right join BBB onAAA.id=BBB.id;

AAA id 1  zhangsan

       2 lisi

BBB id 1  96

       1 87

       2 60

       3 89           

select * fromAAA,BBB where AAA.id=BBB.id and other-condition;

selectgender,count(1) from AAA group by gender having gender='male' union(distinct)  select gender,count(1) from BBB groupby gender having gender='female';      

select id fromAAA union all select id from BBB;     

and   or

select * fromAAA where grade between 60 and 90;

select * fromAAA where grade <>100;

                                    grade !=100;

show createtable AAA;

1、查詢表中多餘的重複記錄,重複記錄是根據單個欄位(peopleId)來判斷

sql語句

        SELECT   *  FROM     people  GROUP BY     peopleId   HAVING  count(peopleId) > 1

sql語句

2、刪除表中多餘的重複記錄,重複記錄是根據單個欄位(peopleId)來判斷,只留有rowid最小的記錄

sql語句

DELETE   FROM people  WHERE  peopleName IN (SELECT   peopleName   FROM    people   GROUP BY  peopleName   HAVING   count(peopleName) > 1)  AND peopleId NOT IN (SELECT   min(peopleId)   FROM people   GROUP BY    peopleName    HAVING  count(peopleName) > 1)

1、查詢表中多餘的重複記錄,重複記錄是根據單個欄位(peopleId)來判斷

sql語句

   SELECT  *   FROM  people  GROUP BY   peopleId  HAVING  count(peopleId) > 1

2、刪除表中多餘的重複記錄,重複記錄是根據單個欄位(peopleId)來判斷,只留有rowid最小的記錄

sql語句

DELETE   FROM   people  WHERE   peopleName IN (SELECT   peopleName FROM

  people GROUP BY  peopleName  HAVING  count(peopleName) > 1 )AND peopleId NOT IN ( SELECT  min(peopleId) FROM  people GROUP BY peopleName  HAVING  count(peopleName) > 1)

3、查詢表中多餘的重複記錄(多個欄位)

sql語句

SELECT  *  FROM  AAA a WHERE   (a.peopleId, a.seq) IN (SELECT  peopleId,seq

  FROM AAA  GROUP BY  peopleId, seq  HAVING  count(*) > 1 )

4、刪除表中多餘的重複記錄(多個欄位),只留有rowid最小的記錄

sql語句

DELETE  FROM  vitaea  WHERE (a.peopleId, a.seq) IN (SELECT  peopleId,  seq

FROM  vitae  GROUP BY  peopleId,  seq  HAVING   count(*) > 1) ANDrowid NOT IN (SELECT   min(rowid)  FROM  vitae  GROUP BY  peopleId, seq  HAVING  count(*) > 1)

5、查詢表中多餘的重複記錄(多個欄位),不包含rowid最小的記錄

sql語句

SELECT * FROM  vitae a WHERE (a.peopleId, a.seq) IN (SELECT peopleId, seq

FROM  vitae GROUP BY   peopleId,seq  HAVING count(*) > 1) AND rowid NOT IN (SELECT  min(rowid) FROM   vitae  GROUP BY peopleId,seq  HAVING  count(*) > 1)