码迷,mamicode.com
首页 > 数据库 > 详细

mysql 查询小demo

时间:2017-03-28 22:52:05      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:english   var   nes   arc   demo   pre   结构   nio   mysql   

 

 

两张表的的结构如下,需求是写出从one表到two表和从two表到one表的查询转换。

create table student_one(
	name varchar(50) default ‘‘ not null,
	type varchar(10) not null default 0,
	score float(5,2) not null default 0.00
)Engine=innodb;


create table student_two(
	name varchar(50) default ‘‘ not null,
	math  float(5,2) not null default 0.00,
	english float(5,2) not null default 0.00,
	chinese float(5,2) not null default 0.00
)Engine=innodb;

  目前表中数据如下所示

技术分享

技术分享

 

以表one为基准查出表two的格式的语句如下:

select a.name,a.score as ‘math‘,b.score as ‘chinese‘,c.score as ‘english‘
from student_one a join student_one b join student_one c 
on a.name=b.name and a.name=c.name and b.name=c.name 
where a.type=‘math‘ and b.type=‘chinese‘ and c.type=‘Engine‘;

查询结果如下图所示:

  技术分享

 

 

以表two为基准查出表one的格式的语句如下:

select a.name,a.math as score,‘math‘ as type from student_two a 
union
select b.name,b.english as score,‘english‘ as type from student_two b 
union
select c.name,c.chinese as score,‘chinese‘ as type from student_two c;

  查询结果如下:

技术分享

 

mysql 查询小demo

标签:english   var   nes   arc   demo   pre   结构   nio   mysql   

原文地址:http://www.cnblogs.com/ddddemo/p/6637427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!