• MySQL入门第五天——数据表简单查询


    简单查询概述

    简单查询即不含whereselect语句。在此,我们讲解简单查询中最常用的两种查询:查询所有字段查询指定字段

    在此,先准备测试数据,代码如下:

    -- 创建数据库
    DROP DATABASE IF EXISTS mydb;
    CREATE DATABASE mydb;
    USE mydb;
    
    -- 创建student表
    CREATE TABLE student (
        sid CHAR(6),
        sname VARCHAR(50),
        age INT,
        gender VARCHAR(50) DEFAULT 'male'
    );
    
    -- 向student表插入数据
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1001', 'lili', 14, 'male');
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1002', 'wang', 15, 'female');
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1003', 'tywd', 16, 'male');
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1004', 'hfgs', 17, 'female');
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1005', 'qwer', 18, 'male');
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1006', 'zxsd', 19, 'female');
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1007', 'hjop', 16, 'male');
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1008', 'tyop', 15, 'female');
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1009', 'nhmk', 13, 'male');
    INSERT INTO student (sid,sname,age,gender) VALUES ('S_1010', 'xdfv', 17, 'female');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    查询所有字段(方法不唯一只是举例)

    查询所有字段 MySQL命令

    select * from student;
    
    • 1

    查询指定字段(sid、sname)

    查询指定字段(sid、sname) MySQL命令

    select sid,sname from student;
    
    • 1

    常数的查询

    在SELECT中除了书写列名,还可以书写常数。可以用于标记

    常数的查询日期标记 MySQL命令

    select sid,sname,'2021-03-02' from student;
    
    • 1

    从查询结果中过滤重复数据

    在使用DISTINCT 时需要注意:在SELECT查询语句中DISTINCT关键字只能用在第一个所查列名之前。

    select distinct gender from student;
    
    • 1

    算术运算符(举例加运算符)

    在SELECT查询语句中还可以使用加减乘除运算符

    查询学生10年后的年龄 MySQL命令

    select sname,age+10 from student;
    
    • 1
  • 相关阅读:
    【02】Camunda系列-扩展案例-用户任务、网关、决策自动化
    VB.NET—DataGridView控件教程详解
    uni.showLoading代替
    【软考 系统架构设计师】案例分析⑧ 数据库索引与视图
    VUE DPlayer编译
    C# 拨号面板 高亮显示
    图片隐写,盲水印,加密logo
    【前端】CSS(2) —— CSS的基本属性
    <类与对象>总结与扩展——《C++初阶》
    Docker常见命令
  • 原文地址:https://blog.csdn.net/weixin_52263256/article/details/126780726