mysql 分组、连接和排序问题

标签 mysql

我需要的结果如下:

person_id   last_name   first_name  region_id   region name                         
  1         barnum         phineas      1       maricopa                                    
  2         loman          willy        2       pima                                        
  2         loman          willy        3       pinal                                       
  2         loman          willy        4       santa cruz                                  
  3         kay            mary         5       cochise                                     
  3         kay            mary         6       gila                                        
  3         kay            mary         7       graham  

我的代码是这样的:

select `person_id`, `last_name`, `first_name`,
`Region_id`, `name` AS 'Region Name'   
from `sales_region`  
inner join sales_people  
on `person_id` = `person_id`      
group by  `region_id` asc, `person_id`        
having `person_id`in ('1','2','3')       
order by `person_id`,`region_id` asc
;

它给了我这个:

person_id   last_name   first_name  Region_id   "Region Name"
1           barnum      phineas     1            maricopa
1           barnum      phineas     2             pima
1           barnum      phineas     3            pinal
1           barnum      phineas     4         santa cruz
1           barnum      phineas     5           cochise
1           barnum      phineas     6            gila
1           barnum      phineas     7           graham
2           loman       willy       1          maricopa
2           loman       willy       2          pima
2           loman       willy       3          pinal
2           loman       willy       4          santa cruz
2           loman       willy       5          cochise
2           loman       willy       6          gila
2           loman       willy       7          graham
3           kay         mary        1          maricopa
3           kay         mary        2              pima
3           kay         mary        3           pinal
3           kay         mary        4       santa cruz
3           kay         mary        5       cochise
3           kay         mary        6       gila
3           kay         mary        7       graham

我不知道如何制作它,所以它显示得像上面那样。我尝试弄乱 order by 和 group by 并得到相同的结果。我不知道如何将范围缩小到结果应该如何。

最佳答案

尝试:

select sales_people.person_id, last_name, first_name, sales_region.Region_id, trim(sales_region.name) AS 'Region Name'   
   from sales_region
   inner join sales_people_region on sales_people_region.region_id = sales_region.region_id 
   inner join sales_people on sales_people_region.person_id = sales_people.person_id

where sales_people.person_id in (1,2,3)
group by  sales_region.region_id, sales_people.person_id
order by sales_people.person_id, sales_region.region_id asc;

关于mysql 分组、连接和排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36820352/

相关文章:

mysql - 是否有任何 SQL 前端显示子数据表?

mysql - 从另一个表实体更新 MySQL 表列

MySQL 在行 SUM 之前选择连接和列乘法

mysql - 操作失败: There was an error while applying the SQL script to the database. MYSQL

mysql - 根据值对行进行分组并插入到另一个表中

mysql - 在 MySQL 中对 JSON 字段进行全文搜索

php - mcrypt 字符导致 mysql 错误

php - 如何在 codeIgniter (mysql) 中按 'name + 0' 排序

mysql - 按正确顺序获取选择输出的结果

mysql - 根据行号查找行