2016-04-24 5 views
0

によって順: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 

私のコードはこれです:

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 

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 
; 

それは私にこれを提供します

私はそれを作る方法がわからないので、上記のように表示されます。私は順序を混乱させようとしましたが、私は同じ結果を得ています。結果をどのようにするべきかを絞り込む方法がわかりません。

答えて

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; 
+0

感謝を!私は両方の試行でエラーが発生します。オーバーラッピングカラム情報はありません。 person_idはsales_peopleにのみ存在します。 region_idはsales_regionにのみ存在します。私は複数の列から選択できるので、私はテーブルに加わるだけです。 2つの選択クエリを書くときにエラーが発生します。 – talesin22

+0

'region_id'と' person_id'列の両方を含むピボットテーブルがありませんか? 'sales_people_region'や' people_region'などと似ていますか? –

+0

私はperson_idとregion_idの両方を持つsales_people_regionというテーブルを持っています。それは不要と思われたので、私はそれをクエリに含めませんでした。 – talesin22

関連する問題