我写了这个选择
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.1所以我希望pics数组只是空的
这只能在一个查询中实现吗?有多个查询没问题,但我希望只有一个。
创建了sqlfiddle
提前感谢您的时间和建议!
答案 0 :(得分:1)
你可以尝试“订购t1.newsdatum DESC,t3.rang ASC”吗?
答案 1 :(得分: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');
}