Home php How to apply if / else conditions in SQL?

How to apply if / else conditions in SQL?

Author

Date

Category

There is a sample in a variable.

Tell me how to apply the if / else in SQL .
For example, if advert.hide = 'show' , then ….. otherwise another.

$ db_query = "select *
  from ".db_pref." Advert Advert "
  . "WHERE"
  . "Advert.hide = 'Show'"
  . "" and advert.end_putdate & gt; "$ globals [" TimeGlobal "]." "
  . "Order by Premium_adv Desc, Sort_time Desc Limit" .kol_new_advert_to_glav;

Answer 1, Authority 100%

Use the if function:

select if (advert.hide = 'show', 'Published', 'hidden') from ...

The function looks in general form, so:

if (expr1, expr2, expr3)

If Expr1 Truth then if () Returns Expr2 , otherwise returns Expr3 .


Answer 2, Authority 50%

To solve this task, it is best to use the case expression, it can be sent to the desired hide = 'show' and depending on whether it takes True or False You need to return the value. One of the possible uses of Case is given below

$ db_query = "select
  id
  Hide
  END_PUTDATE,
  Premium_adv,
  sort_time
  Case When Hide = 'Show' Then 'Published' ELSE 'Not Posted by' End As Hide
from ".db_pref." Advert Advert "
. "WHERE"
. "Advert.hide = 'Show'"
. "" and advert.end_putdate & gt; "$ globals [" TimeGlobal "]." "
. "Order by Premium_adv Desc, Sort_time Desc Limit" .kol_new_advert_to_glav;
Previous articleSQL: Sort by date
Next articleText Display in Div

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions