zoukankan      html  css  js  c++  java
  • 测试那些事儿—SQL server IN和NOT IN 子查询

    IN 后面的子查询可以返回多条记录

    语法:select * from 表名 where 查询表达式 IN(子查询)

    --查询购买了“宝马小帅哥20寸”的用户信息

    【1】根据商品名称查询商品编号

    select commodityid from commodityinfo where commodityname ='宝马小帅哥20寸'

    【2】根据商品编号查询用户编号

    select userid from orderinfo 

    where commodityid=

    (select commodityid from commodityinfo where commodityname ='宝马小帅20寸‘)

    【3】根据用户编号查询用户信息

    select * from userinfo

    where userid in

    (

    select userid from orderinfo 

    where commodityid=

    (select commodityid from commodityinfo where commodityname ='宝马小帅20寸‘))

    --当子查询返回的列的值为多个时,就不能使用比较运算符‘=’了,要使用关键字in

     --not in 子查询

    --查询从未网购过商品的用户信息

    【1】查询订单表中购买过商品的用户编号

    使用关键字distinct去掉重复数据

    select distinct userid from orderinfo  --只要在订单表中存在的userid就是购买过商品的用户

    【2】将用户表中购买过商品的用户去除,剩下的就是没有购买过的

    select * from userinfo 

    where userid not in 

    (select distinct userid from orderinfo)

  • 相关阅读:
    Linux netstat 命令入门知识
    Linux文件预读对系统的影响
    Ubuntu下配置Hadoop环境
    Debian Wheezy 安装Nginx+HHVM
    Linux Glibc库严重安全漏洞检测与修复方案
    关于系统时间的几个问题
    C语言的整型溢出问题
    Django Nginx配置
    linux上安装mysql5.7
    日期、时间选择器
  • 原文地址:https://www.cnblogs.com/mgg520813/p/10934279.html
Copyright © 2011-2022 走看看