SQL.select語句的設(shè)計
數(shù)據(jù)定義語言(DDL):?CREATE? DROP??? ALTER
?用于定義和管理數(shù)據(jù)對象(庫,表锐借,索引问麸,視圖),包括數(shù)據(jù)庫钞翔、數(shù)據(jù)表等严卖。例如:CREATE、DROP布轿、ALTER等語句哮笆。
?INSERT UPDATE DELETE?
數(shù)據(jù)操作語言(DML):?和表中的數(shù)據(jù)記錄
?用于操作數(shù)據(jù)庫對象中所包含的數(shù)據(jù)。例如:INSERT汰扭、UPDATE稠肘、DELETE語句。
SELECT? 60%
數(shù)據(jù)查詢語言(DQL)?:
?用于查詢數(shù)據(jù)庫對象中所包含的數(shù)據(jù)萝毛,能夠進行單表查詢项阴、連接查詢、嵌套查詢笆包,以及集合查詢等各種復(fù)雜程度不同的數(shù)據(jù)庫查詢环揽,并將數(shù)據(jù)返回到客戶機中顯示拷沸。例如:SELECT語句。
數(shù)據(jù)控制語言(DCL):
?是用來管理數(shù)據(jù)庫的語言薯演,包含管理權(quán)限及數(shù)據(jù)更改。例如:GRANT秧了、REVOKE跨扮、COMMIT、ROLLBACK等語句验毡。
?插入表數(shù)據(jù)insert
insert into?表名([字段列表]) values(值列表)衡创,(值列表2),(值列表3)
update?表名?set?字段=‘值’[晶通,字段2=值2?[璃氢,/。狮辽。一也。]][條件]?條件是確定要更改的記錄,可以通過條件指定一條也可指定多條
delete from?表名?[條件]
select
truncate?表名?截斷表數(shù)據(jù)
?
select
select[all/distinct]
{*|table.*|[table.]field1[as alias1][,[table.]field2[as alias2]][.....]}from
where
group by…
having
order by
limit count
1.?字段?要列出要查詢的字段
2.?可以為每個字段起個別名?as?或者用空格
3.?使用distinct過濾一條重復(fù)的數(shù)據(jù)
4.?在sql語句中可以使用表達式的列
5.?where可以在select update delete
<>不等于?
is not?例子?select desn from products where desn is null ;
<=>和=作用一樣可以比較空值
in是作用于? update products set price=100 where id in(1,2,3,4,5);
like?喉脖,模糊查詢?_(任意一個字符)%(0個或者多個任意字符)
select * from products where name like '%java%';
not like?和like相反
多表查詢
起別名查詢??select a.pid,a.name,b.name,b.price from cats a,products b;
??????????????????????????? select a.pid apid,a.name aname,b.name bname,b.price bprice from cats a,products b;
嵌套查詢? mysql> select *from products where cid in(select id from cats where name like 'j%');
排序?order by?字段?asc正
order by?字段?desc?倒
限制查詢?limit? mysql> select *from products where id<15 order by id asc limit 0,3;
group by?字段??select cid,count(*),sum(price),max(price),min(price),avg(price) from products group by cid
having?從句?select cid,count(*),sum(price),max(price),min(price),avg(price) from products group by cid having avg(price)>500;
count()
avg()
sum()
max()