具體我忘記了,大概內容差不多如下。 兩張表,內容如下: 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