Using LIKE to search for a name - php

I have a table which contains contact information. This table has 1 column relevant to names. It looks like this:
Sort Name:
Doe, John
Clinton, Bill
Dooby Doo, Scooby
Sadly, there are no first name / last name columns, and they can't be added.
If a user enters the search term "john doe", is there any way I can get mysql to return the column for "Doe, John" as my result?
Edit: As a follow up to the answer by RHSeeger, what would I do to make
john doe
into
$name(0=>'john', 1=>'doe')
using php
Thanks for the help so far

My thought would be:
SELECT * FROM tablename
WHERE LOWER(sort_name) LIKE '%name1%'
...
AND LOWER(sort_name) LIKE '%nameN%'
where <name1> ... <nameN> are the individual, lowercased "words" (split on space, comma?) in the name the user requested.
Edit: It's worth noting that, for a reasonably large sized data set, searches like this are going to be slow. If you're going to have lots of data, you could consider a search engine (like SOLR) that you use to search for things, and using MySQL just for lookup by ID.

In your application, split the search term at the last space, change the order and add a comma in between.

Look in to the sql "like" operator. For example
select * from TABLENAME
where sort_name like '%doe'
You would need to do a little manipulation to the search terms on the application side and possibly supply other conditional clauses in your sql statement with other like clauses.

I would begin by parsing the search into separable words "john" and "doe" then then write a search using the LIKE functionality for both terms.
EDIT:
To answer your question from a answer below... how do parse "John Doe" into "John" and "Doe"
I would write a MySQL stored procedure to separate them into a result set containing "John" and "Doe" and then use the like versus that column name

have a full-text index on the names column and then do this,
select * from names where match(name) against ('john doe');

Related

Advanced search term in php and MySql

I have a MySql table contain a title field.
Suppose a user enter a term in an input textbox.
Now I want to select rows whose title field has one of the following states:
1) its title is exactly the same as term : "sugar">>"sugar"
2) one of the words of its title is like the term : "pretty flower" >> "rose flower"
3) its title is from the same Word family as that term (term is root word of those) : "biology" >> "biography, biodegradable, symbiotic"
I'm using laravel. if can suggest any solution for that , Would be great
This was an easy solution until you mentioned word family...
However, this is something that is still achievable. It's about how you approach it.
You'll start by using the like condition in MySQL.
You can read more about it on the MySQL Website relating to Pattern Matching
Here's an example of that:
SELECT * FROM my_table WHERE field1 LIKE '%searchTerm%';
In terms of getting results by "word family", you may want to consider adding tags to the result.
Add a tags field in the table and add an array of tags that would relate to the "word family".
Your query would then look something like this:
SELECT * FROM my_table WHERE field1 LIKE '%searchTerm%' OR field2 LIKE '%searchTerm%';
You'll need to loop through the array of tags to find what matches, if at all.
Just my approach to get started.
In terms of how to do this is Laravel, your query may look something like this, according to their documentation
$results = DB::table('table1')
->where('myfield', 'like', '%searchterm%')
->get();
I surely hope this puts you in the right direction.

Querying a table where the field contains any order of given text strings

I want to query a table as follows:
I have a field called "category" and my input match contains N separate words. I want the query to match all rows that contain all N words, but in any order.
For example if the field category contains "hello good morning world", my input query can contain "hello morning" or "good" or "world hello" and all are matches to the query.
How do I formulate such an SQL expression?
Also it would be good if the query can be made case insensitive.
If you are using MySQL you can use the boolean fulltext search feature to achieve this. You can put a + in front of each term and then only results with all the terms, in any order, will be returned. You will need to make sure the column containing the category field has a fulltext index specified on it for this to work. Other database engines probably have similar features. So for example you might do something like the following assuming there were a fulltext index over the category column...
SELECT * FROM myTable WHERE MATCH (category) AGAINST ('+term1 +term2 +term3' IN BOOLEAN MODE);
I would avoid using the "LIKE" operator as others have suggested you would have to worry about the headache of mixed upper/lower case and if you have a large database using a % in the front of a LIKE search term is going to cause a full table scan instead of using an index which is horrible for performance.
I'm not writing the loop that will build this query for you. This will get the job done, but it will be pretty inefficient.
SELECT * FROM table
WHERE (
TOUPPER(category) LIKE '*HELLO*' AND
TOUPPER(category) LIKE '*GOOD*' AND
TOUPPER(category) LIKE '*MORNING*' AND
TOUPPER(category) LIKE '*WORLD*'
);
You could also research using REGEXes with SQL.

Getting rid of functions in the WHERE clause

I have a table called users which contains the columns firstname and lastname.
I am struggling on how to structure the WHERE clause to return results on matched first name, last name, first name plus a space and the last name, and last name plus a comma and the first name. For instance, "John", "Doe", "John Doe", and "Doe, John". It should also work for partial patches (i.e. "John D"). I am thinking of something like the following (substitute ? with the search phrase).
SELECT * FROM people WHERE
firstname LIKE ?
OR CONCAT_WS(" ",firstname,lastname) LIKE ?
OR lastname LIKE ?
OR CONCAT_WS(", ",lastname,firstname) LIKE ?
There is a little flaw with this approach as searching on "John Doe" will also return "John Enders", "John Flaggens", and "John Goodmen", so I will need to add some conditional to just return results when both first and last name match when both are given.
There is also a big flaw with this approach as I have functions in my WHERE clause which prevent the use of indexes and result in significantly reduce performance.
Can I effectively do this using just SQL without using functions in my WHERE clause, or should I user server code (ie PHP) to parse the given search phrase for spaces and commas and create a dynamic SQL query based on the results?
You ask if you can efficiently do this within MySQL, given your schema design without using FULLTEXT search the simple answer is no, you can't do this efficiently.
You could create some wacky query using LIKE / STRCMP and / or string functions. If you were to take this approach it will be much better to write some application logic to create the query inline rather than trying to write one query that can handle everything. Either way it’s not likely be truly efficient
MySQL 5.6 has the ability to perform FULLTEXT searches within INNODB. If you’re using a lower version you can only do this with MyISAM tables – there are many reasons why MyISAM may not be a good idea.
Another approach is to look at a real search solution such as Lucene, Sphinx or Xapian
Have you tried regular expressions http://dev.mysql.com/doc/refman/5.1/en/regexp.html

Search in SQL where string starts with X

Well i currently want to do a search engine with SQL and PHP. I crrently use the following query:
SELECT * FROM info WHERE name LIKE '%$q%' LIMIT 10
But i want to select the info with 'name' that start with $q, not the ones that cointain $q.
Simply remove the first wildcard (%):
SELECT * FROM info WHERE name LIKE 'X%' LIMIT 10
If you are wanting to search fields that have multiple names and you want to search for a name that might be in the middle of the field, then it might be better to use Full Text Search (FTS). For example, if the name field in the OP refers to a full name and could contain "John Doe" and you want to search for "Doe", then FTS is probably going to be better (more accurate and faster) than using a LIKE operator. With FTS, the individual words in text are indexed and so searches can be very fast, plus it allows for more complex searches such as the ability to find two words (or names) in a field that are not adjacent (to pick one example at random).
The specific database vendor is not mentioned in the OP, but if FTS is supported, then it might be a good choice for what you are attempting. Some FTS information for SQL Server and for MySQL is easily found with a search.

How to search for city in a record in MySQL

I have to search user according by city but my problem is that in user table in city field
there are two cities like arizona#losvegas, because in registration user can select two cities.
So how can I search city by city name?
Like if someone searches for all users from arizona...
I have done this by using LIKE in SELECT query but I want some other method to do this.
I'm taking a wild guess here and assuming that the posters problem with like is, that it may also match cities that are contained in other cities (cannot think of such example however). To overcome this, one could use three predicates like this:
WHERE city LIKE 'arizona#%'
OR city like '%#arizona'
OR city like 'arizona'
In my opinion however, combining two cities in one column is bad design in the first place. Why not either make two columns, make a row for each city or make an extra table that links the cites to the users.
Well, the obvious way is using like. I don't know why you wouldn't want to use it:
select * from users where city like '%arizona%';
Maybe you can tell why are you trying to avoid LIKE and someone will try to give you some ideas. If what you need is a better pattern matcher than LIKE you can use REGEXP operator.
I'm a bit confused as to why the database doesn't have multiple city tuples for each user. But, if you want a more powerful way to do searching in MySQL, you can search through text in columns with regular expressions:
SELECT name FROM employees WHERE name RLIKE 'P$'
This matches employee names that end in P since the $ is the end of string anchor. You can also do things like:
SELECT username FROM userTable WHERE city RLIKE '[Mm]assachusett?s'
... if, heaven forbid, people don't know Massachusetts has two 't's or something.
Regular expressions are very powerful so if you want to use this, you can. But to be honest, considering what you're looking for, using LIKE '%arizona%' works perfectly fine.

Categories