目錄
- 1. 相關知識點
- 2. 例題
- 2.1.可回收且低脂的產品
- 2.2.尋找用戶推薦人
- 2.3.大的國家
- 2.4. 文章瀏覽 I
- 2.5. 無效的推文
1. 相關知識點
- sql判斷,不包含null,判斷不出來
- distinct是通過查詢的結果來去除重復記錄
- ASC升序
- 計算字符長度
CHAR_LENGTH()
或LENGTH()
函數來計算列中字符串的字符數。- 這兩個函數的區別在于處理非 ASCII 字符時的行為:
- CHAR_LENGTH() 返回字符串的字符數,LENGTH() 返回字符串的字節數。
- 對于 ASCII 字符,這兩個函數的結果是相同的。
- where 與having的區別
- 在分組之前用where ,分組之后用having
排序 | 語法 |
---|---|
升序 | order by 字段 asc |
降序 | order by 字段 desc |
條件 | 例子 |
---|---|
和 | AND |
或 | or |
是 | is |
不是 | is not |
2. 例題
2.1.可回收且低脂的產品
-- 條件 既是低脂又是可回收 where low_fats="Y" AND recyclable="Y"
-- 查詢屬性 產品編號 select product_id
select product_id
from Products
where low_fats="Y" AND recyclable="Y";
2.2.尋找用戶推薦人
-- null 用數字判斷篩選不出來
select name
from Customer
where referee_id !=2 OR referee_id IS NULL;
2.3.大的國家
-- 查詢屬性:國家名稱、人口和面積
select name,population,area
fromWorld
where area>=3000000 OR population>=25000000;
2.4. 文章瀏覽 I
select distinct author_id as id
from Views
whereauthor_id=viewer_id order by id ASC;
2.5. 無效的推文
知識點:計算字符長度
-- 查詢所有無效推文的編號(ID)
select tweet_id
from Tweets
whereCHAR_LENGTH(content)>15;