MySQL基本语句

1、查询的基本语句

#查询有多少个数据库
show databases;
#切换到某个数据库中
use dvwa;
#查询数据库里有多少个表
show tables;
#查询表里有多少列(查看表的结构)
desc users;
#查询列中存储的数据
select * from users;

#显示表中特定行(编号默认从0开始)(limit 从编号几行开始,向下几行)
select * from ceshi limit 2,3;
#显示表中内容的前三行
select * from ceshi limit 3;

#按照某个属性的升序排序(默认为升序)

select * from ceshi order by id;
select * from ceshi order by id asc;

#按照某个属性降序排序

select * from ceshi order by id desc;

#按照第2个属性升序排列

select * from ceshi order by 2

#匹配username中以a开头的元素

select * from ceshi where username like "a%";

#匹配一个字符_(下划线)

select * from ceshi where username like "a_";

#查看空值

select * from ceshi where username is null;

#查看不为空的命令

select * from ceshi where username is not null;

#联合查询

下图为test库中的两张表分别为ceshi和ceshi1

这是两个表分开查询的效果

联合查询

(注意union连接两边的查询数量需要相同否则会报错。

#substr(string初始字符串,start截取的初始位置,length截取长度)编号从1开始

select substr(username,1,2) from ceshi;

#将两列并在一起concat

select concat(username,password) from ceshi

2、创建一个数据库、创建表、增删改查表中的信息

#创建数据库

create database test;

#进入这个新的数据库
use test;

#在test数据库中创建一张表,其中not null表示id列的内容不能为空
create table ceshi(id int not null,username VARCHAR(10),password VARCHAR(10));

#查看新建表的结构
desc ceshi;

#向表中插入数据
insert into ceshi values('1','test','test');
insert into ceshi values('2','root','root');

#读取表中的所有内容
select * from ceshi;

#从表中读特定列的内容
select username,password from ceshi;

#将原来ceshi表中username是test的值改为admin
update ceshi set username='admin' where username='test';

#将id为1的一行删掉
delete from ceshi where id='1';

#只查询表中某一行的信息

select id,username,password from ceshi where id='1';

3、mysql语句的语法优先级

from>where>group by>having>select>order>limit(有括号先执行括号内的内容)

4、select的写入功能

A、into outfile函数使用的三个条件

1、有root权限

2、在数据库配置文件中的配置项含有secure_file_priv=''。(有写入和导出功能)

默认值为null,需要在my.ini中修改为空

将secure_file_priv赋值为空

再次查询(这样就支持写入和导出了)

3、知道绝对路径

B、into outfile函数的使用(目标,在服务器的c盘下写一个test.txt)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值