简体   繁体   English

Joomla PHP MySQL查询使用MySQL IF函数

[英]Joomla PHP MySQL Query to use MySQL IF function

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. 因此,我有一个Joomla网站,并且在joomla文档中,我找不到与查询中MySQL的IF,ELSE函数有关的信息。

The part of the query i need a if statement in MySQL is here. 我需要在MySQL中的if语句的查询部分在这里。

$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. 如果$ number输入与server_number列数据不匹配,则向MySQL查询添加where语句。

Full MySQL Query : 完整的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

Want to add a "IF server_number != 1 THEN WHERE container != 1 END" would mean replacing "AND container != 1" 想要添加“ IF server_number!= 1 THEN WHERE container!= 1 END”将意味着替换“ AND container != 1”

I figured out a better way to resolve my problem using MySQL's 我想出了一种更好的方法来解决使用MySQL的问题

OR || 或||

function 功能

So my fixed code became this : 所以我的固定代码变成了:

PHP : PHP的:

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

In plain MySQL text : 用简单的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM