I have a system that searches for company. I want that when a user searches for \"Demo\", all records that have \"Demo\" will be returned, like \"The Demo\", \"Demo Inc.\",
I dont see why 1M is a problem I just tested on my laptop MySQL MyISAM that has also a company but it is 250K rows and it took 3.3 ms , and the field is not indexed. can you try folowing
$search='Demo';
$regex="/\b$search\b/i";
$sql = "select * from table where company like '%$search%';
//... get the results
foreach($results as $companyName){
if(preg_match($regex,$companyName,$match)){
//here you got a match
}
}
SELECT *
FROM table_name
WHERE company LIKE "% Demo %"
OR company LIKE "Demo %"
OR company="Demo";
Create an Full text index, and then you can search more easy.
ALTER TABLE table ADD FULLTEXT INDEX fulltext_index;
SELECT * FROM table WHERE MATCH (company) AGAINST ('+Demo' IN BOOLEAN MODE);
dev.mysql.com/doc/refman/5.6/en/fulltext-search.html
You can use REGEXP and the [[:<:]]
and [[:>:]]
word boundary markers:
SELECT
*
FROM
`table`
WHERE
company REGEXP '[[:<:]]Demo[[:>:]]';
Another Solution
SELECT
*
FROM
`table`
WHERE
company REGEXP '(^|[[:space:]])Demo([[:space:]]|$)';
SQL Fiddle Demo
Try this it may help ya..
SELECT * FROM table_name WHERE company LIKE "%Demo%";
A shot in the dark as per my comment. If you're always going to get an exact match criteria. would it not be best to perform a standard select query?
SELECT * FROM table WHERE company='The Demo'
Or for practical:
$Search = $_GET['company'];
SELECT * FROM table WHERE company='$Search'
Obviously use best practices when working with user input & Queries.
The results drawn will be either the rows found to have The Demo, the demo would be returned or nothing.
If you do not always have an exact match. You could again, use $_GET with an appended value ie $_GET['Exact'] & have two different functions:
function ExactMatch ($DB,$Company){
/*
Query to get exact match as exampled
*/
}
function NotExact($DB,$Company){
/*
Query using LIKE syntax
*/
}
and validate:
if (isset($_GET['Exact'])){
if ($_GET['Exact'] === 1){
ExactMatch($DB,$_GET['Company']);
}else{
NotExact($DB,$_GET['Company'])
}
}
Also, a possible read on DBA.stackexchange:
https://dba.stackexchange.com/questions/39693/how-to-speed-up-queries-on-a-large-220-million-rows-table-9-gig-data