SQL學習基礎 => 建立表

--建立表

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 WHERE age >= 23
select sum(age) from userinfo3
相關文章
相關標籤/搜索