0

私はこの選択を書きました

SELECT
  t1.id t1_id, t1.title t1_title, t1.subtitle t1_subtitle, t1.content t1_content,
  t3.picture t3_picture

FROM
  webcms_cms_content t1

INNER JOIN webcms_mod_galleries_pictures t3
  ON t1.gallery_id = t3.gallery_id

WHERE t1.structure_id =3
AND t1.status=1
AND t1.flag=1
ORDER BY t1.newsdatum DESC

そして、この小さな php スニペット:

$i=0;
while($db->next_record()) {
    $con[$db->get_content('t1_id')]['id']           = $db->get_content('t1_id');
    $con[$db->get_content('t1_id')]['title']        = $db->get_content('t1_title');
    $con[$db->get_content('t1_id')]['link']         = $db->get_content('t1_subtitle');
    $con[$db->get_content('t1_id')]['content']      = $db->get_content('t1_content');
    $con[$db->get_content('t1_id')]['pics'][$i]     = $db->get_content('t3_picture');
    $i++;
}

次のような配列を返します。

array
    8 => 
        array
            'id' => string '8' (length=1)
            'title' => string 'Studios (2 Personen)' (length=20)
            'link' => string '350.- bis 550.- ' (length=16)
            'content' => string 'Gemütlich eichenmöbiliertes Studio bestehend aus einem Wohnraum mit zwei Schrankbetten, Fernseher mit Radio, Telefon, Wireless, Küche mit Backofen, Wasserkocher und Kaffeemaschine, Bad/Dusche, WC und auf der Südseite mit Terasse und Matterhornblick.' (length=269)
            'pics' => 
                array
                    0 => string 'p186rgot3ohd612pljp8m8h12d74' (length=28)
                    1 => string 'p186rgot3o1cso2nj4h81e47ils2' (length=28)
                    2 => string 'p186rgl7f84r9h671jkr1vctip61' (length=28)
                    3 => string 'p186rgot3o6fjk7c1j1nc1p1ukj1' (length=28)
                    4 => string 'p186rhu0bjuq9tfe1ca13ll1qte1' (length=28)

これで問題ありませんが、もっと多くのことが必要です :) これが可能かどうかはわかりません (これは私が書いた最初の大きな SQL クエリです)。

  1. t3.picture にエントリがないという「要素」の欠落原因が 1 つあります。

1.1なので、pics配列を空にしたいだけです

  1. t1.newsdatum DESC で注文し、t3.rang ASC で注文する必要があります。

これは1回のクエリで可能ですか? 複数のクエリを使用しても問題ありませんが、1 つだけにしたいと思います。

sqlfiddleを作成しました

あなたの時間と提案を前もってありがとう!

4

2 に答える 2

1

「ORDER BY t1.newsdatum DESC, t3.rang ASC」を試してみませんか?

于 2013-10-14T15:11:54.857 に答える
0

LEFT OUTER JOIN を使用して、他のテーブルに一致する行がない行を取得できます。

SELECT t1.id AS t1_id, 
        t1.title AS t1_title, 
        t1.subtitle AS t1_subtitle, 
        t1.content AS t1_content,
        t3.picture AS t3_picture
FROM webcms_cms_content t1
LEFT OUTER JOIN webcms_mod_galleries_pictures t3
ON t1.gallery_id = t3.gallery_id
WHERE t1.structure_id = 3
AND t1.status = 1
AND t1.flag = 1
ORDER BY t1.newsdatum DESC, t3.rang ASC

次に、配列を設定するためのいくつかのphp:-

$prev_id = 0;
while($db->next_record()) 
{
    if ($prev_id != $db->get_content('t1_id'))
    {
        $con[$db->get_content('t1_id')]['id']       = $db->get_content('t1_id');
        $con[$db->get_content('t1_id')]['title']    = $db->get_content('t1_title');
        $con[$db->get_content('t1_id')]['link']     = $db->get_content('t1_subtitle');
        $con[$db->get_content('t1_id')]['content']  = $db->get_content('t1_content');
        $con[$db->get_content('t1_id')]['pics']     = array();
        $prev_id = $db->get_content('t1_id');
    }
    $con[$db->get_content('t1_id')]['pics'][]     = $db->get_content('t3_picture');
}
于 2013-10-15T15:44:30.137 に答える