2017-06-22 4 views
1

という表があり、chance_noの表記はintです。他の2つの列はis_group1is_group2であり、両方ともtinyintタイプであり、互いに排他的です。だから私はis_group1=1is_group2=1max(chance_no)を1つのクエリで取得したいと思います。 相互に排他的な列の複数の最大値を1つの行に取り出します

私は max_chance_group12として来るべきではないので、出力が間違っている

max_chance_group1 max_chance_group2 
       0     3 

として結果を与えたこのクエリ

select if(is_group1 <> 0, max(chance_no), 0) as max_chance_group1, 
     if(is_group2 <> 0, max(chance_no), 0) as max_chance_group2 
from call_memmo 

を試してみました。

は、それから私は、これは正しく出力を与えているが、複数のレコードが

max_chance_group1 max_chance_group2 
       2     3 
       2     3 

私は私が使用したクエリが適切でないことを知って来ているので、誰もが私を助けることができる、このクエリ

select (select max(chance_no) from call_memmo where is_group1 = 1) as max_chance_group1, 
     (select max(chance_no) from call_memmo where is_group2 = 1) as max_chance_group2 

from call_memmo group by is_group1, is_group2 

を試してみましたより良い方法と私は繰り返しをスキップすることができますように単一の行に出力を取得?

+0

によってグループのために、この必要性を全くしてみませんがhttps://meta.stackoverflow.com/questions/333952/を参照してくださいすることができますなぜ、私が提供する、何のためのものなのか、非常にシンプルなSQLクエリ – Strawberry

答えて

1

のような新しいカラムが

select (select max(chance_no) from call_memmo where is_group1 = 1) as max_chance_group1, 
     (select max(chance_no) from call_memmo where is_group2 = 1) as max_chance_group2 
1
select (select max(chance_no) from call_memmo where is_group1 = 1) as max_chance_group1, 
     (select max(chance_no) from call_memmo where is_group2 = 1) as max_chance_group2 
from call_memmo limit 1 

またはuグループ

select (select max(chance_no) from call_memmo where is_group1 = 1) as max_chance_group1, 
(select max(chance_no) from call_memmo where is_group2 = 1) as max_chance_group2 from call_memmo group by max_chance_group1, 
max_chance_group2 
関連する問題