dozan0001 2016-06-11 12:21
浏览 85
已采纳

Joomla PHP MySQL Query使用MySQL IF功能

So i have a Joomla site and in the joomla documentation I can't find anything to do with MySQL's IF, ELSE function within a query.

The part of the query i need a if statement in MySQL is here.

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

It should be doing something like this :

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

If the $number input does not match with the server_number column data then to add a where statement to the mysql query.

Full MySQL Query :

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

Want to add a "IF server_number != 1 THEN WHERE container != 1 END" would mean replacing "AND container != 1"

  • 写回答

1条回答 默认 最新

  • doushu9253 2016-06-13 00:31
    关注

    I figured out a better way to resolve my problem using MySQL's

    OR ||

    function

    So my fixed code became this :

    PHP :

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

    In plain MySQL text :

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型