sql语句(二)

编程入门 行业动态 更新时间:2024-10-25 04:14:43

sql<a href=https://www.elefans.com/category/jswz/34/1770772.html style=语句(二)"/>

sql语句(二)

1、where

语法:select 列名 from 表名 where 列满足的条件;

例:select * from products where prod_price='2.50';

注:数值型不用加引号

在根据where进行查询时,字段的值要放到单引号当中,不论字段值是什么数值类型。

2、语法:先过滤,再排序——where>order by

例:select * from products  where prod_price !='2.50' order by prod_price;

3、where语句支持的操作符

        a. 为空 is null

例:select * from products  where prod_price is null ;#为空

        b. 不等于 <>

例:从products查询vend_id和prod_name要求vend_id不等于1003

select vend_id,prod_name from products where vend_id<>'1003';

        c. between and  区间查询 包含区间值

例:从products表中查询prod_name,prod_price,要求prod_price价格在5-10之间

select prod_name,prod_price from products where prod_price between 5 and 10;

        d. and操作符 同时满足

例:select vend_id,prod_name,prod_price from products where vend_id ='1003' and prod_price<'10';

        e. or  满足其一即可  and>or 加括号优先级最高

        f. in——or

例:select vend_id,prod_name,prod_price from products  where vend_id in ('1003','1002');

        g. not in 操作符 不属于

        h. like——模糊匹配——匹配一部分

                % 匹配个任意字符,不限制个数

例:找出所有以Jet开头的产品

select * from products where  prod_name like 'Jet%';

                _ 匹配单个字符,只代表一位

例:查询产品名称第三个字符到最后内容是ton anvil

select * from products where  prod_name like '__ton anvil';

4、concat 拼接 

例:实现将vend_id,vend_country连接在一起,作为一条数据显示

select concat(vend_name,'(',vend_country,')') from vendors;

5、用as加别名  as可以省略,用空格隔开

例:select concat(vend_name,'(',vend_country,')')  as vend_title from vendors;

6、聚合函数

        a. upper()将小写字母转化为大写字母

例:select vend_name,upper(vend_name) from vendors;

        b. length() 字段值有多少位(空格算字数)

例:select vend_name,length(vend_name) from vendors;

        c. substring(str,x,y) str代表字符串,x代表从第几位开始截取,y表示截取几位数

        如:substring('abcdefgh',3,2) 结果为:cd

例:select cust_name ,substring(cust_name,5) from customers; #从第5位开始截取

        d. curdate() date(),year(),month(),day(),curtime(),adddate(date,30) 日期函数

例:计算订单30天后的收款日期

select order_date,adddate(order_date,30) from orders;

例:查找orders表中cust_id,order_num,order_date的值,要求年份是2005,并且月份是9月份

select cust_id,order_num,order_date from orders where year(order_date)='2005' and month(order_date)='9';

        e. avg() 求平均值

例:products中平均价格,并且给平均价格取别名

select avg(prod_price) avg_price from products;

        f. count() 求总数

例:查询customers一共多少条数据

select count(*) from customers;

        g. max() 最大值

例:最贵物品的价格

select max(prod_price) from products;

        h. min() 最小值

        i. sum() 总和

例:select sum(quantity) from  orderitems where order_num='20005';

7、函数组合使用

例:select count(*) pro_quantity,max(prod_price) max_price,min(prod_price) min_price,avg(prod_price) avg_price from products;

更多推荐

sql语句(二)

本文发布于:2024-02-27 06:18:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1705500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   sql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!