【MYSQL】select from group by

  • group by
mysql> select * from t_user group by lvl;
+-----+-------+----------+------+--------+
| uid | uname | tel      | lvl  | salart |
+-----+-------+----------+------+--------+
|   1 | alex  | 23131231 | A    |   1100 |
|   3 | kit   | 87416574 | B    |   1200 |
|   4 | poker | 41874521 | C    |   1560 |
+-----+-------+----------+------+--------+
mysql> select lvl from t_user group by lvl;
+------+
| lvl  |
+------+
| A    |
| B    |
| C    |
+------+
  • group by + group_concat()
mysql> select lvl,group_concat(salart) from t_user group by lvl;
+------+----------------------+
| lvl  | group_concat(salart) |
+------+----------------------+
| A    | 1100,4300            |
| B    | 1200                 |
| C    | 1560                 |
+------+----------------------+
3 rows in set (0.00 sec)

mysql> select lvl,group_concat(uid) as salary from t_user group by lvl;
+------+--------+
| lvl  | salary |
+------+--------+
| A    | 1,2    |
| B    | 3      |
| C    | 4      |
+------+--------+
3 rows in set (0.00 sec)

mysql> select lvl,group_concat(salart) from t_user group by lvl;
+------+----------------------+
| lvl  | group_concat(salart) |
+------+----------------------+
| A    | 1100,4300            |
| B    | 1200                 |
| C    | 1560                 |
+------+----------------------+
3 rows in set (0.00 sec)

mysql> select lvl,group_concat(salart) as salary from t_user group by lvl;
+------+-----------+
| lvl  | salary    |
+------+-----------+
| A    | 1100,4300 |
| B    | 1200      |
| C    | 1560      |
+------+-----------+
3 rows in set (0.00 sec)
  • #group by + 集合函数
mysql> select lvl,group_concat(uid) as salary from t_user group by lvl;
+------+--------+
| lvl  | salary |
+------+--------+
| A    | 1,2    |
| B    | 3      |
| C    | 4      |
+------+--------+
3 rows in set (0.00 sec)

mysql> select lvl,count(uid) as salary from t_user group by lvl;
+------+--------+
| lvl  | salary |
+------+--------+
| A    |      2 |
| B    |      1 |
| C    |      1 |
+------+--------+
3 rows in set (0.00 sec)
  • #group by + having
mysql> select lvl,group_concat(salart) from t_user group by lvl having group_concat(salart)=1200;
+------+----------------------+
| lvl  | group_concat(salart) |
+------+----------------------+
| B    | 1200                 |
+------+----------------------+
1 row in set (0.00 sec)

mysql> select lvl,group_concat(salart) from t_user group by lvl having group_concat(salart)=1100;
+------+----------------------+
| lvl  | group_concat(salart) |
+------+----------------------+
| A    | 1100,4300            |
+------+----------------------+
1 row in set (0.00 sec)

mysql>
mysql> select lvl,count(uid) as salary from t_user group by lvl;
+------+--------+
| lvl  | salary |
+------+--------+
| A    |      2 |
| B    |      1 |
| C    |      1 |
+------+--------+
3 rows in set (0.00 sec)

mysql> select lvl,count(uid) as salary from t_user group by lvl having count(uid)>1;
+------+--------+
| lvl  | salary |
+------+--------+
| A    |      2 |
+------+--------+
1 row in set (0.00 sec)
时间: 2024-09-30 19:21:48

【MYSQL】select from group by的相关文章

【Mysql】select

描述 The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. 大意:select用来从数据库中挑选数据 有以下两种形式 SELECT column_name,column_name FROM table_name; 和 SELECT * FROM table_name; 下面是测试代码 #include <

【Mysql】利用group by附带having进行聚类查询

聚类查询所针对的对象是表的其中一列,譬如如下的testtable表,要查出username这一列中,各个项所出现的次数,则用到聚类查询 显然,聚类查询之后,得到的结果必须与id,number这两列半点关系都没有.因此,也就是正如上门,所说,聚类查询所针对的对象是表的其中一列. 聚类查询往往配合count(*),sum(*),avg(*)等聚类语句,用来对表进行统计.由于查询的字段往往是字符串varchar类型,因此统计该字符串的数目count(*)这个函数就是出现最多的. 比如,要查询上面的te

【MySql】赶集网mysql开发36条军规

[MySql]赶集网mysql开发36条军规 2012-05-14 14:02:33 分类: Linux 写在前面的话: 总是在灾难发生后,才想起容灾的重要性: 总是在吃过亏后,才记得曾经有人提醒过. (一)核心军规 (1)不在数据库做运算 cpu计算务必移至业务层: (2)控制单表数据量 int型不超过1000w,含char则不超过500w: 合理分表: 限制单库表数量在300以内: (3)控制列数量 字段少而精,字段数建议在20以内: (4)平衡范式与冗余 效率优先: 往往牺牲范式: (5)

FluentData -Micro ORM with a fluent API that makes it simple to query a database 【MYSQL】

官方地址:http://fluentdata.codeplex.com/documentation MYSQL: MySQL through the MySQL Connector .NET driver. 连接字符串:Server=127.0.0.1;Database=testDB;Uid=root;Pwd=jnex;<system.data> <DbProviderFactories> <add name="MySQL Data Provider" i

【Mysql】—— 报错:Can&#39;t call commit when autocommit=true

java.sql.SQLException: Can't call commit when autocommit=true at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:934) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931) at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.jav

【MySQL】MySQL锁和隔离级别浅析一

参考:http://imysql.cn/2008_07_10_innodb_tx_isolation_and_lock_mode 本文只是对于"SELECT ... LOCK IN SHARE MODE"和"SELECT ... FORUPDATE"事务中的锁和RR隔离级别内的测试,针对于表结构.索引结构以及其他隔离级别情况下的触发锁类型,可以参考网易何登成网盘中"MySQL 加锁处理分析.pdf"这篇文章,很细致. 何登成百度网盘:http:/

【Mysql】常用指令之——用户操作(创建,授权,修改,删除)

Mysql中的用户 user 每一个user都对应了不同的用户地址和权限 创建Mysql用户共有三种方式1.create user 2.grant 3.操作mysql.user表 1.CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例子: CREATE USER 'aa'@'localhost' IDENTIFIED BY '123456'; CREATE USER 'aa'@'192.168.1.101_' IDENDIFIED

【MySql】性能优化之分析命令

[MySql]性能优化之分析命令     一 当发现程序运行比较慢的时候,首先排除物力资源问题之后,就将注意力转向mysq数据库: 1.首先确定运行慢的sql语句: mysql> show full processlist; 2.确认低效的查询: 多次执行第一步发现time耗费大的sql语句.查看耗费的时间. 3.为sql生成一个执行计划query Execution plan(QEP) mysql> explain select * from tbal_name where ...; 4.查

【Mysql】将Mysql的一张表导出至Excel

把Mysql的一张表导出至Excel不像<[Mysql]将Excel表导入至Mysql的其中一张表>(点击打开链接)那样困难, 直接输入如下的T-SQL语句即可: select * from 某个表 into outfile '文件路径' 比如,我要把如下的test数据库的一张i_student表导出至d盘根目录下的1.xls: use test 之后,输入: select * from i_student into outfile 'd:/1.xls' 如下图: 然后你就会发现在D盘静静躺着