0

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"

2
  • And exactly what does not work when you use the above code with IF and ELSE? Commented Jun 11, 2016 at 18:37
  • I added the full MySQL query to my question the output with every single IF method i try just errors. MySQL syntax is wrong. Commented Jun 11, 2016 at 19:49

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.