mysql多表连接between_MySQL之多表查询练习 与基本查询基础

MySQL  增删查改

一、增:有2种方法

1.使用insert插入单行数据:

语法:insert [into] [列名] values

例:insert into Strdents (姓名,性别,出生日期) values ('XXX','男','1990/6/15')

注意:如果省略表名,将依次插入所有列

2.使用insert,select语句将现有表中的 数据添加到已有的新表中

语法:insert into select from

例:insert into  table_name('姓名','地址','电子邮件') select name,address,email  from  Strdents

注意:查询得到的数据个数、顺序、数据类型等,必须与插入的项保持一致

二、删:有二种方法

1.使用delete删除数据某些数据

语法:delete from [where ]

例:delete from table_a where name='邢金聪'(删除表a中列值为邢金聪的行)

注意:删除整行不是删除单个字段,所以在delete后面不能出现字段名

2.使用truncate table 删除整个表的数据

语法:truncate table

例:truncate table addressList

注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用于有外建约束引用的表

三、改

使用update更新修改数据

语法:update set [where ]

例:update table_a set col_1=value_1 where id=1;

注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用于有外建约束引用的表

四、查

1.普通查询

语法:select from [where ] [order by[asc或desc]]

1).查询所有数据行和列

例:select * from a

说明:查询a表中所有行和

2).查询部分行列--条件查询

例:select i,j,k  from  a   where f=5

说明:查询表a中f=5的所有行,并显示i,j,k3列

3).在查询中使用 as 更改列名

例:select name as 姓名 from a where  gender='男'

说明:查询a表中性别为男的所有行,显示name列,并将name列改名为(姓名)显示

4).查询空行

例:select name from a where e-mail is null

说明:查询表a中e-mail为空的所有行,并显示name列;SQL语句中用is null或者is not null来判断是否为空行

5).在查询中使用常量

例:select name , '廊坊' as 地址 from a

说明:查询表a,显示name列,并添加地址列,其列值都为'廊坊'

6).查询返回限制行数(关键字:top )

例1:select top 6 name from a

说明:查询表a,显示列name的前6行,top为关键字(oracle 中没有top关键字用rownum替代)

select   *   from  a where   rownum<6

7).查询排序(关键字:order by , asc , desc)

例:select name

from a

where grade>=60 分组查询

order by desc

说明:查询表中成绩大于等于60的所有行,并按降序显示name列;默认为 asc 升序

2.模糊查询

1).使用like进行模糊查询

注意:like运算符只用语字符串,多表

例:select * from a where name like '赵%'

说明:查询显示表a中,name字段第一个字为赵的记录

2).使用between在某个范围内进行查询

例:select * from a where age between 18 and 20

说明:查询显示表a中年龄在18到20之间的记录

3).使用in在列举数值内进行查询(in后是多个的数据)

例:select name from a where address in ('廊坊','石家庄','唐山')

说明:查询表a中address值为廊坊或者石家庄或者唐山的记录,显示name字段

3.分组查询

1).使用group by进行分组查询

例:select studentID as 学员编号, AVG(score) as 平均成绩  (注释:这里的score是列名)

from score

group by studentID

2).使用having子句进行分组筛选

例:select studentID as 学员编号, AVG

from score

group by studentID

having count(score)>1

说明:接上面例子,显示分组后count(score)>1的行,由于where只能在没有分组时使用,分组后只能使用having来限制条件,

4.多表联接查询

1).内联接

①在where子句中指定联接条件

例:select a.name,b.mark

from a,b

where a.name=b.name

说明:查询表a和表b中name字段相等的记录,并显示表a中的name字段和表b中的mark字段

MySQL 基础:

1. 如何查看表结构:

select *from sysobjects

用上面这个查询之后,看你想看那个表的表列名以及相关属性,找到该表的名字,看id是什么,然后,

select*from syscolumns where id= 就可以知道你想知道的一个表的表列的相关信息

2. 如何创建索引

create index ix_com_Employee_IDName on Employee (ID,Name);

3. 如何创建存储过程?

create --存储过程头部区域开始

or replace --可选表示如果数据库中已经存在一条相同名称的存储过程就把它替换掉

procedureproc_emp_create--存储过程名称 procedure_name

(

empnonumber, ename varchar2, job varchar2, mgr number, hiredate date, sal number)--parameter_declaration 声明参数(注意不需要写长度),存储过程头部区域结束

as

--声明区域,不需要声明变量可以不写

begin --PL/SQL标准执行语句--执行区域

insert into emp values(empno, ename, job, mgr, hiredate, sal, comm, deptno);end;

--执行存储过程

beginproc_emp_create(7778,'Alexander','analyst',6789,sysdate,4321,'',20);end;

[MySQL]多表关联查询技巧

理解与小结:

理解sql语句的连接,例如inner join, left join,right join,full join的区别;

能搭配使用查询嵌套,使用sum,avg,count等函数,注意使用此类函数必须搭配group by进行分类;

能使用having和where关键字,注意使用where的时候,要采用table.ll 的形式,否则会报错; having 则不用;

语法格式如下:

select  'tb1.a','tb2.b' ,avg(tb1.cc) from tb1

left join tb2 on tb1.id=tb2.id

lef join ......

where ......

group by id

having price>3

示例表A/B/C:

author_id

author_name

1

Kimmy

2

Abel

3

Bill

4

Berton

bo ok_id

author_id

start_date

end_date

9

1

2017-09-25 21:16:04

2017-09-25 21:16:06

10

3

11

2

2017-09-25 21:21:46

2017-09-25 21:21:47

12

1

13

8

order_id

book_id

price

order_date

1

9

0.2

2017-09-24 21:21:46

2

9

0.6

2017-09-25 21:16:04

3

11

0.1

2017-09-25 21:21:46

在以上表中执行ABC表关联

SELECT `authors`.*, `books`.book_id, `orders`.order_id, `orders`.price FROM`authors`LEFT JOIN `books` ON `authors`.author_id =`books`.author_idLEFT JOIN `orders` ON `books`.book_id = `orders`.book_id

#通过group by 分类,使用sum ,avg,count等函数求值

SELECT `authors`.*, sum(`orders`.price) FROM`authors`LEFT JOIN `books` ON `authors`.author_id =`books`.author_idLEFT JOIN `orders` ON `books`.book_id =`orders`.book_idGROUP BY `books`.book_id

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一些 MySQL 多表查询连接查询练习题,希望能帮到你: 1. 创建两个表:`students` 和 `classes`。`students` 表包含字段:`id`(主键,自增),`name`(字符串),`age`(整数),`class_id`(整数,外键,引用 `classes` 表的 `id` 字段)。`classes` 表包含字段:`id`(主键,自增),`name`(字符串),`teacher`(字符串)。 2. 向 `classes` 表中插入 3 条记录,分别是:`id` 为 1,`name` 为 "Class1",`teacher` 为 "Teacher1";`id` 为 2,`name` 为 "Class2",`teacher` 为 "Teacher2";`id` 为 3,`name` 为 "Class3",`teacher` 为 "Teacher3"。 3. 向 `students` 表中插入 4 条记录,分别是:`id` 为 1,`name` 为 "Tom",`age` 为 18,`class_id` 为 1;`id` 为 2,`name` 为 "Lucy",`age` 为 20,`class_id` 为 2;`id` 为 3,`name` 为 "Jack",`age` 为 19,`class_id` 为 3;`id` 为 4,`name` 为 "Mike",`age` 为 18,`class_id` 为 1。 4. 查询 `students` 表中的所有记录,并显示每个学生的 `name`,`age`,以及所在班级的 `name` 和 `teacher`。 5. 查询 `classes` 表中的所有记录,并显示每个班级的 `name`,`teacher`,以及该班级中的学生数目。 6. 查询 `students` 表中每个班级的平均年龄,并按照平均年龄从大到小排序。 7. 查询 `classes` 表中每个班级的平均成绩,并按照平均成绩从高到低排序。假设每个班级有若干个学生,每个学生有一个成绩,成绩存储在 `students` 表中。 8. 查询 `classes` 表中至少有两个学生的班级,显示这些班级的 `name` 和 `teacher`。 希望这些练习题能够帮助你熟悉 MySQL 多表查询连接查询的操作。如果你还有其他问题,可以随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值