zoukankan      html  css  js  c++  java
  • MySQL学习笔记:数据库查询、过滤、更新、删除、别名、去重、拼接、补全操作(navicat界面)

    # 查看当前所有的数据库
    show databases;
    # 打开数据库
    use myemployees;
    # 查看所有的表
    show tables;
    # 查看数据库结构
    desc employees;
    # 创建表
    create table stu(
    id int,
    name varchar(20))
    # 往表中插入数据
    insert into stu (id,name) values(1,'huang');
    insert into stu (id,name) values(2,'li');
    # 查看表中所有数据
    select * from stu;
    # 过滤表中数据
    select `name` from stu where id=2;
    # 更新表中数据
    update stu set `name`='wang' where id=1;
    # 查看更新结果
    select name from stu where id=1;
    # 删除表中特定行
    delete from stu where id=1;
    # 查看删除结果
    select id from stu; 
    # 查看所在数据库
    select database();
    # 查看MySQL版本
    select version();
    # 进阶1:基础查询
    /* 
    语法:
    select 查询列表(可以有多个表) from 表名
    特点:
    1、查询列表可以是字段、常量值、表达式、函数 
    2、查询的结果是一个虚拟的表格
    */
    # 打开数据库
    use myemployees;
    # 查询单个字段
    select last_name from employees;
    # 查询多个字段
    select last_name,salary,email from employees;
    # 查询所有字段
    select * from employees;
    
    # 查询常量值
    select 100;
    select 'john';
    # 查询表达式
    select 100*98;
    # 查询函数
    select version();
    
    # 为字段起别名:语法: 
    # 便于理解、区分重名
    # 方式1:select A as B from tableselect last_name as 姓,first_name asfrom employees;
    # 方式2:select A B from table;
    select last_name 姓,first_name 名 from employees;
    
    # 案例:查询salary, 别名为out put
    select salary `out put` from employees;
    # 8.去重,查询员工表中涉及到的部门编号
    use myemployees;
    select department_id from employees;
    select distinct department_id from employees; # 相当于matlab unique()
    
    # 9 +号作用,查询员工名和姓连接成一个字段,并显示为姓名,使用cancat函数
    select concat(last_name,',',first_name) as 姓名 from employees;
    
    # 测试
    # 成功,别名和查询同时进行
    select last_name,job_id,salary as sal from employees;
    # 成功
    select * from employees;
    # 注意:两个字段之间如果不是, 而是空格 则表示起别名
    select employee_id,last_name,salary*12 "annual salary" from employees;
    select employee_id,last_name,salary*12 as "annual salary" from employees;
    # 显示department的结构
    desc departments;
    # 显示表employees的全部job_id
    select distinct job_id from employees;
    # 显示表employees的全部列,列间用逗号隔开,列投显示成out_put
    show tables from myemployees;
    # 查看某个表的结构,如果要查看表的所有列,可以用desc得到表结构后,用concat连接
    desc employees;
    select concat(employee_id,',',first_name,',',last_name,',',email,',',phone_number,',',job_id,'...') as `output`
    from employees;
    # 如果拼接结果出现null, 原因是拼接的某一个字段为null
    select concat(employee_id,',',first_name,',',last_name,',',email,',',phone_number,',',job_id,',',commission_pct) as `output`
    from employees;
    # 解决部分字段为null的字段拼接,用ifnull函数对数据进行补全
    # 对比补全前后数据
    select ifnull(commission_pct,0) as 奖金率,commission_pct from employees;
    # ifnull()与concat()联合使用
    select concat(employee_id,',',first_name,',',last_name,',',email,',',phone_number,',',job_id,',',ifnull(commission_pct,0)) as `output`
    from employees;

    参考资料:https://www.bilibili.com/video/BV12b411K7Zu?p=28&spm_id_from=pageDriver

  • 相关阅读:
    sql developer 中文乱码解决办法
    ubuntu oracle数据库18c安装
    ubuntu Oracle SQL Developer 安装
    web.xml is missing and <failOnMissingWebXml> is set to true
    MySQL设置快速删除
    Annoying “Remote System Explorer Operation” causing freeze for couple of seconds
    安装程序时出现2502 2503错误解决方法
    Smoke Testing(冒烟测试)
    MySQL5.7.11免安装版的安装和配置:解决MYSQL 服务无法启动问题
    Error 2503 and 2502 when installing/uninstalling on Windows 10
  • 原文地址:https://www.cnblogs.com/feynmania/p/14771820.html
Copyright © 2011-2022 走看看