0

这里有一个示例查询以获取两种类型的图标..

$fetchIcon = array();
$queryIcon = $db->query("SELECT * FROM icon_code WHERE icon_type = 'icon'");
while ($fIcon = $db->fetch_assoc($queryIcon)) {
    $fetchIcon[] = $fIcon;
}

$fetchOther = array();
$queryOther = $db->query("SELECT * FROM icon_code WHERE icon_type = 'other'");
while ($fOther = $db->fetch_assoc($queryOther)) {
    $fetchOther[] = $fOther;
}

然后我用来foreach在同一页面上显示输出。

foreach ($fetchIcon as $Icon) {
  echo $Icon['IconName']; // select option for Icon
}

foreach ($fetchOther as $Other) {
  echo $Other['IconName']; // select option for other icon
}

问题: 如何将两条语句结合起来进行一次查询?

4

3 回答 3

5
SELECT * FROM icon_code 
WHERE icon_type = 'icon' 
or icon_type = 'other' 
ORDER BY icon_type

或者

SELECT * FROM icon_code 
WHERE icon_type in ('icon', 'other')
ORDER BY icon_type

order by按类型获取订单是可选的。

于 2012-05-08T19:44:48.117 回答
3
SELECT *
FROM icon_code
WHERE icon_type IN ('icon', 'other')

进而

$fetchIcon = array();
$fetchOther = array();

while($row = mysql_fetch_assoc($result)) {
    switch($row['icon_type']) {
       case 'icon':
           $fetchIcon[] = $row;
           break;
       case 'other':
       default:
           $fetchOther[] = $row;
    }
}
于 2012-05-08T19:46:55.283 回答
2

我建议使用IN函数:

$queryOther = $db->query("SELECT * FROM icon_code WHERE icon_type IN ('other', 'icon'");
于 2012-05-08T19:46:24.607 回答