0
私は'House'
と呼ばれる最初の2つの異なるテーブルを持つデータベースを持っており、次の列:id, name, updated_at
を含んでいます。私の2番目のテーブルは'House_meta'
と呼ばれ、次の列を含みます:house_id, name, value
。メタテーブルで私は、次のデータ(例えば)持っている:SQL:列として複数の行の値を結合する
house_ id name value
1 street 123 streetname
1 color black
1 city chicago
1 image image-1.jpg
1 image image-2.jpg
1 image image-3.jpg
が、私は、各家のエントリが唯一の行にあるように、これらのテーブルを結合する必要があります。ある画像の一方のみに
SELECT
house.id AS house_id,
house.name AS housename,
updated_at
max(case when house_meta.name = 'street' then house.value end) as street,
max(case when house_meta.name = 'color' then house.value end) as color,
max(case when house_meta.name = 'city' then house.value end) as city,
max(case when house_meta.name = 'image' then house.value end) as image
FROM house
join house_meta on house.id = house_meta.house_id
group by
house.id
こののみ(自然に)結果:私の問題は、それら全てが等画像1、画像2、私の現在のSQLのように出力される可能性が同じ行上にあるとする必要があり、画像であります追加されました。どのようにすれば、すべての画像が1つの行に表示されるようにすることができますか?私がしたいどのように
それが返さ:
house_id updated_at street color city image1 image2 image3
1 2016-12-02 123 streetname black chicago image-1.jpg image-2.jpg image-3.jpg
を助けます – Rahul