我想知道是否有办法在SQL语法中执行IF
ELSE
语句,或者是否有人可以帮助我CASE
。基本上,如果在file_url
列中找到文件名(字符串),则更改我要使用辅助信息的位置。我希望我的例子可以帮助进一步解释我想要做的事情。
$sql = "SELECT vm.file_url FROM #__virtuemart_medias AS vm
INNER JOIN #__virtuemart_product_medias AS pm ON vm.virtuemart_media_id = pm.virtuemart_media_id
WHERE pm.virtuemart_product_id = {$product_id} ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$FILEURL = $row["vm.file_url"];
if ($FILEURL == "thumb_phone01.jpg"){
$sql .= "AND pm.ordering = 2";
}
else {
$sql .= "AND pm.ordering = 1";
}
$this->_db->setQuery($sql);
$img = $this->_db->LoadResult();
return $img;
}
谢谢。
答案 0 :(得分:2)
一种选择是使用CASE和REGEXP:
SELECT vm.file_url FROM #__virtuemart_medias AS vm
INNER JOIN #__virtuemart_product_medias AS pm
ON vm.virtuemart_media_id = pm.virtuemart_media_id
WHERE pm.virtuemart_product_id = {$product_id}
AND pm.ordering = CASE
WHEN vm.file_url REGEXP 'thumb_phone01.jpg'
THEN 2
ELSE 1
END
这是SQL Fiddle。
祝你好运。