noorwala
noorwala

Reputation: 121

Mutiple conditional statement in mysql query

how to write else if statement in the mysql query

if the string is three characters first check is a country code if the string is five character check country name otherwise, check city name or country name

like in this way

SELECT * FROM `geo_allcities` WHERE if(country_code3)='pak' 
else if(SUBSTRING(country_name, 1, 5))='pakis' 
else ( country_name LIKE'other string%' OR city_name like 'other string')

Upvotes: 0

Views: 63

Answers (3)

Friday Ameh
Friday Ameh

Reputation: 1684

First is you check the string length using the string length function for example if(queryString.length())=5 then your query statement else if(queryString.length())=3 then your query statements, else report error.. Hope this solves the problem

Upvotes: 0

Ngo Tuan
Ngo Tuan

Reputation: 205

You can use CASE EXPRESSION.

For more detail, visit : https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html#operator_case

Example :

SELECT col1, col2, (case when (action = 2 and state = 0) 
 THEN
      1 
 ELSE
      0 
 END)
 as state from tbl;

Upvotes: 0

Rams
Rams

Reputation: 2169

No need of if statement, you can simply put or condition

SELECT * FROM `geo_allcities` 
WHERE country_code3='pak' or 
SUBSTRING(country_name, 1, 5)='pakis' or
  country_name LIKE'other string%' OR 
city_name like 'other string'

Upvotes: 2

Related Questions