469,590 Members | 2,446 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,590 developers. It's quick & easy.

condition in CONCAT

160 100+
hi to all

i have three fields of name in database named as fnam,mname,lname. fname and lname is mandatory field and mname is optional.

i want to make a search query on name
Expand|Select|Wrap|Line Numbers
  1. mysql_query("SELECT * FROM abc where CONCAT(fname,' ',mname,' ',lname) like'" . $q .  "%' order by id");
  2.  
in records where mname is not empty then above code is working properly but records in which mname is empty then i have to enter double space between fname and lname which disturb search

on the other case when i write

Expand|Select|Wrap|Line Numbers
  1. mysql_query("SELECT * FROM abc where CONCAT(fname,' ',mname,'',lname) like'" . $q . "%' order by id");
  2.  
in records where mname is empty then above code is working properly but records in which mname is not empty then i have to remove space between fname and lname which disturb search

please provide me some solution. can i use if condition in concat?

i need to know if i am not clear in my question
Jan 23 '09 #1
4 4004
mwasif
801 Expert 512MB
Yes, you can use IF() within CONCAT().
Jan 23 '09 #2
waqasahmed996
160 100+
then please guide me about syntax
Jan 23 '09 #3
mwasif
801 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT CONCAT(fname,' ', IF(mname<>'', CONCAT(mname,' '), '') ,lname) FROM abc
  3.  
Jan 23 '09 #4
waqasahmed996
160 100+
thanks a lot........
Jan 23 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Gerald Aichholzer | last post: by
3 posts views Thread by Bryan Valencia | last post: by
16 posts views Thread by Jacky | last post: by
8 posts views Thread by Doug Stiers | last post: by
1 post views Thread by Trint Smith | last post: by
4 posts views Thread by Martin Evans | last post: by
6 posts views Thread by morch | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.