Home > Database > Mysql Tutorial > mysql一个简单查询中的group by和order by的使用_MySQL

mysql一个简单查询中的group by和order by的使用_MySQL

WBOY
Release: 2016-06-01 13:43:32
Original
994 people have browsed it

bitsCN.com 具体我忘记了,大概内容差不多如下。两张表,内容如下:Sql代码  +--------+-------+------+  | z_code | mgr   | pigs |  +--------+-------+------+  | 021    | zhang | 85   |  | 010    | wang  | 70   |  +--------+-------+------+  2 rows in set  查询最终结果  Sql代码  +--------+------+-------+  | z_code | city | mgr   |  +--------+------+-------+  | 010    | BJ   | wang  |  | 021    | HB   | zhang |  +--------+------+-------+  2 rows in set  A表  Sql代码  +------+-------+------+  | part | mgr   | pigs |  +------+-------+------+  | 001  | wang  |   20 |  | 002  | wang  |   50 |  | 011  | zhang |   49 |  | 012  | zhang |   36 |  +------+-------+------+  4 rows in set   B表 所用查询语句为:Sql代码  select z_code, A.mgr mgr, sum(pigs) pigs from A, B where A.mgr = B.mgr group by A.mgr order by pigs desc;    下面是建表和插入语句的SQL:Sql代码  create database pig_store;  use pig_store;    create table A(      z_code varchar(8),      city varchar(4),      mgr varchar(16)  );  create table B(      part varchar(8),      mgr varchar(16),      pigs int(5)  );    insert into A values ( '010', 'BJ', 'wang'), ( '021', 'HB', 'zhang' );  insert into B values ( '001', 'wang', 20 ), ( '002', 'wang', 50 ), ( '011', 'zhang', 49 ), ( '012', 'zhang', 36 );     作者 wangtingbang bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template