MySQL的基本操作

SQL 能做什么

① 从数据库中查询数据
② 向数据库中插入新的数据
③ 更新数据库中的数据
④ 从数据库删除数据
⑤ 可以创建新数据库
⑥ 可在数据库中创建新表
⑦ 可在数据库中创建存储过程、视图
⑧ etc…

MySQL常用的语句

SELECT 语句用于从表中查询数据。执行的结果被存储在一个结果表中(称为结果集)。
这里以user表进行示例

在这里插入图片描述

-- 通过 * 把 users 表中所有的数据查询出来
-- select * from users

-- 从 users 表中把 username 和 password 对应的数据查询出来
-- select username, password from users

-- 向 users 表中,插入新数据,username 的值为 tony stark  password 的值为 098123
-- 语法解读:INSERT INTO语句像指定的表中,插入如下几列数据,列得值通过values --指定

-- insert into users (username, password) values ('tony stark', '098123')
-- select * from users

-- 将 id 为 4 的用户密码,更新成 888888
-- 1.UPDATE指定要更新哪个表中的数据
-- 2.SET指定列对应的新值
-- 3.WHERE指定更新的条件
-- update users set password='888888' where id=4
-- select * from users

-- 更新 id 为 2 的用户,把用户密码更新为 admin123  同时,把用户的状态更新为 1
-- update users set password='admin123', status=1 where id=2
-- select * from users

-- 删除 users 表中, id 为 4 的用户
-- delete from users where id=4
-- select * from users

-- 演示 where 子句的使用
-- <>为不等于 between 在摸个范围内 like搜索某种模式 %%代表包含某个字符 
-- select * from users where status=1
-- select * from users where id>=2
-- select * from users where username<>'ls'
-- select * from users where username!='ls'
-- select * from users WHERE id BETWEEN 1 AND 3
-- SELECT * FROM users WHERE username LIKE "ceshi";
-- SELECT * FROM users WHERE username LIKE "%c%";


-- 使用 AND 来显示所有状态为0且id小于3的用户
-- select * from users where status=0 and id<3

-- 使用 or 来显示所有状态为1 或 username 为 zs 的用户
-- select * from users where status=1 or username='zs'

-- 对users表中的数据,按照 status 字段进行升序排序
-- order by 进行排序
-- select * from users order by status

-- 按照 id 对结果进行降序的排序  desc 表示降序排序   asc 表示升序排序(默认情况下,就是升序排序的)
-- select * from users order by id desc

-- 对 users 表中的数据,先按照 status 进行降序排序,再按照 username 字母的顺序,进行升序的排序
-- select * from users order by status desc, username asc

-- 使用 count(*) 来统计 users 表中,状态为 0 用户的总数量
-- select count(*) from users where status=0

-- 使用 AS 关键字给列起别名
-- select count(*) as total from users where status=0
-- select username as uname, password as upwd from users
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值