我需要结果如下:
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
我不知道如何制作,所以它显示如上所述。我试过搞乱订单和分组,我得到了同样的结果。我不确定如何将其缩小到结果应该如何。
答案 0 :(得分:0)
尝试:
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;