學習資料庫筆記三,資料庫筆記
一
select查詢模型?
灰常重要!決定你能否寫出強大的查詢語句!
模型【列是變數(變數可以計算,所以列可以計算)
where後是運算式,值為真假,值為真時取出這行變數(列)的值】 (where id=3和id為3的進行比較,結果為真,就取id為3的行)
**所以SQL語句的執行過程分兩步1.判斷where後運算式的值,2.等於true時,取出列(變數)的值,否則不取出。
select * from user where 1; 這條語句按照模型來解釋就很好理解了,運算式什麼情況下都等於真,那就是整個表的行都等於真,取出*所有列,結果就是取出整張表的資料
select * from user where 0; 這條語句是不管什麼情況都為假,那就根本沒有合格行數,所以一行內容也沒有查出
現在來說說’變數(列)可以計算‘?
先來個簡單的,假如查詢明年的年齡
select uid,name,age+1 from user; 直接在age上加1
假如你的老闆讓你查詢本店鋪商品比市場價格便宜多少
select goods_id,goods_name,shop_price,market_price,market_price-shop_price from goods; 直接拿市場價格減去本店價格就可以了
只查某幾列的術語叫“投影運算”,意為把這兩列的值投影出來,你要取這兩列的值可以說,把這哪幾列做個投影。
把列看成變數,並拿兩列做運算,這叫做“廣意投影”。
二
SQL語句查詢練習
1.查詢出商品ID為32的商品
select goods_id,goods_name from goods where goods_id=32;
2.查詢出不屬於第3欄目的所有商品(欄目列名cat_id)
select cat_id,goods_id,goods_name from goods wher cat_id <> 3; 在SQL語句中<>和!=都是不等於的意思
3.查詢出本店價格大於3000的商品
select shop_price,goods_id,goods_name from goods where shop_price >3000;
4.查詢出本店價格低於或等於100元的商品
select shop_price,goods_id,goods_name from goods where shop_price <=100;
5.查詢出欄目為4或11的所有商品,不許用or
不許用or就用||,或in
用|| select cat_id,goods_id,goods_name from goods where cat_id=4 || cat_id=11;
它出這題目的意思是考你會不會用in,所以用下面這樣寫法好
用in select cat_id,goods_id,goods_name from goods where cat_id in (4,11);
6.查詢出價格大於100小於500的商品,不許用or
select shop_price,goods_id,goods_name from goods where shop_price between 100 and 500;
這種寫法也可以
select shop_price,goods_id,goods_name from goods where (shop_price >=100) and (shop_price<=500);
7.查詢出欄目id不等於3和欄目id不等於11的商品,用and和not in分別實現
select cat_id,goods_id,goods_name from goods where (cat_id<>3) and (cat_id<>11);
select cat_id,goods_id,goods_name from goods where cat_id not in (3,11);
8.查詢出商品價格大於100小於500 或 大於4000小於5000的商品
select shop_price,goods_id,goods_name from goods where (shop_price between 100 and 500) or (shop_price between 4000 and 5000);
9.取出第3欄目下,價格小於1000或大於3000,並且點擊量大於5的商品
這種情況把條件一一列出用and或or串連它們
select cat_id,shop_price,click_count,goods_name from goods where (cat_id=3) and (shop_price<1000 or shop_price>3000) and (click_count>5);
10.取出第一個欄目下的商品(第一個欄目通常是父結點,一般它下面是沒有直接的商品的,而是放子欄目,所以在沒有學子查詢的情況下,要先看欄目表中,哪些屬於欄目一,把這些欄目下的商品取出)
select cat_id,goods_id,goods_name from goods where cat_id in (2,3,4,5);
11.取出以‘諾基亞’開頭的商品
select goods_id,goods_name from goods where goods_name like '諾基亞%'; 記不清的部分用%代替,它代表任一字元。
12.取出名字為‘諾基亞Nxx’的商品
select goods_id,goods_name from goods where goods_name like '諾基亞N__'; _代表一個字元
13.取出名字不以‘諾基亞’開頭的商品
select goods_id,goods_name from goods where goods_name not like '諾基亞%';
14.取出第3個欄目下,價格大於1000小於3000,點擊量大於5,名字以‘諾基亞’開頭的商品*
select cat_id,shop_price,goods_name from goods where (cat_id=3) and (shop_price between 1000 and 3000) and (click_count >5) and (goods_name like '諾基亞%');
like模糊比對
%匹配任一字元
_匹配單一字元
三
兩道面試題
1.把num值處於[20,29]之間的數,改為20
把num值處於[30,39]之間的數,改為30
答update mian set num=floor(num/10)*10 where num between 20 and 39;
解題思路 首先where的範圍在20~39,這是把20、30兩個範圍的數一起算,然後對這20~39的數進行num=floor(num/10)*10,先除以10,再向下取整,現在20多的都是2,30多的都是3,再乘以10就得到想要的結果了。
2.把goods表中商品名為‘諾基亞xxx’的商品改為‘HTCxxx’
這道題比較複雜,用到了函數
可以分步驟完成
a) select goods_name from goods where goods_name like '諾基亞___'; 先把‘諾基亞xxx’的資料查出來
+------------+
| 諾基亞e66 |
| 諾基亞n96 |
| 諾基亞n85 |
+------------+
b) select substring(goods_name,4) from goods where goods_name like '諾基亞___'; 用substring函數把前面諾基亞三個字截取掉
+-------------------------+
| substring(goods_name,4) |
+-------------------------+
| e66 |
| n96 |
| n85 |
+-------------------------+
c) select concat('HTC',substring(goods_name,4)) from goods where goods_name like '諾基亞___'; 用concat函數在前面拼接HTC三個字元,即可替換成功。