我需要结果看起来像这样:
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
我不确定如何制作它,使其像上面一样显示。我已经尝试弄乱了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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句