SQL學習基礎 => 建立表
阿新 • • 發佈:2019-01-11
--建立表
CREATE TABLE userinfo3 ( ID INT PRIMARY KEY NOT NULL, --ID 整數型別,設定為主鍵,並且不能為空 name VARCHAR ( 10 ) NOT NULL, --name 可變長度字元型別,不能為空 age INT NULL --age 整數型別,可以為空 )
--in子查詢 把子查詢select的結果當作主查詢的in 條件使用即可 SELECT * FROM userinfo3 WHERE age in (select age from userinfo2 WHERE isV = 1) SELECT* FROM userinfo3 WHERE age not in (select age from userinfo2 WHERE isV = 1) --not in 反向 --查詢某一條件的區間條件 select * from userinfo3 where age BETWEEN 22 and 25 -- 查詢a表和b表中age欄位中相同的值,存在則顯示 SELECT a.id,a.name,a.age,a.sex from userinfo3 as a where exists(select * from userinfo2 as b where a.age = b.age);-- 反,不存在顯示 SELECT a.id,a.name,a.age,a.sex from userinfo3 as a where not exists(select * from userinfo2 as b where a.age = b.age); --查詢返回結果排序 --age倒序, id升序 select * from userinfo3 ORDER BY age DESC,id --預設是=升序 select * from userinfo3 ORDER BY age,id ASC --函式 select count(name) from userinfo3 WHEREage >= 23 select sum(age) from userinfo3