2017-06-28 8 views
0
+----+---------------+--------------------+------------+----------+-----------------+ 
| id | restaurant_id |  filename  | is_profile | priority | show_in_profile | 
+----+---------------+--------------------+------------+----------+-----------------+ 
| 40 |   20 | 1320849687_390.jpg |   |   |    1 | 
| 60 |   24 | 1320853501_121.png |   1 |   |    1 | 
| 61 |   24 | 1320853504_847.png |   |   |    1 | 
| 62 |   24 | 1320853505_732.png |   |   |    1 | 
| 63 |   24 | 1320853505_865.png |   |   |    1 | 
| 64 |   29 | 1320854617_311.png |   1 |   |    1 | 
| 65 |   29 | 1320854617_669.png |   |   |    1 | 
| 66 |   29 | 1320854618_636.png |   |   |    1 | 
| 67 |   29 | 1320854619_791.png |   |   |    1 | 
| 74 |   154 | 1320922653_259.png |   |   |    1 | 
| 76 |   154 | 1320922656_332.png |   |   |    1 | 
| 77 |   154 | 1320922657_106.png |   |   |    1 | 
| 84 |   130 | 1321269380_960.jpg |   1 |   |    1 | 
| 85 |   130 | 1321269383_555.jpg |   |   |    1 | 
| 86 |   130 | 1321269384_251.jpg |   |   |    1 | 
| 89 |   28 | 1321269714_303.jpg |   |   |    1 | 
| 90 |   28 | 1321269716_938.jpg |   1 |   |    1 | 
| 91 |   28 | 1321269717_147.jpg |   |   |    1 | 
| 92 |   28 | 1321269717_774.jpg |   |   |    1 | 
| 93 |   28 | 1321269717_250.jpg |   |   |    1 | 
| 94 |   28 | 1321269718_964.jpg |   |   |    1 | 
| 95 |   28 | 1321269719_830.jpg |   |   |    1 | 
| 96 |   43 | 1321270013_629.jpg |   1 |   |    1 | 
+----+---------------+--------------------+------------+----------+-----------------+ 

私はこのテーブルを持っており、レストランIDのリストのファイル名を選択します。 24,29,154については、例えば :ランダムなものを取得しない場合、基準に一致するすべての行を選択

+----+--------------- 
|  filename  | 
+----+--------------- 
1320853501_121.png (has is_profile 1) 
1320854617_311.png (has is_profile 1) 
1320922653_259.png (chosen as profile picture because restaurant doesn't have a profile pic but has pictures) 

私は声明で、ケースのグループを試してみましたが、私はあなたがそれによってグループを使用する場合で、完全なグループである必要がありnowhere.Alsoました。

+0

ご使用のデータベースで質問にタグを付けてください。 –

答えて

2

あなたは集約し、いくつかのロジックでこれを行うことができます。

select restaurant_id, 
     coalesce(max(case when is_profile = 1 then filename end), 
       max(filename) 
       ) as filename 
from t 
where restaurant_id in (24, 29, 154) 
group by restaurant_id; 

/プロファイルのファイル名の最初に見て。次に任意のものを選んでください。

+0

restaurant_idのようにselectにファイル名の列を入れる方法はありますか? –

+0

ファイル名が選択されています。私はそのコメントを理解していない。あなたがレストランを望んでいない場合は、ただそれを削除します。 –

関連する問題