当前位置:Gxlcms > 数据库问题 > mysql联表查询,使用phpStudy自带的

mysql联表查询,使用phpStudy自带的

时间:2021-07-01 10:21:17 帮助过:1人阅读

 

#显示数据库
show databases;

#判断是否存在数据库wpj1105,有的话先删除
drop database if exists student;

#创建数据库
create database student;

#删除数据库
drop database student;

#使用该数据库
use  class;

#显示数据库中的表
show tables;

#先判断表是否存在,存在先删除
drop table if exists student1;

技术分享

 

#创建表
create table student(
id int auto_increment primary key,
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
)default charset=utf8;

#删除表
drop table student;

#查看表的结构
describe student;  #可以简写为desc student;

技术分享

 

#插入数据
insert into student values(null,‘dd‘,‘男‘,‘1998-10-2‘,‘......‘),(null,‘bb‘,‘女‘,‘1889-03-6‘,‘......‘),(null,‘cc‘,‘女‘,‘1899-03-6‘,‘......‘),(null,‘cc‘,‘男‘,‘1894-03-6‘,‘......‘);//多条数据
insert into student values(null,‘aa‘,‘女‘,‘1889-03-6‘,‘......‘);//插入单条数据

#查询表中的数据

# and 且
select * from student where date>‘1989-1-2‘ and date<‘1998-12-1‘;

技术分享

# or 或

select * from student where date<‘1899-11-2‘ or date>‘1990-12-1‘;

 技术分享

#between

select * from student where date between ‘1890-1-2‘ and ‘1994-12-1‘;

技术分享

 

 #in 查询制定集合内的数据
select * from student where id in (1,3,5);

 

#排序 asc 升序  desc 降序

ORDER BY 语句

ORDER BY 语句用于根据指定的列对结果集进行排序。

ORDER BY 语句默认按照升序对记录进行排序。

如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。

 select * from student order by id desc;

技术分享

 

#分组查询 #聚合函数 
select max(id),name,sex from student group by sex;

select min(date) from student;

select avg(id) as ‘求平均‘ from student;

select count(*) from student;   #统计表中总数

select count(sex) from student;   #统计表中性别总数  若有一条数据中sex为空的话,就不予以统计~

select sum(id) from student;

#查询第i条以后到第j条的数据(不包括第i条)

技术分享

 

#修改表的名字
#格式:alter table tbl_name rename to new_name
alter table c rename to a;
 
#表结构修改
create table test
(
id int not null auto_increment primary key, #设定主键
name varchar(20) not null default ‘NoName‘, #设定默认值
department_id int not null,
position_id int not null,
unique (department_id,position_id) #设定唯一值
);

#向表中增加一个字段(列)
#格式:alter table student add type_id int;/alter table student add(type_id int);
alter table student add  type_id varchar(20);

#修改表中某个字段的名字
alter table tablename change columnname newcolumnname type;  #修改一个表的字段名
alter table student change name uname varchar(50);

select * from student;

技术分享

 

#表student 增加列test
alter table student add(test char(10));

技术分享

 


#表student 修改列test
alter table student modify test char(20) not null;
#表student 去掉列test
alter table student drop column test;

 

mysql联表查询,使用phpStudy自带的

标签:

人气教程排行