標籤:排序 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 常用命令