zoukankan      html  css  js  c++  java
  • mysql的SQL语句基本语法

    sql语句分为:
    USE test :表示在执行sql语句时候切换到test数据库

    1、插入
    语法: insert into 表名称(字段1,字段2,.......) values (字段1的值,字段2的值,.....)
    举例: 向userinfo表中插入数据
    注意一个问题 uid 是一个自增的主键,所以在插入数据的时候在字段中应该将这个字段的名称忽略
    insert into userinfo(uname,upwd,ustatus) values ('test1','123',0)

    2、查
     语法: select * from 表名称 -- 查询表中的所有字段
    select 字段名称,字段2的名称,..... from 表名称 --查询表中的指定字段
    select * from 表名称 limit 跳过多少条数据,拿到多少条数据 ->分页的sql语句写法
    select * from 表名称 where 字段的值 = '条件值' -> 带条件查找 (相等)
    select * from 表名称 where 字段的值 like '%条件值%' -> 带条件查找 (只要字段的值的任何位置包含有条件之即可查出)
    select * from 表名称 where 字段的值 like '条件值%' -> 带条件查找 (模糊查询,表示条件值是字段值的前缀)
    select * from 表名称 where 字段的值 like '%条件值' -> 带条件查找 (模糊查询,表示条件值是字段值的后缀)

    -- 满足大于,小于,大于等于,小于等于某个条件值的写法--
    select * from 表名称 where 字段的值 > 条件值
    select * from 表名称 where 字段的值 >= 条件值
    select * from 表名称 where 字段的值 < 条件值
    select * from 表名称 where 字段的值 <= 条件值

    -- 想要查找一个表中主键为1或者为2的值 (包含)
    select * from 表名称 where 主键值 in (1,2)
    select * from 表名称 where 主键值 =1 or 主键值 =2

    -- 想要找到一个表中同时有两个字段满足要求的数据
    select * from 表名称 where 字段值1='条件值' and 字段值2 = '条件值2' and .....

    -- 统计一个表中的数据总条数
    select count(*) from 表名称

    3、修改
    - 语法: update 表名称 set 字段1=更新的值,字段2=更新的值,... (更新所有数据的这些字段)
    update 表名称 set 字段1=更新的值,字段2=更新的值,... where 主键=1 (更新一定要带where)


    4、删除(数据)
    - 语法: delete from 表名称 (表示将表中的所有数据删除,这是很危险的)
    delete from 表名称 where 字段值=条件值 (推荐写法)

    sql写法举例:
    -- 1.0 查询 select * from 表名称 -- 查询表中的所有字段
    SELECT * FROM userinfo

    -- 2.0 select 字段名称,字段2的名称,..... from 表名称 --查询表中的指定字段
    SELECT uname FROM userinfo
    -- select * from 表名称 limit 跳过多少条数据,拿到多少条数据 ->分页的sql语句写法
    select * from userinfo limit 0,1

    -- select * from 表名称 where 字段的值 = '条件值' -> 带条件查找 (相等)
    select * from userinfo where uname='admin'

    -- select * from 表名称 where 字段的值 like '%条件值%'
    select * from userinfo where uname like '%es%'

    -- select * from 表名称 where 字段的值 like '条件值%'
    select * from userinfo where uname like 'ad%'

    -- select * from 表名称 where 字段的值 like '%条件值'
    select * from userinfo where uname like '%1'

    -- select * from 表名称 where 字段的值 > 条件值
    select * from userinfo where uid > 2

    -- 想要查找一个表中主键为1或者为2的值 (包含)
    select * from userinfo where uid in (1,2)
    select * from userinfo where uid =1 or uid = 2

    -- 想要找到一个表中同时有两个字段满足要求的数据
    select * from userinfo where uname ='test1' and upwd='123'

    -- 统计一个表中的数据总条数
    select count(*) from userinfo

    -- 统计userinfo表中的uname=123的数据总条数
    select count(*) from userinfo where upwd='123'

    -- 更新userinfo表中uname = 'test2'这个用户的upwd为456
    update userinfo set upwd = '456' where uname='test2'

    -- 删除userinfo表中的uid=3的这条数据
    delete from userinfo where uid=3


    5、连表查询
    - 内联
    select t1.* from 表名称1 as t1 ,表名称2 as t2 where t1.外键值 = t2.主键值
    - 左连接
    select * from 表名称1 as t1
    left join 表名称2 as t2
    on (t1.外键 = t2.主键)

    - 右连接
    select * from 表名称1 as t1
    right join 表名称2 as t2
    on (t1.外键 = t2.主键)

    6、删除一个表
    drop table 表名称 (危险做法,不要去做)

  • 相关阅读:
    vue.js 绑定数组, 数据源改变,view不更新问题
    安装Chrome插件网下载的.CRX格式插件安装时提示程序包无效:“CRX_HEADER_INVALID”的解决方法
    关于页面数据更新websocket 纪要
    SQLServer 统计24小时内数据,按小时展示。
    sqlserver 按日统计采集数据数量,并根据上下限值统计越界数量
    Qt项目发布
    SQL基础总结-03
    SQL基础总结-02
    php基础-14
    php基础-13
  • 原文地址:https://www.cnblogs.com/jolene/p/6105194.html
Copyright © 2011-2022 走看看