SQL語句一覽
1. 查詢前5行
SQL Server
select top 5 name from user;
MySQL
select nanme from user limit 5;
返回第5行起得4行數據,limit 指定返回幾條數據,offset指定從第幾行開始,注意第一行是0行
select name from user limit 4 offset 5;
2. 排序 ORDER BY 默認升序ASC
在指定一條order by 子句時應該保證它是select語句中最後一條子句
select name,genser from user order by name,gender;
按位置列排序
select name,genser from user order by 1;
降序排列 DESC 只對其前面的一個列有效 多列
select name, gender from user order by name,gender desc;
3.過濾操作 WHERE
select name, gender from user where gender = '男';
ORDER BY 應該在 WHERE 之後
操作符:不等於(<>、!=) 不小於!< 不大於 !> ;指定兩個值直接 between x and >y; is null 爲null值
OR;AND 操作符
select id , name, gender from user where id = 'DLL01' and gender = '男';
select id , name, gender from user where id = 'DLL01' or id = 'DLL02';
注意:AND 操作符優先級更高
select t_id,id , name, age from test_user where t_id = 'DLL01'
or id = 'ALL01' and age >= 19;
以上語句將會被理解成:id=ALL01並且age>=19的或者t_id=‘DLL01’,如圖所示
select t_id,id , name, age from test_user where t_id = 'DLL01'
or (id = 'ALL01' and age >= 19);
IN 操作符
select name , id from user where id IN ('DLL01','DLL02','DLL03');
-- 同
select name , id from user where id = 'DLL01' or id = 'DLL02';
NOT 操作符
MariaDB 支持 NOT,不支持 IN,BETWEEN,EXISTS,大多數DBMS支持NOT
select name , id from user where not id = 'DLL01';
4.通配符過濾,模糊查詢
LIKE 語句
% 表示任何字符出現任意次數;
[A,B,C,D]裏面的任意一個字符;
[^A,B,C,D]裏面的任意一個字符;不包括裏面的字符
……
select name , age from user where name like '李%';
select name , hobby from user where hobbby like '%籃球%';
select name , email from user where email like '[i,m]%@163.com';
5. 字段拼接
SQL Server 使用 + 號,DB2、Oracle等使用 || ;MySQL必須使用特殊函數
SQL Server 或 Oracle
select name + '-' + age + '歲' from user;
select name || '-' || age || '歲' from user;
MySQL
select Concat(name, '-', age, '歲') from user;
會有多餘空格
去掉空格
RTRIM() 去掉字符串右邊空格,LTRIM();TRIM()
select name + '-' + RTRIM(age) + '歲' from user;
6. 執行算術運算
+、-、*、/
7.分組數據
GROUP BY
group by 子句必須出現在WHERE子句之後,order by 子句之前
select age,count(*) from user group by age;
注意:where 過濾行,having 過濾分組
1.having與where不同點
(1)where後面不能寫分組函數,而having後面可以使用分組函數。
(2)having只用於group by分組統計語句。
where 在數據分組前進行過濾,having在數據分組後進行過濾
查詢人數大於等於3的年齡
SELECT age, count(*) from user GROUP BY age HAVING COUNT(*) >= 3;
Select 子句順序
select from where group by having order by
9.子查詢
10.聯結表
連結的表越多,性能越差
等值聯結和內聯結
select s_name, t_name from student,teacher where student.class = teacher.class (and ...可接更多條件);
# 同下
#內聯結
select s_name,t_name from student inner join teacher on student.class = teacher.class;
支持字段別名和表別名,sutdent as s ; Oracle 不支持AS,直接使用 studnet s即可
外聯結
outer join
與內連結不同的是,外聯結還包括沒有關聯的行
必須使用 left 或 right,表示選擇 outer join 左邊或右邊的表的所有行
左外聯結
select s_name,t_name from student
left outer join teacher on student.class = teacher.class;
全外聯結:包含兩個表的不關聯行
select s_name,t_name from student
full outer join teacher on student.class = teacher.class;
11.組合查詢
UNION
必須兩條及以上select 子句組成;每個查詢必須包含相同列,列類型需兼容
使用union複合查詢時,只能使用一條 oredr by 子句且需位於最後一個 select 子句後
select name, age from user where age in(18,19)
union
select name, age from user where age = 20;
union ....
12.插入數據
insert into user values('張山','男','20');
# 推薦依次匹配字段
insert into user(name, gender, age) values('張山','男','20');
# 插入檢索出的數據
insert into user(name,gender,age)
select name,gender,age from student;
從一個表複製到另一個表
DB2 不支持
SELECT INTO
select * into student_copy from student;
MySQL,Oracle,PostgreSQL
create table student_copy as select * from student;
13.刪除、更新數據
爲了安全 請不要忘記使用 where 限定某些條件
#更新
update student set age = 16 where id = '1001';
#刪除
delete from student where id = '1001';
14.創建和操作表
create table student(
id char(10) not null,
name char(10) not null,
phone char(10) null,
city char(10) not null default '北京'
......
);
獲取系統日期:
MySQL current_date()
oracle sysdate
SQL Server getdate()
更新表 alter table
# 添加列
alter table student add gender char(10);
#刪除列
alter table student drop colum phone;
刪除表 drop table
drop table student;
重命名錶
不同數據庫不同
MySQL -------- rename 語句
SQL Server--------------- sp_rename
…