Joomla PHP MySQL Query使用MySQL IF功能

时间:2016-06-11 12:21:00

标签: php mysql joomla

所以我有一个Joomla网站,在joomla文档中,我无法找到与查询中的MySQL的IF,ELSE函数有关的任何内容。

我需要在MySQL中使用if语句的部分查询。

$query->where($db->quoteName('container').' != 1');

它应该做这样的事情:

$query->where('IF '.$db->quoteName('server_number').' != '.$number.' THEN '$query->where($db->quoteName('container').' != 1');' END');

如果$ number输入与server_number列数据不匹配,则将where语句添加到mysql查询。

完整的MySQL查询:

SELECT a.*,ext.media_type
FROM database_hwdms_processes AS a
LEFT JOIN database_hwdms_media AS media ON media.id = a.media_id
LEFT JOIN database_hwdms_ext AS ext ON ext.id = media.ext_id
WHERE (a.status = 1 || a.status = 3) AND a.attempts < 5 AND `container` != 1 AND
 server = 1
ORDER BY a.media_id ASC

想要添加一个&#34; IF server_number!= 1 THEN WHERE container!= 1 END&#34;将意味着替换&#34; AND container!= 1&#34;

1 个答案:

答案 0 :(得分:0)

我想出了一个更好的方法来解决我使用MySQL的问题

  

OR ||

功能

所以我的固定代码变成了这个:

PHP:

$query->where('('.$db->quoteName('server_number').' = '.$number.' || '.$db->quoteName('container').' != 1 )');

在普通的MySQL文本中:

SELECT a.*,ext.media_type
FROM database_hwdms_processes AS a
LEFT JOIN database_hwdms_media AS media ON media.id = a.media_id
LEFT JOIN database_hwdms_ext AS ext ON ext.id = media.ext_id
WHERE (a.status = 1 || a.status = 3) AND a.attempts < 5 AND ( `server_number` = 1 || `container` != 1 )AND
 server = 1
ORDER BY a.media_id ASC