yangwenlong


根据需要和条件,查看并显示结果集,如果需要,可将结果集生成数据表
select:查什么,列筛选,可以用*代表全部列
from:在哪个表中查,
where:符合什么样的条件,行筛选
select:
        表示x不等于\'\'的方法:
        1.x!=\'\'
        2.x<>\'\'
        3.x Not IN(\'\')
        4.Not X=\'\'

        not
        and  (Between m and n)
        or
        查找某个字段未填写:x IS Null(题2)
        查找多个对象时,一种可以用or,另一种可以用 x IN(\'\',\'\',\'\')
1.查询出客户年龄在20-30之间的客户ID和姓名;
    A.between 20 and 30
    B. >=20 and <=30
2.查询客户没有填写年龄信息的.
3.查询客户姓名为:X,Y,Z的客户ID,和年龄
  X or Y or Z

1.as:以字段名别名的方式显示.
2.distinct:去重,用法: distinct 去重字段名
3.聚合函数(max(),avg(),sum(),count(),min()共五个),where后不能跟聚合函数.
4.top(自然数, 20 percent,order by NewID()--随机生成行标识符)
    select top 5 * from Mdf-table-name order by NewID()
5.into:生成一个表,或者表结构,存储于master库中,如:
    where 1<>1,只要结构:
    select top 6 * from table where 1<>1---生成一个全结构的空表
    select top 6 * from table where x like \'%s%\'---生成一个临时表
from:
1.重命名;
 查询出哪些商品被购买的商品名称. 商品表和明细表.
 select 商品名称
 from 表名1 as 表1,表名2 as 表2
 where 表1.cno=表2.cno

 也可以省略 as 直接跟空格 表1

2.结合子查询;
    两个表查询
    select 字段
    from 表1,表2
    where 表1.id=表2.id

    可以换成以下写法:
    select 字段
    from 表1 as t1,表2 as t2   ---可以不要as写成空格
    where t1.id=t2.id

    结合子查询:
    上述查询的是两个存在的表,如果有一个不是实表,只能使用下面的方法:
    select distinct 字段名
    from 表1 t1,
    (select distinct id from 表2)t2
    where t1.id=t2.id

where:
    1.数字:=,>,<,>=,<=,!=,<>,!>,!<
    2.字符:=,like,Contains
        匹配字符中间有空格的,必须使用双引号,如: \' "马  磊"\'
        %:表示任意个字符;
        _(下划线):表示任意1个字符.
        如:
            like \'15[89]%[^67]_ _ _\'
            Contains:针对大文本,事先要对该字段进行全文检索.而like是精准度比较高.
            使用方法:Contains(字段名,\'马%\')
        如果在字符串里有通配符,就要使用转义符,如:
        select 字段名列表 from 表名 where 字段名=\'%/%123\' escape \'/\'
        (查询全部结尾为%123的全部记录.)
    3.Not,And,Or
    4.Between...and
    select * from customer where cname between \'陈男\' and \'马国磊\'
    表示陈男到马国磊之间的数据,包含两者.系统按拼音排序后的结果.

    5.IN
order by 字段列表  asc|desc排序
    如:
    order by 字段1 asc,字段2 desc---第一个字段值相同时按第二个字段
    如:对客户表按照年龄排序(升序) ,如果年龄一样,女性在前,男性在后.(order by)

    查询出手机号码为135或者138开头,倒数第一二位为66的客户姓名和ID(like)
    查询出购买商品的客户号(distinct)
    
    查询出年龄最大的客户号和姓名(max)
        select cno,cname
        from customer
        A.order by cage desc    倒序排列,但如果有两个相同呢
        B.where cage=max(cage)  聚合函数不能写在where子句里
        使用嵌套方法解决:
        where cage=
            (
                select max(cage)
                from customer
            )
如果查询字符中包含通配符%_的,使用escape转义.

like \'%/%33\' escape \'/\'


查询男性客户的平均年龄;
    select avg(cage) as \'平均年龄\'
    from customer
    where csex=\'男\'
查询出多少位客户有购物行为;
    select count(distinct cno) //如果只查询客户号,不太符合题意
    from 明细表
    如果查询姓名,就要嵌套查询:
    select cname as \'姓名\'  //明细表中没姓名,有客户号
    from customer
    where cno in
    (
        select distinct cno
        from 明细表
    )
查询出某一类商品库存情况;
    需要用到商品库存表和商品类别表。
    select sum(库存量字段) as \'库存量\'
    from 库存表
    where id =
    (
        select  id
        from 类别表
        where 类别=\'商品名\'
    )


分类:

技术点:

相关文章:

  • 2021-08-04
  • 2021-08-31
  • 2021-05-08
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2022-01-12
猜你喜欢
  • 2022-12-23
  • 2022-02-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2021-10-29
相关资源
相似解决方案