mysql query not working if middle text is missing - php

I am trying to search user based on full_name. I have database table with field full_name
Here are the records of table table : users
ID full_name
1 adam bell nithan jhon albrt
2 ali imam khan
If i use query like this then it will work,
select * from users where full_name like '%ali imam%'
select * from users where full_name like '%adam bell%'
But if i search like this it will not work,
select * from users where full_name like '%adam bell nithan albrt%'
select * from users where full_name like '%ali khan%'
I dont know why this happening even we used proper like query. Is there any solution regarding this issue?

You can try this:
select *
from users
where full_name like concat('%', replace('adam bell nithan albrt', ' ', '%'), '%');
It replaces the spaces with wildcards. However, you are still stuck with the ordering of the names.
I think you should look into full text search functions (see here). I think it might be exactly what you need, and the performance is much better than like.

Related

How to search the multiple keywords in multiple columns? [duplicate]

I'm trying to make a search feature that will search multiple columns to find a keyword based match. This query:
SELECT title FROM pages LIKE %$query%;
works only for searching one column, I noticed separating column names with commas results in an error. So is it possible to search multiple columns in mysql?
If it is just for searching then you may be able to use CONCATENATE_WS.
This would allow wild card searching.
There may be performance issues depending on the size of the table.
SELECT *
FROM pages
WHERE CONCAT_WS('', column1, column2, column3) LIKE '%keyword%'
You can use the AND or OR operators, depending on what you want the search to return.
SELECT title FROM pages WHERE my_col LIKE %$param1% AND another_col LIKE %$param2%;
Both clauses have to match for a record to be returned. Alternatively:
SELECT title FROM pages WHERE my_col LIKE %$param1% OR another_col LIKE %$param2%;
If either clause matches then the record will be returned.
For more about what you can do with MySQL SELECT queries, try the documentation.
If your table is MyISAM:
SELECT *
FROM pages
WHERE MATCH(title, content) AGAINST ('keyword' IN BOOLEAN MODE)
This will be much faster if you create a FULLTEXT index on your columns:
CREATE FULLTEXT INDEX fx_pages_title_content ON pages (title, content)
, but will work even without the index.
1)
select *
from employee em
where CONCAT(em.firstname, ' ', em.lastname) like '%parth pa%';
2)
select *
from employee em
where CONCAT_ws('-', em.firstname, em.lastname) like '%parth-pa%';
First is usefull when we have data like : 'firstname lastname'.
e.g
parth patel
parth p
patel parth
Second is usefull when we have data like : 'firstname-lastname'. In it you can also use special characters.
e.g
parth-patel
parth_p
patel#parth
Here is a query which you can use to search for anything in from your database as a search result ,
SELECT * FROM tbl_customer
WHERE CustomerName LIKE '%".$search."%'
OR Address LIKE '%".$search."%'
OR City LIKE '%".$search."%'
OR PostalCode LIKE '%".$search."%'
OR Country LIKE '%".$search."%'
Using this code will help you search in for multiple columns easily
SELECT * FROM persons WHERE (`LastName` LIKE 'r%') OR (`FirstName` LIKE 'a%');
Please try with above query.

SELECT * FROM tbl name WHERE name LIKE 'ask'

Hy, i have a sql tabl name kill, with fields like
ID
name
lname.
There are several names are same, like ali, kiran etc, i want to show all the people with the name ali, so i tried this
SELECT * FROM ask WHERE name LIKE 'ali'
but it shows only the last added ali, please will you tell me the right way to do this. thanks
IF you are trying to find all values containing ali for e.g.
Bali
Alison
etc...
What you need to do is run a wildcard search query, so try this:
SELECT * FROM ask WHERE name LIKE '%ali%'
This will find all values where name contains part of ali in it.
If you want to find all names ending in ali, you can do this:
SELECT * FROM ask WHERE name LIKE '%ali'
If you want to find all names starting with ali, you can do this:
SELECT * FROM ask WHERE name LIKE 'ali%'
etc...
I prefer to use REGEXP, for example:
SELECT * FROM ask WHERE name REGEXP 'ali';
Your query should be as below:-
SELECT * FROM ask WHERE name LIKE '%ali%'
Check this link for detailed info.
SELECT * FROM ask WHERE name LIKE '%ali%';
The Syntax for query in your case is
SQL LIKE Syntax
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
You should use query below to selects all names starting with the letter "ali":
SELECT * FROM ask WHERE name LIKE 'ali%';
You should use query below to selects all names ending with the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali';
You should use query below to selects all names containing the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali%';
Refer This link for tutorials on Like.

Php, Mysql Match in Set multiple values

How can I perform this type of search?
Here is my table:
Name Category
---------------------------------
Company1 food,services
Company2 garden,gifts,services
Company3 other,auto
and here is the query:
$search = $_GET['search'];
"SELECT * FROM companies WHERE category IN ('$search')";
The above search variable looks like mydomain.com?search=garden,services
The idea is to get all relevant rows if any of the matching keywords are present within Category field.
The simplest SQL search is probably a LIKE search:
SELECT * from companies where category LIKE ("%$search%");
You can also do a REGEXP search:
SELECT * from companies where category REGEXP ("$search");
What many have ended up doing, though, is fulltext searching. You can configure MySQL to do this, or perhaps you'll want to try a solution like SphinxSearch.

MySQL Searching forename and surname

OK, I have a DB table that's called players and each player has a forename and surname. Then I have a PHP Ajax search thing that I call to search for players. For example... in the input box, someone types James and there's a row in the table with forename and surname James and Smith respetively.
I do this $check = mysql_query("SELECT * FROMplayersWHEREsurnameLIKE '%$name%' ORforenameLIKE '%$name%' LIMIT 0, 10") or die(mysql_error());
And it returns at least 10 with either forename or surname like the keyword James. However, if I type James Smith, despite it being in the table, I get zero results.
How do I fix this?
Are you using InnoDB or MyISAM? If your using MyISAM, you can create a single field which holds the combined name and then search it using a full text index. So lets imagine you add a new field called combined_names you would search it like this
SELECT * FROM table WHERE match(combined_names) against('John Smith');
This would find any row with either John or Smith in, you can change it to match only those rows with both parts you would add plusses like so:
SELECT * FROM table WHERE match(combined_names) against('+John +Smith');
Here is the documentation on the MySQL site where you can find out more:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
SELECT * FROM players WHERE CONCAT(forename, ' ', surname) = '$name' OR forename LIKE '%$name%' OR surname LIKE '%$name%'
split the name up on spaces so the query runs twice (if there is one space)
the query will run for both names
$nameBits = explode($name," ");
run the query for each piece of $nameBits
surname LIKE '%$nameBits[$i]%'

How to make search engine query in Mysql&PHP?

I want to make a search engine in an intranet. Now i use this clause in PHP.
$k = explode(" ",$_GET[key]);
$sql = "select entreprise.*, employee.* where entreprise.* or employee.* like '%$k[0]%' or '%$k[1]%'";
But it seems doesn't work. Do you know where is wrong?
Thanks in advance.
Edit:
$sql = "select * from entreprise, site, salarie where entreprise.*, site.*, salarie.* like '%$k[0]%' or '%$k[1]%'";
I have modified the query clause. With this code, i think you can know what i want to do.
I want to find anything that matches the content in all the columns of entreprise table and the content in all the columns of employee table.
It's hard to exactly see what you're trying to do, but you need, in your SQL query, to specify :
on which tables you are working, with a from clause
on which fields the search has to be done, in the where clause.
how the data between employees and enterprises are related :
do you want to search for entreprises and their employees ?
for employees and there enterprises ?
for all enterprises and the employees when the employee or the enterprise contains the words ?
You could use something like this to search for entreprises that contain the word, and get their employees to :
select *
from entreprise
inner join employee on employee.id_entreprise = entreprise.id
where entreprise.name like '%word%'
or entreprise.description like '%word%';
Or, to search for employees that match the criteria and get their entreprise too :
select *
from employee
inner join entreprise on entreprise.id = employee.id_entreprise
where employee.name like '%word%';
(just some ideas -- you'll have to build from there !)
This:
$sql = "select entreprise.*, employee.* where entreprise.* or employee.* like '%$k[0]%' or '%$k[1]%'";
is not valid SQL. It is hard to guess what you want to do, but I'm trying anyway: you want to find employees, and search them by name or by enterprise that employs them. Is that the case? Or do you want to search employess and/or enterprises?
EDIT
I want to find anything that matches the content in all the columns of entreprise table and the content in all the columns of employee table.
Ok, first of all you should realize that SQL is probably not the best tool for this job. See the other commenter - his suggestions about sphinx and friends are good. But still, if you really want to:
$sql = '
SELECT e.id, e.name
FROM enterprise e
-- first, look in column1
WHERE e.column1 LIKE '."'%".$k[0]."%'".'
OR e.column1 LIKE '."'%".$k[1]."%'".'
...etc for all entries in k...
OR e.column1 LIKE '."'%".$k[N]."%'".'
-- then, look in column2
OR e.column2 LIKE '."'%".$k[0]."%'".'
OR e.column2 LIKE '."'%".$k[1]."%'".'
...and so on and so forth for all entries in $k and all columns in enterprise...
UNION ALL
SELECT s.id, s.name
FROM salarie s
WHERE ...and the same for columns of salarie...
...
UNION ALL
...any other tables you want to search...
';
As you can see, not something that makes you happy.
Another approach that might give you more joy is having some overnight job to scan all rows in the tables you're interested in, parse the texts you want to search into separate words, and store those in a keyword table, and storing the association between an object from the source database and the keyword in a separate table. You can then search the keyword table and use the id's and table names you find for a collection of keywords to build the actual query to retrieve those rows. This is what I do, and it works great. It works better because there is a relatively small amount of words that you will encounter, whereas the collection of objects is quite possible very large.

Categories