mysql 4-9

####################
#第四章 检索数据#
####################

show columns from products;
#检索单个列 无序
select prod_name from products;
#检索多个列 
select prod_id,prod_name,prod_price from products;
#检索所有列 
select * from products;  
#检索不重复的行 
select vend_id from products;
select distinct vend_id from products;
#限制结果(返回前几行)
select prod_name from products;
select prod_name from products limit 5;
select prod_name from products limit 5,5;#从行5开始的5行

#使用完全限定的表名
select products.prod_name from products;
select products.prod_name from test.products;


#############################
#第五章 排序检索数据  
#############################


#排序数据 
select prod_name from products order by prod_name;#按照字母顺序排列  
#按多个列排序 (有先后顺序) 
select prod_id,prod_price,prod_name from products
order by prod_price,prod_name;#在price相同时才能按照name排序
#指定排序方向 
select prod_id,prod_price,prod_name from products
order by prod_price DESC;#降序排序 ,默认是升序
select prod_id,prod_price,prod_name from products
order by prod_price DESC,prod_name; #前面的降序 后面的升序 


######################
#第六七章 过滤数据 
######################

#使用where子句
select prod_name,prod_price from products where prod_price<10;
#操作符<,>,<>,=,!=,<=,>=,between
select prod_name,prod_price from products 
where prod_price between 5 and 10;

#空值检查 null
select prod_name from products where prod_price is null;#返回空prod_price的字段 
select cust_id from customers where cust_email is null;

########where ...and/or/in/not
select prod_id,prod_price,prod_name from products
where vend_id=1003 and prod_price <=10;

select prod_id,prod_price,prod_name from products
where vend_id=1003 or vend_id=1002;
#and or共用时候要加上小括号指定次序 
select prod_name,prod_price from products
where (vend_id=1002 or vend_id =1003) and prod_price >=10;
#in  
select prod_name,prod_price from products
where vend_id in (1002,1003)
order by prod_name;
#not
select prod_name,prod_price from products
where vend_id  not in (1002,1003)
order by prod_name;

######where +like+通配符 
#%通配符 表示任何字符出现任意次数(包括0次) 
select prod_id,prod_name from products 
where prod_name like 'jet%';#匹配任何以jet开头的,默认不区分大小写 

select prod_id,prod_name from products 
where prod_name like '%anvil%';#匹配包含anvil

select prod_id,prod_name from products 
where prod_name like 's%e';#匹配s开头e结尾

# _通配符 匹配单个字符(不是多个) 
select prod_id,prod_name from products 
where prod_name like '_ ton anvil'; 

######where +正则表达式regexp 
#基本字符匹配 (.代表匹配任意字符) 
select prod_id,prod_name from products 
where prod_name REGEXP '.000'
order by prod_name;

#########注意:like代表这个列必须全部匹配,但regexp代表部分匹配 

#进行or匹配 (|)
select prod_id,prod_name from products 
where prod_name regexp '1000|2000'
order by prod_name;

#匹配几个字符之一 
select prod_id,prod_name from products 
where prod_name regexp '[123] ton'
order by prod_name;#[]内代表或的意思 

#匹配范围 
select prod_id,prod_name from products 
where prod_name regexp '[1-5] ton'
order by prod_name;

#匹配特殊字符 (加上转义符\\)
##\\.    \\|    \\[]   \\\
select prod_id,prod_name from products 
where prod_name regexp '\\.'
order by prod_name;#匹配.alter

#匹配字符类  (简书)

#匹配多个实例 
select prod_name from products 
where prod_name regexp '\\([0-9] sticks?\\)'
order by prod_name;

select prod_name from products
where prod_name regexp '[[0-9]]{4}'
order by prod_name;#重复四次 

#定位符 ^开头 $结尾 
select prod_name from products 
where prod_name regexp '^[0-9\\.]'
order by prod_name;#如果没有^,则在任何地方匹配 而不仅仅是开头 




猜你喜欢

转载自blog.csdn.net/kylin_learn/article/details/80890503
今日推荐