Jump to content

Querying table as if two columns were one.


shergar1983

Recommended Posts

I have recently implemented a 'Google Suggests' type drop-down box on my site, and developing this has thrown up an issue.

 

I have a table with three columns:

 

id (auto_inc) int 10

first_name - varchar 80

second_name - varchar 80

 

Now lets say there is a name John Smith in the table with John in the first name field and Smith in the second.

 

I need to be able to see whether the search string is like either two columns combined so if user enters 'John Sm' then John Smith is returned. The results returned also have to be distinct.

 

I've played around with concat ideas etc. However if anyone can help I'd be very grateful.]

Many thanks,

Link to comment
Share on other sites

I would probably say it's safe to assume that they'll not be entering partial names, i.e. for John we won't enter "oh", thus you can alter your query to be partially more efficient as follows by removing the starting %

 

$sql = "SELECT * FROM tbl WHERE first_name LIKE '{$searcharr[0]}%' && second_name LIKE '{$searcharr[1]}%'";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.