zoukankan      html  css  js  c++  java
  • MySQL数据库 —子查询,联合查询

    一 使用IN关键字的子查询

     1.查询游戏类型是'棋牌类' 的游戏的分数信息

        游戏分数表中并未包含游戏类型信息

       思路一:采用链接查询

       思路二: 分两步进行,首先找到所以'棋牌类'游戏的编号,再以这一组编号为查询依据完成查询

       select * from scores where gno in (select gno from games where gtype ='棋牌')

    2.查询没有参与5号游戏的玩家QQ

     select user_qq from users where user_qq not in (select user_qq from scores where gno=5)

    二 使用exists 关键字的子查询

    1.如果存在昵称为‘孙悟空’,则查询分数表中的数据

      select * from scores where exists (select * from users user_name ='孙悟空')

    三 联合查询

     select _statement union[all] select_statement [union[all] select_statement][...n]

    作用与特点:可以把多条查询语句所产生的结果集纵向连接为一体

                     有ALL关键字可以显示全部数据(即重复的也显示出来)

                     列的数量与类型都要兼容

     select user_name from users 

     union

     select gname from games

    1.查询玩家表中所有女性玩家和生日为空的玩家

    select * from users where user_sex='女'

    union

    select * from users where user_birthday is null

    <<=====>> select * from users where user_sex='女' or select * from users where user_birthday is null

    2.查询qq号是‘12302’的玩家所有分数并计算出总分数和平均分数,并显示到同一结果集中

    select user_qq,gno,score from scores where user_qq='12302' union all select '总分',' ',sum(score) from scores union all select '平均分',' ',avg(score) from scores where user_qq='12302'

  • 相关阅读:
    java io 学习笔记(一)
    Centos中查看系统信息的常用命令
    arcgis影像批量裁剪代码
    VS2010中VC++目录和C/C++之间的区别。VC++ Directories和C/C++的区别。
    VS中为什么不同的项目类型属性查看和设置的界面不一样
    C++函数中返回引用和返回值的区别
    java中HashMap的keySet()和values()
    repoquery详解——linux查看包依赖关系的神器
    linux学习笔记
    log4j的AppenderLayout格式符
  • 原文地址:https://www.cnblogs.com/jly144000/p/7375852.html
Copyright © 2011-2022 走看看