
#b把列看成变量,把where后面看成php中if(exp)里的exp表达式
哪些行,被取出来?--哪一行能让exp为真,哪一行就能取出来
判断下一行取出什么
select goods_id,goods_name from goods where 1>2;
把列看成变量
既然是变量,变量之间就可以运算 。
取出商品id,商品名,本店价格比市场价省的钱。
select goods_id,goods_name,market_price-shop_price from goods where 1;
表里面原来没有“market_price-shop_price”的列
这一列其实是一个运算结果。属于较“广义投影”
你就把列看成变量,来运算即可。
列的运算结果。可以当成列看 。还可以起个列别名。
select goods_id,goods_name,(market_price-shop_price) as discount
from goods
where cat_id!=3;
帮我查出本店价比市场价省的钱,而且省钱200以上的商品。
select goods_id,goods_name,(market_price-shop_price) as discount
from goods
where (market_price-shop_price)>200;
where是对表中的数据发挥的作用,查询出数据来。
where发挥作用时。表上并没有discount列
发挥完作用,形成的结果才能discount
对于结果中的列。如果再筛选,须用having
查询模型(important)
列就是变量,在每一行上,列的值都在变化
where条件是表达式,在哪一行上表达式为真,
哪一行就取出来。
一道面试题
有如下表和数组
把num值处于[20,29]之间,改为20
num值处于[30,39]之间的,改为30
num
3
12
15
25
23
29
34
37
32
45
48
52
(3),(12),(15),(25),(23),(29),(34),(37),(32),(45),(48),(52);
把num当成变量看,因此num/10取整,再乘以10
update mian set num = floor(num/10)*10
where num >=20 and num<=39;
联系。把goods表中商品名为nokiaxxx的商品,改为htcxxx
substring concat()
select goods_id,good_name,concat('HTC',substring(goods_name,4)) from goods
where goods_name like '诺基亚%';
查出最贵的商品的价格
select max(shop_price) from goods;
查出最便宜商品的价格
select min(shop_price) from goods;
查出最早的商品。即good_id最小的值
select min(goods_id) from goods;
goods_number是指库存量
想统计,本店一共库存多少商品
因此,把所有字段的全部加起来
select sum(goods_number) from goods;
查看所有商品的品均价格
select avg(shop_price) from goods;
统计,一共有多少种商品
select count(*) from goods;
select count(*) from 表名,查询的就是绝对的行数
哪怕某一行所有字段权威null,也计算在内。
而select count(列名) from 表名
查询的是该列不为null的所有行的行数。
用count(*),count(1)谁好呢?
其实对于myisam引擎的表,没有区别
这种引擎内部有一计数器在维护者行数
Inodb的表,用count(*)直接读行数。效率很低,因为Innodb要去输一遍
计算。第三个栏目下所有商品的库存量之和sum(goods_num)
select sum(goods_number) from goods
where cat_id = 3;
计算第四个栏目下所有商品的库存量之和
select sum(goods_number) from goods
where cat_id = 4;
一次计算完,每个栏目下的库存量之和
group分组查询
select sum(goods_number) from goods
group by cat_id;
select cat_id,sum(goods_number) from goods
group by cat_id;
select goods_id,sum(goods_number) from goods;
按cat_id分组,计算每个栏目下的商品的平均价格
select cat_id,avg(shop_price) from goods
group by cat_id;
严格的讲。select的a,b列必须在group by a,b,c里选择
语义上才没有矛盾
哪些行,被取出来?--哪一行能让exp为真,哪一行就能取出来
判断下一行取出什么
select goods_id,goods_name from goods where 1>2;
把列看成变量
既然是变量,变量之间就可以运算 。
取出商品id,商品名,本店价格比市场价省的钱。
select goods_id,goods_name,market_price-shop_price from goods where 1;
表里面原来没有“market_price-shop_price”的列
这一列其实是一个运算结果。属于较“广义投影”
你就把列看成变量,来运算即可。
列的运算结果。可以当成列看 。还可以起个列别名。
select goods_id,goods_name,(market_price-shop_price) as discount
from goods
where cat_id!=3;
帮我查出本店价比市场价省的钱,而且省钱200以上的商品。
select goods_id,goods_name,(market_price-shop_price) as discount
from goods
where (market_price-shop_price)>200;
where是对表中的数据发挥的作用,查询出数据来。
where发挥作用时。表上并没有discount列
发挥完作用,形成的结果才能discount
对于结果中的列。如果再筛选,须用having
查询模型(important)
列就是变量,在每一行上,列的值都在变化
where条件是表达式,在哪一行上表达式为真,
哪一行就取出来。
一道面试题
有如下表和数组
把num值处于[20,29]之间,改为20
num值处于[30,39]之间的,改为30
num
3
12
15
25
23
29
34
37
32
45
48
52
(3),(12),(15),(25),(23),(29),(34),(37),(32),(45),(48),(52);
把num当成变量看,因此num/10取整,再乘以10
update mian set num = floor(num/10)*10
where num >=20 and num<=39;
联系。把goods表中商品名为nokiaxxx的商品,改为htcxxx
substring concat()
select goods_id,good_name,concat('HTC',substring(goods_name,4)) from goods
where goods_name like '诺基亚%';
查出最贵的商品的价格
select max(shop_price) from goods;
查出最便宜商品的价格
select min(shop_price) from goods;
查出最早的商品。即good_id最小的值
select min(goods_id) from goods;
goods_number是指库存量
想统计,本店一共库存多少商品
因此,把所有字段的全部加起来
select sum(goods_number) from goods;
查看所有商品的品均价格
select avg(shop_price) from goods;
统计,一共有多少种商品
select count(*) from goods;
select count(*) from 表名,查询的就是绝对的行数
哪怕某一行所有字段权威null,也计算在内。
而select count(列名) from 表名
查询的是该列不为null的所有行的行数。
用count(*),count(1)谁好呢?
其实对于myisam引擎的表,没有区别
这种引擎内部有一计数器在维护者行数
Inodb的表,用count(*)直接读行数。效率很低,因为Innodb要去输一遍
计算。第三个栏目下所有商品的库存量之和sum(goods_num)
select sum(goods_number) from goods
where cat_id = 3;
计算第四个栏目下所有商品的库存量之和
select sum(goods_number) from goods
where cat_id = 4;
一次计算完,每个栏目下的库存量之和
group分组查询
select sum(goods_number) from goods
group by cat_id;
select cat_id,sum(goods_number) from goods
group by cat_id;
select goods_id,sum(goods_number) from goods;
按cat_id分组,计算每个栏目下的商品的平均价格
select cat_id,avg(shop_price) from goods
group by cat_id;
严格的讲。select的a,b列必须在group by a,b,c里选择
语义上才没有矛盾
