472,127 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

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 4075
mwasif
802 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
802 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 leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.