sql基礎
---------------------------------------------------------
【1】、說明:創建新表
【2】、根據已有的表創建新表:
A:create table tab_new like tab_old (使用舊錶創建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
DELETE FROM EMPLOYEES
WHERE BRANCH_OFFICE = 'Los Angeles';
CREATE
TABLE
tb
(
id
INT
IDENTITY(1,1)
PRIMARY
KEY
,
name
VARCHAR
(20)
)
select distinct *from user
查詢user表中的所有字段的值都一樣只顯示一條記錄
select distinct password from user
查詢user表中的password字段值相同的只顯示一條記錄
select distinct username,passwordfrom user;
查詢user表中username和password字段值都相同的只顯示一條記錄。
10、條件
10.1: is null 和is not null
null是一種狀態,不是裏面的字符串爲空。
通配符:
10.2,% :表示任意0個或多個字符。可匹配任意類型和長度的字符,有些情況下若是中文,請使用兩個百分號(%%)表示。
比如 SELECT * FROM [user] WHERE u_name LIKE '%三%'
10.7:排序
ORDER BY
asc desc 當對多個字段排序時,只作用前面的一個字段。
order by :排序字段可以查詢字段的別名,先查後排序,where條件中不能用查詢字段的別名。
SELECT *FROM emp ORDER BY deptno ASC,sal DESC;
對emp 表中兩個字段排序,先對deptno的值進行升序排列,在先對deptno字段的值排序好的基礎上再對sal字段的值進行降序排列11、模糊查詢:
關鍵字LIKE
例如查詢user表中userName字段值最後一個字符是‘東’的,語句如下:
select *from user from userName LIKE ‘%東’;
例如查詢user表中userName字段值中包含'東'的,語句如下:
select *from user from userName LIKE ‘%東%’
;注:"%"表示零個或者多個字符.我這裏只介紹了模糊查詢中的一種通配符的使用,具體的看上面的通配符使用。