mysql 应用实例_MySQL应用实例转载

以下内容出处:http://blog.csdn.net/u013142781/article/details/50836476

整理的内容可以作为使用或者了解。

1、用一条SQL 语句 查询出每门课都大于80 分的学生姓名。(表结构如下图)

c0602b914219d671e156244ab1f2c3d6.png

答案可以有如下两种:select distinct student_name from table_test_one where student_name not in

(select distinct student_name from table_test_one where score<=80);select student_name from table_test_one group by student_name having min(score)>80;

第二种方法是group by 、min函数 结合 having的使用,w3school教程里面也提到过(在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用)

下面我们自己造数据,后面的例子也会用到。

建表然后倒入初始数据:DROP TABLE IF EXISTS `table_test_one`;

CREATE TABLE `table_test_one` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`student_no` varchar(10) NOT NULL,

`student_name` varchar(10) NOT NULL,

`subject_no` varchar(10) NOT NULL,

`subject_name` varchar(10) NOT NULL,

`score` int(11) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;INSERT INTO `table_test_one` VALUES

('1', '201601', '张三', '0001', '数学', '98'),

('2', '201601', '张三', '0002', '语文', '66'),

('3', '201602', '李四', '0001', '数学', '60'),

('4', '201602', '李四', '0003', '英语', '78'),

('5', '201603', '王五', '0001', '数学', '99'),

('6', '201603', '王五', '0002', '语文', '99'),

('7', '201603', '王五', '0003', '英语', '98');

可以运行一下上面两个语句试试结果是不是你想要的。

2、删除除了id不同, 其他都相同的学生冗余信息,表如下:

bc63bdb24612c8f5713dd6ef0f76b4f6.png

答案:delete table_test_one where id not in

(select min(id) from table_test_one group by

student_no, student_name, subject_no, subject_name, score);

先来造数据,题1中的数据只需要执行如下SQL就变成题2中的数据了:update table_test_one set subject_no = '0001', subject_name = '数学' where id = 6;

然后我们先执行这个看看:select min(id) from table_test_one group by

student_no, student_name, subject_no, subject_name, score

这个的执行结果如下:

a3230b0b6ed4f6199d9a13df42206fa6.png

PS:GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。刚刚就是GROUP BY 对多列的使用场景。

3、行转列:

表数据如下:

0e46a8bba30bb0f44ad0dd52c00b2c75.png

希望查询到结果如下:

23983c0bd526584b02fecc6595227cba.png

答案:select year,

(select amount from table_test_two t where t.month = 1 and t.year = table_test_two.year) as month1,

(select amount from table_test_two t where t.month = 2 and t.year = table_test_two.year) as month2,

(select amount from table_test_two t where t.month = 3 and t.year = table_test_two.year) as month3

from table_test_two group by year;

利用group by 实现行转列,这种场景在数据统计的时候经常用到。

猿友可以造数据自己运行试试:-- ----------------------------

-- Table structure for `table_test_two`

-- ----------------------------

DROP TABLE IF EXISTS `table_test_two`;

CREATE TABLE `table_test_two` (

`year` int(11) NOT NULL,

`month` int(11) NOT NULL,

`amount` decimal(10,1) NOT NULL,

PRIMARY KEY (`year`,`month`,`amount`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------

-- Records of table_test_two

-- ----------------------------

INSERT INTO `table_test_two` VALUES ('1991', '1', '1.1'),

('1991', '2', '1.2'),

('1991', '3', '1.3'),

('1992', '1', '2.1'),

('1992', '2', '2.2'),

('1992', '3', '2.3');

4、复制表( 只复制结构, 源表名:table_test_two 新表名:table_test_three)

答案:create table table_test_three as

select * from table_test_two where 1=2;

PS:如果需要将数据也复制过去,则上面改成where 1=1

5、复制表数据(将表 table_test_two 的数据复制到表table_test_three 里面)

答案:insert into table_test_three (year,month,amount)

select year,month,amount from table_test_two;

6、两张关联表,删除主表中已经在副表中没有的信息

答案:delete from table_test_student where not exists

(select * from table_test_class where table_test_student.class_id = table_test_class.calss_id);

我们先造点数据吧:-- ----------------------------

-- Table structure for `table_test_class`

-- ----------------------------

DROP TABLE IF EXISTS `table_test_class`;

CREATE TABLE `table_test_class` (

`calss_id` int(11) NOT NULL AUTO_INCREMENT,

`calss_name` varchar(10) CHARACTER SET utf8 NOT NULL,

PRIMARY KEY (`calss_id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

-- ----------------------------

-- Records of table_test_class

-- ----------------------------

INSERT INTO `table_test_class` VALUES ('1', '一班');-- ----------------------------

-- Table structure for `table_test_student`

-- ----------------------------

DROP TABLE IF EXISTS `table_test_student`;

CREATE TABLE `table_test_student` (

`student_id` int(11) NOT NULL AUTO_INCREMENT,

`student_name` varchar(10) CHARACTER SET utf8 NOT NULL,

`class_id` int(11) NOT NULL,

PRIMARY KEY (`student_id`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

-- ----------------------------

-- Records of table_test_student

-- ----------------------------

INSERT INTO `table_test_student` VALUES ('1', '罗国辉', '1');

INSERT INTO `table_test_student` VALUES ('2', '小宝鸽', '2');

执行后数据如下:

81e27bbd6ef669fa9005b2057d4909de.png

869579c9752475e4218dbaf813bbc071.png

显然副表student中小宝鸽这条数据的calss_id,主表没有对应的class_id.

执行对应SQL语句就会把小宝鸽这条数据删除掉了。

如本文对您有用,您的捐赠和留言将是我最好的支持~

如您愿意,请向更多志同道合的朋友们推荐本站,谢谢。

a909dc4d3501b4cf46a6e8a71f097f70.png

请尊重他人劳动成果。

转载请务必附上原文链接,我将感激不尽。

有什么问题可以 留言或QQ联系我

与《MySQL应用实例转载》相关的博文:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MySQL 视图的限制包括: 1. 视图不能使用临时表 2. 视图不能包含 ORDER BY 子句,除非同时使用了 TOP 或 LIMIT 子句 3. 视图不能包含 INTO OUTFILE 或 INTO DUMPFILE 子句 4. 视图不能被索引,也不能被视为基表 5. 视图不能修改基表的数据,除非使用了特定的 WITH CHECK OPTION 子句 下面是一个 MySQL 视图限制的实例: 假设有一个名为 `orders` 的表,包含以下字段:`order_id`、`customer_id`、`order_date` 和 `total_amount`。 我们可以创建一个名为 `orders_view` 的视图,它只包含 `customer_id` 和 `total_amount` 这两个字段,如下所示: ``` CREATE VIEW orders_view AS SELECT customer_id, total_amount FROM orders; ``` 然而,我们不能在这个视图中包含 ORDER BY 子句,否则会收到以下错误消息: ``` ERROR 1351 (HY000): View's SELECT contains a subquery in the FROM clause ``` 因此,我们应该在使用 ORDER BY 子句时,同时使用 TOP 或 LIMIT 子句来限制结果集的大小,如下所示: ``` CREATE VIEW orders_view AS SELECT customer_id, total_amount FROM orders ORDER BY total_amount DESC LIMIT 10; ``` 这样就可以避免上述错误。 ### 回答2: MySQL视图是虚拟的表,它是根据定义视图时指定的查询语句动态生成的,可以简化复杂的查询操作。然而,MySQL视图也有一些限制。 首先,MySQL视图不支持带有全局或本地临时表的查询。这意味着如果查询需要使用临时表,无法将其放在视图中进行处理。 其次,MySQL视图不能索引。因为视图是根据查询结果动态生成的,而不是实际存储数据,所以无法为视图创建索引。这可能会导致在对视图进行复杂查询时性能下降。 此外,MySQL视图还有许多使用限制。例如,视图不能引用临时表、不能使用存储函数、不能使用用户变量,并且定义视图的SELECT语句不能包含DISTINCT关键字。 下面是一个关于MySQL视图限制的示例: 假设有一个名为"employees"的表,包含员工的姓名、年龄和工资信息。我们希望创建一个名为"young_employees"的视图,只包含年龄小于30岁的员工信息。 创建视图的语句可以是: CREATE VIEW young_employees AS SELECT * FROM employees WHERE age < 30; 然而,如果我们尝试在这个视图上使用DISTINCT关键字进行查询,就会遇到限制: SELECT DISTINCT * FROM young_employees; 会报错,因为MySQL不允许在视图的查询中使用DISTINCT关键字。 综上所述,MySQL视图不支持临时表、无法索引、存在许多其他使用限制。在使用MySQL视图时,我们应该遵守这些限制并考虑它们可能带来的性能问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值