设为首页 加入收藏

TOP

MySQL中关于ORDERBY、DISTINCT、ALTER、LIKE/NOTLIKE、REGEXP/NOTREGEXP、COUNT、MAX的使用介绍(二)
2018-03-14 09:00:37 】 浏览:444
Tags:MySQL 关于 ORDERBY DISTINCT ALTER LIKE/NOTLIKE REGEXP/NOTREGEXP COUNT MAX 使用 介绍
ame REGEXP '^.{5}$';

分组计数:
You can use COUNT() if you want to find out how many pets each owner has:

mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;

ONLY_FULL_GROUP_BY参数开启与未开启区别

If the ONLY_FULL_GROUP_BY SQL mode is enabled, an error occurs:

mysql> SET sql_mode = 'ONLY_FULL_GROUP_BY';
Retrieving Information from a Table
265
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT owner, COUNT(*) FROM pet;
ERROR 1140 (42000): In aggregated query without GROUP BY, expression
#1 of SELECT list contains nonaggregated column 'menagerie.pet.owner';
this is incompatible with sql_mode=only_full_group_by

If ONLY_FULL_GROUP_BY is not enabled, the query is processed by treating all rows as a single
group, but the value selected for each named column is indeterminate. The server is free to select
the value from any row:

mysql> SET sql_mode = '';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT owner, COUNT(*) FROM pet;
+--------+----------+
| owner | COUNT(*) |
+--------+----------+
| Harold | 8 |
+--------+----------+
1 row in set (0.00 sec)

假如有一个商品的数据表,请查找出价格最贵的一件商品的名称以及价格

SELECT name,MAX(price) AS price FROM shop;

also:

SELECT article, dealer, price
FROM shop
WHERE price=(SELECT MAX(price) FROM shop);
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MySQL事务的使用详解 下一篇SQL Server2005实例讲解数据库的..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目