1. 程式人生 > >MySQL常見的查詢語句的運用

MySQL常見的查詢語句的運用

insert into 表名 values (資料1,資料2,資料3.。。。。。。),(資料1,資料2,資料3.。。。。。。)
INSERT into student (stuid,name,sex,phone,birthday,cid) VALUES (1,"王一",'男',13888888888,'20010301',1)
修改
update 表名 set 欄位= 新值 where 條件
UPDATE student SET cid=2 WHERE `name`="王一"
刪除
delete from 表名//表裡所有的資料刪除
delete from 表名 where 條件
DELETE from student WHERE NAME='王八'
查詢
select 需要的結果 from 表名 
SELECT name FROM student
select 需要的結果 from 表名 where 條件
SELECT cid from student WHERE name='王一'
SELECT name,score from student
SELECT name,score+10 from student
SELECT name as "姓名",score+10 '成績'from student
SELECT score  from student WHERE sex<>'女'
SELECT name from student WHERE score>60
SELECT * from student WHERE score BETWEEN 60 AND 80
SELECT * from student WHERE score in (10,60,80)
SELECT * from student WHERE name LIKE "王%"//% 匹配一個及多個字元
SELECT * from student WHERE name LIKE "王_"//  _  匹配一個字元
SELECT * from student WHERE score>70 and cid=1     //and 與    or  或    NOT 非
SELECT avg(score) FROM student WHERE cid=1
SELECT sum(score) FROM student WHERE cid=1
SELECT max(score) FROM student WHERE cid=1
SELECT min(score) FROM student WHERE cid=1
SELECT COUNT(name) from student WHERE score>60
SELECT cid,avg(score) from student GROUP BY cid 
SELECT sex,cid,avg(score) from student GROUP BY cid,sex
SELECT stuid ,score+5 as sa from student HAVING sa>60
SELECT * from student ORDER BY score ASC//從小到大
SELECT * from student ORDER BY score DESC//從大到小
SELECT cid,avg(score) from student  GROUP BY cid HAVING avg(score)>60 ORDER BY avg(score) DESC
SELECT * from student ORDER BY score DESC LIMIT 1,3
SELECT * from student,classinfo WHERE student.cid=classinfo.cid
SELECT name,cname FROM student,classinfo WHERE student.cid=classinfo.cid
SELECT student.name '姓名',classinfo.cname '班級名' FROM student,classinfo WHERE student.cid=classinfo.cid
SELECT student.stuid,student.name,student.cid,classinfo.cname from student,classinfo WHERE student.cid=classinfo.cid AND student.name='王一'