0

I have a query that just isn't providing results:

$name = "John Smith";

$query = "SELECT * FROM my_table WHERE lname LIKE '%$name%' AND fname LIKE '%$name%'";

lname in the database would be listed as Smith fname in the database would POSSIBLY be listed as John S.

Some entries have a middle initial attached to their name, some don't.

Also, names are pulled in as one string on the page where I am querying this information, not first name and last name strings. Limitation of the existing code.

So I'm trying to search for the last name in the string $name as well as the first name. I assume I'm doing it wrong. Is this possible? Or is there another way to do this?

8
  • echo $query; and run it in MySQL directly. Commented Apr 1, 2013 at 19:08
  • 2
    You're searching for the string John Smith in both the lname and fname fields - that's not going to find anything, as the contents are split over both fields. Commented Apr 1, 2013 at 19:08
  • $query = "SELECT * FROM my_table WHERE lname LIKE '%$name%' OR fname LIKE '%$name%'"; Commented Apr 1, 2013 at 19:09
  • What if I have Dan Smith and John Walters though? I need to make sure that the string John Smith, either first or last name appears in both lname and fname. Commented Apr 1, 2013 at 19:14
  • Which version of mysql do you use? Commented Apr 1, 2013 at 19:17

3 Answers 3

1

How about this (if I'm understanding you correctly):

SELECT *
FROM my_table
WHERE '$name' like concat(fname,' %')
  AND '$name' like concat('% ',lname)

SQL Fiddle Demo

To search the fname field, it concats a ' %' after the fullname being searched. And to search the lname field, it concats a '% ' before the fullname.

EDIT: A slight variation of Mario's good comment that uses REGEXP:

SELECT *, CONCAT_WS(' ', fname, lname)
FROM my_table
WHERE  CONCAT_WS(' ', fname, lname) REGEXP REPLACE('$name',' ', '.*')

More Fiddle

Since you mention you cannot break up the parameter (why?), this uses replace to replace the space accordingly.

Sign up to request clarification or add additional context in comments.

6 Comments

I would modify this a bit: select * from foo2 where "john smith" in (fname, lname) or CONCAT_WS(" ", fname, lname) REGEXP "john.*smith";
@MarioMueller -- I like the REGEXP option -- I'll edit the post to show. Thanks!
@MarioMueller -- actually, that would require altering the input of $name and I didn't think OP could do that. If OP can alter input, then I like this option too.
Yes, you're right. This might be no acceptable due the presented immutability of the original value, but I might be worth a try ;)
@MarioMueller -- I updated with a version that should work using SQL to replace the input. Up to OP at this point :) Thanks!
|
0

i have the solution for it.. you can try like this

$name = "John Smith";

$query = "SELECT * FROM my_table WHERE fname LIKE substring_index(".$name.",' ',1) AND fname LIKE substring_index(".$name.",' ',-1)";

Comments

0

I don't understand what is exactly the problem but the next query could help you:

SELECT * FROM my_table WHERE '$name' LIKE CONCAT('%', lname ,'%') and '$name' LIKE         CONCAT('%',fname ,'%')

If you need to check if lname is contained in $name the query you show is incorrectly and you are checking the opposite.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.