入門MySQL語句(2)

查詢num行數:

select count(num) from info;   


查詢num去重複後的行數:

1.select instinct num from info;               (給num去重)

2.select count (instinct num) from info;   (查詢去重後num的行數)


查找數據最大值和最小值:

select max(字段名) as 字段名MAX,min(字段名) as 字段名MIN;


計算平均值:select avg(score) from info;

計算總值:select sum(score) from info;


group  by:按照...進行分組

select num,sname,gender group by gender;             (按照gender進行分組)


加一列:alter table info add column 字段名 數據類型(字節);

減一列:alter table info drop column 字段名;


創建自增列:

create table info(id int auto_increament primary key,sname char(8));


兩表連接:

首先要求兩個表相引用的字段數據類型和約束相同

alter table stu add constraint foreign key(sid) references info(num);


兩表拼接:

select * from info inner join stu on info.num=stu.sid;    兩表相對應的列


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章