mysql系列:mysql基礎+select5中子句+子查詢mysql
mysql查詢模型sql
mysql基礎語句+數據類型實例bash
# 先建表,有數據再說
create table test10(
id int,
sname varchar(20)
)engine myisam charset utf8;
create table test11(
cat_id int,
cname varchar(20)
)engine myisam charset utf8;
insert into test10
values
(1, '雲彩'),
(2,'月亮'),
(3,'星星');
insert into test11
values
(95,'猴子'),
(96,'老虎');複製代碼
如今,咱們有兩個集合A和B。函數
A = {0,1} B = {2,3,4}post
集合 A×B 和 B×A的結果集就能夠分別表示爲如下這種形式:ui
A×B = {(0,2),(1,2),(0,3),(1,3),(0,4),(1,4)};spa
B×A = {(2,0),(2,1),(3,0),(3,1),(4,0),(4,1)};3d
以上A×B和B×A的結果就能夠叫作兩個集合相乘的‘笛卡爾積’。code
select * from test10,test11;複製代碼
# 創建新的表
create table goods (
goods_id mediumint(8) unsigned primary key auto_increment,
goods_name varchar(120) not null default '',
cat_id smallint(5) unsigned not null default '0',
brand_id smallint(5) unsigned not null default '0',
goods_sn char(15) not null default '',
goods_number smallint(5) unsigned not null default '0',
shop_price decimal(10,2) unsigned not null default '0.00',
market_price decimal(10,2) unsigned not null default '0.00',
click_count int(10) unsigned not null default '0'
) engine=myisam default charset=utf8複製代碼複製代碼
插入數據cdn
insert into `goods` values (1,'kd876',4,8,'ecs000000',1,1388.00,1665.60,9),
(4,'諾基亞n85原裝充電器',8,1,'ecs000004',17,58.00,69.60,0),
(3,'諾基亞原裝5800耳機',8,1,'ecs000002',24,68.00,81.60,3),
(5,'索愛原裝m2卡讀卡器',11,7,'ecs000005',8,20.00,24.00,3),
(6,'勝創kingmax內存卡',11,0,'ecs000006',15,42.00,50.40,0),
(7,'諾基亞n85原裝立體聲耳機hs-82',8,1,'ecs000007',20,100.00,120.00,0),
(8,'飛利浦9@9v',3,4,'ecs000008',1,399.00,478.79,10),
(9,'諾基亞e66',3,1,'ecs000009',4,2298.00,2757.60,20),
(10,'索愛c702c',3,7,'ecs000010',7,1328.00,1593.60,11),
(11,'索愛c702c',3,7,'ecs000011',1,1300.00,0.00,0),
(12,'摩托羅拉a810',3,2,'ecs000012',8,983.00,1179.60,13),
(13,'諾基亞5320 xpressmusic',3,1,'ecs000013',8,1311.00,1573.20,13),
(14,'諾基亞5800xm',4,1,'ecs000014',1,2625.00,3150.00,6),
(15,'摩托羅拉a810',3,2,'ecs000015',3,788.00,945.60,8),
(16,'恆基偉業g101',2,11,'ecs000016',0,823.33,988.00,3),
(17,'夏新n7',3,5,'ecs000017',1,2300.00,2760.00,2),
(18,'夏新t5',4,5,'ecs000018',1,2878.00,3453.60,0),
(19,'三星sgh-f258',3,6,'ecs000019',12,858.00,1029.60,7),
(20,'三星bc01',3,6,'ecs000020',12,280.00,336.00,14),
(21,'金立 a30',3,10,'ecs000021',40,2000.00,2400.00,4),
(22,'多普達touch hd',3,3,'ecs000022',1,5999.00,7198.80,16),
(23,'諾基亞n96',5,1,'ecs000023',8,3700.00,4440.00,17),
(24,'p806',3,9,'ecs000024',100,2000.00,2400.00,35),
(25,'小靈通/固話50元充值卡',13,0,'ecs000025',2,48.00,57.59,0),
(26,'小靈通/固話20元充值卡',13,0,'ecs000026',2,19.00,22.80,0),
(27,'聯通100元充值卡',15,0,'ecs000027',2,95.00,100.00,0),
(28,'聯通50元充值卡',15,0,'ecs000028',0,45.00,50.00,0),
(29,'移動100元充值卡',14,0,'ecs000029',0,90.00,0.00,0),
(30,'移動20元充值卡',14,0,'ecs000030',9,18.00,21.00,1),
(31,'摩托羅拉e8 ',3,2,'ecs000031',1,1337.00,1604.39,5),
(32,'諾基亞n85',3,1,'ecs000032',4,3010.00,3612.00,9);複製代碼複製代碼
還須要一張表
# 須要再建一張表,結合上面的goods表練習
create table category(
cat_id int auto_increment primary key,
cat_name varchar(20) not null default ''
)engine myisam charset utf8;複製代碼複製代碼
# 插入數據
insert into category
(cat_name)
values
('手機類型'),
('CDMA手機'),
('GSM手機'),
('3G手機'),
('雙模手機'),
('手機配件'),
('充電器'),
('耳機'),
('電池'),
('讀卡器和內存'),
('充值卡'),
('小靈通/固話充值卡'),
('移動手機充值卡'),
('聯通手機充值卡');
複製代碼複製代碼
select goods_id,goods_name,goods.cat_id,cat_name,category.cat_id
from goods, category
where goods.cat_id = category.cat_id;複製代碼
上面那種方法太low了,只是爲了引出左鏈接
左鏈接語法: A表 left join B表 on 條件
實際上就是A表和B表作了笛卡兒積,而後條件就是來篩選這個笛卡兒積裏的數據,這樣是否是就很容易理解左鏈接了呢
select goods_id,goods_name,goods.cat_id
from goods left join category
on goods.cat_id = category.cat_id;複製代碼
是否是結果跟上面用笛卡兒積差很少呢,可是左鏈接效率更高
確定的是能夠的,案例以下:
取出第四個欄目下商品,以及商品的欄目名稱
select goods_id,goods_name,cat_name, goods.cat_id
from goods left join category
on goods.cat_id = category.cat_id
where goods.cat_id = 4;複製代碼
結論:把左鏈接的結果就當成一個表
先建表
create table boy(
bname varchar(20),
other char(1)
)engine myisam charset utf8;
insert into boy
values
('屌絲','A'),
('李四','B'),
('王五','C'),
('高富帥','D'),
('鄭七','E');
create table girl(
gname varchar(20),
other char(1)
)engine myisam charset utf8;
insert into girl
values
('空姐','B'),
('大s','C'),
('阿嬌','D'),
('張柏芝','D'),
('林黛玉','E'),
('寶釵','F');複製代碼
須要注意的若是這裏boy left join girl ,boy第一項屌絲是沒有匹配對象的,而且高富帥同時匹配了兩我的,一個是阿嬌,一個是張柏芝,因此咱們看下這兩個表左鏈接會出現什麼狀況
select boy.*,girl.* from boy left join girl on boy.other=girl.other;複製代碼
右鏈接語法: A表 right join B表 on 條件
右鏈接其實就是左鏈接,上面的語法至關於
B表 left join A表 on 條件
select boy.*,girl.* from boy right join girl on boy.other=girl.other;複製代碼
內鏈接語法: A表 innerjoin B表 on 條件
select boy.*,girl.* from boy inner join girl on boy.other = girl.other;複製代碼
內連接至關於左右鏈接的交集
咱們將上面建好的兩張表用來union
注意了,他們的列名稱不同,咱們union看看
select * from boy union select * from girl;複製代碼
union的結果集就看成一張表,能夠繼續用子句,好比用order by子句
select * from boy union select * from girl order by other;
複製代碼
abs(x) 返回絕對值函數
bin(x)返回x的二進制
floor(x)對x小數取整
rand()返回0到1內的隨機數
ceiling(x)x向上取整
函數返回一個字符串結果,該結果由分組中的值鏈接組合而成
group_concat()
length(x)返回x字節長度
char_length(x) 返回x的字符數
reverse(x) 反轉字符串
right(a,b) 把a字符串的後b個字符返回
position(a in b)返回a在b字符串中的位置
now() 返回當前日期。datetime格式
dayofweek(date) 返回date所表明的一星期中的第幾天(1-7)
dayofmonth(date) 返回date是一個月的第幾天(1-31)
dayofyear(date)返回date是一年的第幾天(1-366)同理
week(date)是第幾周
case 值 when 某種可能 then 返回值 記住最後一行要寫end
if(表達式,表達式爲true的值, 表達式爲false的值)
查看當前用戶