• MySQL查询实例


    单表查询
    查询所有列

    1 SELECT * FROM product;

    查询指定列

    1 SELECT pro_name,price,pinpai FROM product;

    添加常量列

    1 SELECT pro_name AS '产品名称',price FROM product;

    创建学生表

     
     1 CREATE TABLE stu(
     2      sid  INT,
     3      sname  VARCHAR(10),
     4      sex  VARCHAR(2),
     5      servlet  DOUBLE,
     6      jsp   DOUBLE,
     7      html  DOUBLE 
     8 )
     9 SHOW TABLES;
    10 INSERT INTO stu VALUES(1,'佩奇','女',100,60,80);
    11 INSERT INTO stu VALUES(2,'乔治','男',25,58,100);
    12 INSERT INTO stu VALUES(3,'薛之谦','男',100,100,100);
    13 INSERT INTO stu VALUES(4,'李荣浩','男',90,90,90);
    14 INSERT INTO stu(sid,sname,servlet,html) VALUES(5,'于文文',90,90);
    15 INSERT INTO stu(sid,sname,servlet,html) VALUES(6,'',90,10);
    16 SELECT * FROM stu;
     

    查询时合并列

    1 SELECT sname,(servlet+jsp+html) AS '总成绩' FROM stu;

    查询时去除重复记录

    1 SELECT DISTINCT sex FROM stu;

    条件查询

    1 SELECT * FROM stu WHERE sex='男' AND sname='薛之谦';
    2 SELECT * FROM stu WHERE sex='女' OR sname='薛之谦';

    查询大于70分的学生

    1 SELECT * FROM stu WHERE servlet>60;

    查询jsp成绩不等于60分的学生

    1 SELECT * FROM stu WHERE jsp<>60;

    查询html成绩在60和100之间的学生

    1 SELECT * FROM stu WHERE html BETWEEN 60 AND 100;
    2 SELECT * FROM stu WHERE html >= 60 AND html<=100;

    查询sex为null的学生

    1 SELECT * FROM stu WHERE sex IS NULL;
    2 SELECT * FROM stu WHERE sex IS NOT NULL;

     查询sname是空字符串的学生

    1 SELECT * FROM stu WHERE sname='';
    2 SELECT * FROM stu WHERE sname<>'';

    查询sex不为空的学生

    1 SELECT * FROM stu WHERE sex IS NOT NULL AND sex<>'';

    查询所有产品中带有索尼的产品

    1 SELECT * FROM product WHERE pro_name LIKE '%索尼%';

     查询班级中所有两个字的同学

    1 SELECT * FROM stu WHERE sname LIKE '__';

     聚合函数:查询stu表中所有学生的servlet总成绩

    1 SELECT SUM(servlet) FROM stu;

    聚合函数:查询stu表中所有学生的servlet平均成绩

    1 SELECT AVG(servlet) FROM stu;

     聚合函数:查询stu表中jsp课程中的最高分

    1 SELECT MAX(JSP) FROM stu;

    最低分

    1 SELECT MIN(jsp) FROM stu;

    查询stu表中有多少人

    1 SELECT COUNT(*) FROM stu;(每列统计 取最大值)

     分页

    1 SELECT * FROM stu LIMIT 0,2;

    查询排序

    1 SELECT * FROM stu ORDER BY html ASC;(升序)
    2 SELECT * FROM stu ORDER BY html DESC;(倒序)

    查询stu表中所有男同学的html成绩排序

    1 SELECT * FROM stu WHERE sex='男' ORDER BY html;
    2 SELECT * FROM stu ORDER BY jsp ASC,html DESC;

    查询男女人数

    1 SELECT sex,COUNT(*) FROM stu GROUP BY sex; 

    查询总人数大于2的性别

    1 SELECT SEX,COUNT(*) FROM STU GROUP BY sex HAVING COUNT(*)>2;
  • 相关阅读:
    SpringMVC的入门示例
    [PTA] 数据结构与算法题目集 6-1 单链表逆转
    [PTA] L3-015 球队“食物链”
    [PTA] 1001. 害死人不偿命的(3n+1)猜想 (Basic)
    [PTA] 1002. 写出这个数 (Basic)
    [opengl] 画一个可移动的自行车 二维几何变换(平移、旋转、缩放)
    css inline-block 水平居中
    css 图片裁剪显示
    [leetcode] 19. Remove Nth Node From End of List (Medium)
    [leetcode] 20. Valid Parentheses (easy)
  • 原文地址:https://www.cnblogs.com/qq1312583369/p/10223175.html
Copyright © 2020-2023  润新知