If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > MySQL - IF Condition in Where clause?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-11, 12:06
cancer10 cancer10 is offline
Registered User
 
Join Date: Mar 2008
Posts: 33
Question MySQL - IF Condition in Where clause?

Hi

I was wondering if we can use SQL's IF ELSE condition in the where clause?

Let me explain you what exactly I am looking for:

Consider the following DDL:

Code:
/*Table structure for table `users` */
CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `city_name` varchar(50) DEFAULT NULL,
  `user_name` varchar(50) DEFAULT NULL,
  `is_admin` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

and the Data :
Code:
/*Data for the table `users` */

insert  into `users`(`id`,`city_name`,`user_name`,`is_admin`) values (1,'AA','joe',1),(2,'BB','smith',0),(3,'CC','ricky',0),(4,'DD','mathew',0),(5,'EE','ricky',1),(6,'FF','martin',0),(7,'AA','parry',0),(8,'AA','james',0),(9,'BB','ricky',1);

I want to do a count of the cities available in the table but at the same time I want to put a condition such that:

The system should count ALL cities available, but in case if the user_name is ricky, the system should also check if the is_admin column is 1. If either of these conditions fail, the count for this row should NOT happen.

So in our case, row # 3 should NOT be counted as the is_admin for it is set to 0.

http://i.imgur.com/Y9VXG.png



I am trying to run the following query but it seems like MySQL does not support IF ELSE in the where clause.

Code:
SELECT 	id, 
	city_name, 
	user_name, 
	is_admin,
	COUNT(*) AS city_count
	FROM 
	test.users 

WHERE 
IF(user_name = 'ricky')
	is_admin=1
END IF;
GROUP BY city_name


Any help will be appreciated.

Thanks
__________________
Interview Questions & Answers - www.focusinterview.com
http://outlineme.com/cancer10

Last edited by cancer10; 09-27-11 at 12:25.
Reply With Quote
  #2 (permalink)  
Old 09-27-11, 13:31
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
don't boither with an if statement, even if MySQL supported such a syntax

its all down to your where clause
If I read your post right
then you want all rows except where the userid = ricky and is_admin = true

Code:
where not (userid='ricky' and isadmin = false)
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 09-27-11, 23:44
cancer10 cancer10 is offline
Registered User
 
Join Date: Mar 2008
Posts: 33
Quote:
Originally Posted by healdem View Post
don't boither with an if statement, even if MySQL supported such a syntax

its all down to your where clause
If I read your post right
then you want all rows except where the userid = ricky and is_admin = true

Code:
where not (userid='ricky' and isadmin = false)
A small alteration in your understanding.

I want:

all rows except where the userid = ricky and is_admin = 0
__________________
Interview Questions & Answers - www.focusinterview.com
http://outlineme.com/cancer10
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On