• 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
  • 相关阅读:
    Apache安装教程
    接口自动化测试总结
    【信号和槽】
    进行 Spring 6 迁移的最佳方式
    MySQL数据库基本操作+用户管理+用户授权
    JavaScript 单线程之异步编程
    坦克大战游戏开发中的设计模式总结
    非线性海洋捕食者算法(Matlab代码实现)
    Linux中system函数
    最长公共前缀
  • 原文地址:https://blog.csdn.net/weixin_52263256/article/details/126780726