MySql cmd下的学习笔记 —— 有关select的操作(in, and, where, like等等)

为方便本节学习, 请先自行建立本表:

建一个商品表:
create table goods (
    -> goods_id mediumint(8) unsigned not null auto_increment,
    -> cat_id smallint(5) unsigned not null default '0',
    -> goods_sn varchar(60) not null default '',
    -> goods_name varchar(120) not null default '',
    -> goods_number smallint(5) unsigned not null default 0,
    -> market_price decimal(10,2) unsigned not null default 0.00,
    -> shop_price decimal(10,2) unsigned not null default 0.00,
    -> add_time int(10) unsigned not null default 0,
    -> is_best tinyint(1) unsigned not null default 0,
    -> is_new tinyint(1) unsigned not null default 0,
    -> is_hot tinyint(1) unsigned not null default 0,
    -> primary key(goods_id)
    -> )engine myisam charset utf8;

添加数据:
mysql> insert into goods
    -> values
    -> (1, 4, '000001', 'KD876', 1, 1665.60, 1388.00, 1240902890, 1, 1, 1);
    -> (2, 8, '000002', 'vivo充电器', 17, 25.60, 12.80, 1241422402, 0, 0, 0);
    -> (3, 8, '000003', '兰士顿D4 重低音四核双动圈耳机', 24, 159, 79, 1241422402, 0, 0, 0),
    -> (4, 8, '000004', '华为原装充电器', 17, 58, 25, 1241422402, 0, 0, 0),
    -> (5, 11, '000005', '威讯读卡器', 8, 62, 39, 1241422518, 1, 1, 0),
    -> (6, 11, '000006', '内存卡64G', 15, 150, 29.90, 1241422573, 0, 0, 0),
    -> (7, 8, '000007', '地对空 K1', 20, 69, 39, 1241422785, 0, 0, 0),
    -> (8, 3, '000008', '小米 8', 9, 2006.67, 1806, 1241425512, 1, 1, 1),
    -> (9, 3, '000009', 'vivo Z1', 23, 1598, 1598, 1241511871, 1, 1, 1),
    -> (10, 3, '000010', '荣耀 9i', 11, 1449.01, 1449.00, 1241965622, 0, 0, 1),
    -> (11, 3, '000011', 'oppo A5', 1, 1499, 1499, 1241966951, 0, 0, 0),
    -> (12, 3, '000012', '华为 nova3', 8, 2799, 2799, 1245297652, 0, 1, 0),
    -> (13, 3, '000013', 'vovi Z1i', 8, 1798, 1598, 1241967762, 0, 0, 1),
    -> (14, 4, '000014', '荣耀Paly', 1, 2110, 1899, 1241968492, 0, 0, 1),
    -> (15, 3, '000015', '美图 T9', 3, 3399, 3990, 1241968703, 0, 1, 1),
    -> (16, 2, '000016', '小米MAX', 3, 929, 929, 1241968949, 0, 0, 0),
    -> (17, 3, '000017', '魅族 16', 1, 2499, 2499, 124196394, 1, 0, 1),
    -> (18, 4, '000018', '诺基亚 X6', 1, 1420, 1278, 1241969533, 0, 0, 0),
    -> (19, 3, '000019', '华为 P20', 12, 3278, 3278, 1241970139, 1, 1, 1),
    -> (20, 3, '000020', 'iphone', 12, 6600, 6468, 1241970417, 1, 1, 1);

  

查看主键为3的商品(where)

查看cat_id != 3 的商品(!= 或 <>)

 

两种方法都可以

查出本店价(shop_price)低于50的商品(where)

取出第4个栏目和第11个栏目的商品(in或and)

 

 

查看价格在500到1000之间的商品

 

 

(但不能用下面的写法)

 查看cat_id != 3 并且cat_id != 11(not in 或 !=)

 

 

计算本店价比市场价便宜多少

查出本店价比市场价低200以上的商品

但是不能直接用as 的discount列

查找开头为小米的商品(like, '%')

查找 小米*** 此类型的商品(_)

 

可以看出%和_的区别

%可以代替任意多个字符(包括零个)

_则是代替某一个字符

当我只想取出 '张_三' 这个字符串时

在‘_’前后直接加%是行不通的

这里的_不再表示任意一个字符,而是只表示为一个下划线

原文地址:https://www.cnblogs.com/abc23/p/9396391.html