Searching by name both in the first name and last name from the database.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhepsi
    New Member
    • Apr 2007
    • 111

    Searching by name both in the first name and last name from the database.

    hii all,
    i have aproject where...

    wen i enter a text in the textbox..., accordingly it should search in teh first name and last name of the database and display the records

    How to search records by name....???


    plz some one help me the query...


    thnx..
    hepsi..
  • henryrhenryr
    New Member
    • Jun 2007
    • 103

    #2
    I think you could try a number of approaches. Two I can think of are:

    1. Concatenate the two columns:
    [code=sql]
    SELECT CONCAT(first_na me,' ',last_name) AS name FROM table WHERE CONCAT(first_na me,' ',last_name) LIKE 'name_entered_i n_text_box';
    [/code]

    NB - if you want exact matches don't use LIKE, just use =. If you want more results, put some wildcards (%) in your LIKE clause (ie LIKE '%henry%').

    2. Search each column for the same term:
    [code=sql]
    SELECT first_name, last_name FROM table WHERE first_name LIKE '%henry%' OR last_name LIKE '%henry%';
    [/code]
    This will give lots of results as used % before and after and I used OR. Cut the results by using fewer '%' and using AND instead of OR.

    Hope that sheds some light on your problem! Good luck!

    Henry

    Comment

    • rhepsi
      New Member
      • Apr 2007
      • 111

      #3
      Originally posted by henryrhenryr
      I think you could try a number of approaches. Two I can think of are:

      1. Concatenate the two columns:
      [code=sql]
      SELECT CONCAT(first_na me,' ',last_name) AS name FROM table WHERE CONCAT(first_na me,' ',last_name) LIKE 'name_entered_i n_text_box';
      [/code]

      NB - if you want exact matches don't use LIKE, just use =. If you want more results, put some wildcards (%) in your LIKE clause (ie LIKE '%henry%').

      2. Search each column for the same term:
      [code=sql]
      SELECT first_name, last_name FROM table WHERE first_name LIKE '%henry%' OR last_name LIKE '%henry%';
      [/code]
      This will give lots of results as used % before and after and I used OR. Cut the results by using fewer '%' and using AND instead of OR.

      Hope that sheds some light on your problem! Good luck!

      Henry

      thnx for ur reply...

      Comment

      • wish
        New Member
        • May 2007
        • 65

        #4
        Hi,

        If my problem is user input one keyword then i wan to find all the columns in the tables or all the tables in the database?
        Eg. table_employee, table_customer, table_product. Each tables got a lot of columns then i wan to find out the similar with the keyword record.

        Can someone guide me?

        Thanks

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by wish
          Hi,

          If my problem is user input one keyword then i wan to find all the columns in the tables or all the tables in the database?
          Eg. table_employee, table_customer, table_product. Each tables got a lot of columns then i wan to find out the similar with the keyword record.

          Can someone guide me?

          Thanks
          Google about how to use LIKE with wildcards.

          Comment

          Working...