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)

Soo 现在这没问题,但我想要更多:) 不知道这是否可能(这是我写的第一个更大的 sql 查询)。

  1. 缺少一个“元素”,因为在 t3.picture 中没有任何条目

1.1 所以我希望图片数组为空

  1. 我需要按 t1.newsdatum DESC 订购并按 t3.rang ASC 订购

这是否可能仅在一个查询中实现?有多个查询没问题,但我希望只有一个。

创建了一个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 回答