condition in CONCAT

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • waqasahmed996
    New Member
    • Jun 2008
    • 160

    condition in CONCAT

    hi to all

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

    i want to make a search query on name
    Code:
    mysql_query("SELECT * FROM abc where CONCAT(fname,' ',mname,' ',lname) like'" . $q .  "%' order by id");
    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

    Code:
    mysql_query("SELECT * FROM abc where CONCAT(fname,' ',mname,'',lname) like'" . $q . "%' order by id");
    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
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Yes, you can use IF() within CONCAT().

    Comment

    • waqasahmed996
      New Member
      • Jun 2008
      • 160

      #3
      then please guide me about syntax

      Comment

      • mwasif
        Recognized Expert Contributor
        • Jul 2006
        • 802

        #4
        [CODE=mysql]
        SELECT CONCAT(fname,' ', IF(mname<>'', CONCAT(mname,' '), '') ,lname) FROM abc
        [/CODE]

        Comment

        • waqasahmed996
          New Member
          • Jun 2008
          • 160

          #5
          thanks a lot........

          Comment

          Working...