MySql語句,慢慢完善

2021-10-25 10:13:23 字數 4402 閱讀 1512

表名:student

建立表:(primary key:主鍵;auto_increment:自動遞增)

create table student

( stuid int primary key auto_increment,

stuname varchar(40) not null

);

插入資料:(因為寫了主鍵遞增,所有stuid=10的後面是stuid=11)

insert into student(stuname) value('zhangsan');

insert into student value(2,'lisi');

insert into student value(10,'wangwu');

insert into student(stuname) value('maliu');

新增列:age (int型別)

alter table student  add age int;
更新age列的內容(即根據stuid=xx時,向age裡面新增多大的數)

update student set age=16 where stuid=1;

update student set age=16 where stuid=2;

update student set age=17 where stuid=10;

update student set age=16 where stuid=11;

新增刪除唯一約束:

alter table student add constraint un_name unique(stuname);

alter table student drop index un_name;

增加列:chinese (語文)

alter table student add chinese int;
更新chinese列的內容(即根據stuid=xx時,向chinese裡面新增多大的數)

update student set chinese=122 where stuid=1;

update student set chinese=113 where stuid=2;

update student set chinese=108 where stuid=10;

update student set chinese=98 where stuid=11;

增加列:mathematics (數學)

alter table student add mathematics int;
更新mathematics 列的內容(即根據stuid=xx時,向mathematics 裡面新增多大的數)

update student set mathematics=78 where stuid=1;

update student set mathematics=99 where stuid=2;

update student set mathematics=137 where stuid=10;

update student set mathematics=126 where stuid=11;

增加列:english (英語)

alter table student add english int;
更新english列的內容(即根據stuid=xx時,向english裡面新增多大的數)

update student set english=68 where stuid=1;

update student set english=89 where stuid=2;

update student set english=92 where stuid=10;

update student set english=101 where stuid=11;

增加列:sum(和)

alter table student add sum int;
刪除列:sum(和)

alter table student drop sum;
增加列:stusum(和)

alter table student add stusum int;
求每個人的總分和

select sum(chinese+mathematics+english) from student where stuid=1;

update student set stusum=(chinese+mathematics+english) where stuid=1;

update student set stusum=(chinese+mathematics+english) where stuid=2;

update student set stusum=(chinese+mathematics+english) where stuid=10;

update student set stusum=(chinese+mathematics+english) where stuid=11;

排序:公升序為asc 降序為desc:

select * from student order by stusum desc;
日期獲取

select now();
更新日期

update student set stutime=now() where stuid=1;

update student set stutime=now() where stuid=2;

update student set stutime=now() where stuid=10;

update student set stutime=now() where stuid=11;

字串函式:ascii(返回stuname欄位的第乙個字母的ascii碼)

select ascii(stuname) as numcodeoffirstchar from student;
函式:char_length(s):返回字串 wbn的字元數

select char_length("wbn") as lengthofstring;
函式:character_length(s):返回字串 wbn的字元數

select character_length("wbn") as lengthofstring;
函式:concat(s1,s2…sn):合併多個字串

select concat("sql ", "runoob ", "gooogle ", "facebook") as concatenatedstring;
函式:concat_ws(x, s1,s2…sn):合併多個字串,並新增分隔符:

select concat_ws("-", "sql", "tutorial", "is", "fun!")as concatenatedstring;
函式:field(s,s1,s2…):返回第乙個字串 s 在字串列表(s1,s2…)中的位置:

select field("c", "a", "b", "c", "d", "e");//c在abcde中的位置,3
函式:find_in_set(s1,s2):返回在字串s2中與s1匹配的字串的位置:

select find_in_set("c", "a,b,c,d,e");

String類 常用 操作 方法 合集 慢慢完善

string用的挺多,但是針對它的操作方法時間長了不用還真想不起來,總結一下子的話,這裡先總結下常用的 返回值型別 基本操作方法 機能boolean equals string str 和其他字串相等時返回true char charat int pos 獲取字串中的第i個字元 intindexof...

語句完善 錢中平 2020 01 08

語句的完善 if邏輯運算中的and和or and 條件1 and 條件2 兩個條件同時滿足,就返回true 兩個條件都不滿足,返回false or 條件1 or 條件2 兩個條件只要有乙個滿足,就返回true 兩個條件都不滿足,返回false 以年齡為例 age int input 請輸入您的年齡 ...

mysql知識積累 慢慢登記防止忘記

mysql 的整體架構分為如下幾個部分 1 mysql 向外提供的互動介面 connectors 2 管理服務元件和工具元件 management service utilities 3 連線池元件 connection pool 4 sql 介面元件 sql inte ce 5 查詢分析器元件 p...