mysql 常用命令

來源:互聯網
上載者:User

標籤:排序   blog   mini   let   alter   arc   10個   名稱   表數   

★增系列★
----複製表(帶資料)----
create table 表名 select *from 老表名

----複製表(不帶資料)----
create table 表名 like 老表名

----插入已有表的資料到新表(格式一樣)----
insert into 新表 select *from 舊錶

----表中加入多個記錄----
insert into 表名 values (),(),(); 其中括弧裡為表的結構

★刪系列★
----刪除表-----
drop table if exists 表名

----刪除表中所有資料-----
delete from 表名

----刪除表中的某個資料-----
delete from 表名 where 條目1 = xxx

----刪除表中的某個重複資料的記錄,保留id最小的一個資料-----
delete from 表名 where id not in (select minid from (select min(id) as minid from person group by 條目) b);
★改系列★
----修改欄位名稱----
alter table 表名 change 舊欄位名 新欄位名 類型

★查系列★
----查詢一張表資料個數-----
select count(*) from 表名

----一張表某個id資料個數-----
select count(id) from 表名

----查詢表某個id最大資料對應的整條資料-----
select *from 表名 where id in (select max(id) from 表名)

----查詢某個資料的上一條資料,以id為標識-----
select *from 表名 as a where a.id > (select id from 表名 as b where a.id > b.id order by id desc limit 0,1)


★常用語句系列★
order by 排序:
select *from 表名 order by desc\asc

limit 分頁,限制:
放末尾,格式常見為 limit 取記錄起始位置(0為起始,如果省略則視作0),待取記錄個數(為-1則取到末尾) \\ limit 3,10 表示從第4個資料起往後取10個。

join 拼接:
left join 表名1 on 表名2 // 以表1為基準,將表2按照表1的格式拼過來,多餘的部分截掉,少的部分補NULL

exists 用於判斷條件是否存在,成立,如果成立返回true,常見定式為:
select 條目1 from 表名 where exists(select 條目2 from 表名 where t1 > t2)
更詳細的exists見:https://www.cnblogs.com/glory-jzx/archive/2012/07/19/2599215.html

group by 分組,能夠將某列相同的資訊合并成1行進行處理,經常與having連著使用,常見定式:
select *from 表名 group by 條目2 having count(條目) > 100

in 和 not in 表示某個欄位是否存在,常用定式:
where 條目 in/ not in (select *from where 條目1 > 條目2)

mysql 常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.