There is a sample in the variable.

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

 $db_query="select * from ".DB_PREF."advert advert" ." where " ." advert.hide='show' " ." and advert.end_putdate > ".$GLOBALS["timeGlobal"]." " ." order by premium_adv DESC, sort_time DESC limit ".KOL_NEW_ADVERT_TO_GLAV; 

2 answers 2

Use the IF function:

  select IF (advert.hide='show', 'Опубликовано', 'Скрыто') from ... 

The function looks like this:

 IF(expr1,expr2,expr3) 

If expr1 true then IF() returns expr2 , otherwise returns expr3 .

  • one
    Just remember that not all SQL dialects support this feature. However, if we are talking about MySQL, then this is a rather compact version compared to CASE. - cheops
  • Yes, well noticed. If you use from popular databases, such as MSSQL, PostgreSql, Oracle, then this function does not work, but in this case, most likely there is a question about MySQL. - Firepro

To solve this problem, it is best to use the CASE expression, you can pass the required condition to it hide='show' and, depending on whether it accepts TRUE or FALSE you need to return the value. One possible use case for CASE given below.

 $db_query="select id, hide, end_putdate, premium_adv, sort_time, CASE WHEN hide='show' THEN 'Опубликовано' ELSE 'Не опубликовано' END AS hide from ".DB_PREF."advert advert" ." where " ." advert.hide='show' " ." and advert.end_putdate > ".$GLOBALS["timeGlobal"]." " ." order by premium_adv DESC, sort_time DESC limit ".KOL_NEW_ADVERT_TO_GLAV;