MySql plural search without Fulltext - mysql

I want to make a plural search on my table but i don't want to use FULLTEXT.I tried FULLTEXT but my table doesn't support it.My query is like:
SELECT
*
FROM
items
WHERE
LOWER(items.`name`) LIKE '%parameter%'
OR LOWER(items.brand) LIKE '%parameter%'
OR LOWER(items.sku) LIKE '%parameter%'
When i search 'shirt' it returns good results when i search shirts i doesn't.Is there a way to make plural search without fulltext

I suggest you to create separate table items with MyIsam Engine for items
with fields you want to perform search and primary id.
Now you can do full-text search on new table and retrieve ID and based on ID you can retrieve result of fields from main items table.
The additional table for "items" needs to be updated regularly, may be though trigger or automated script.

it will match all those beginning with parameter passed.
SELECT
*
FROM
items
WHERE
LOWER(items.`name`) LIKE 'parameter%'
OR LOWER(items.brand) LIKE 'parameter%'
OR LOWER(items.sku) LIKE 'parameter%'

Related

MySQL/Doctrine - Search in all columns and joined tables

I have a table in MySQL database with about 30 text fields and about 10 joined N-N tables.
My client wants one form input field to search through all the data.
Is there an easy way to do it?
My assumption is that if I do so many joins, the query is going to take ages.
So an idea I had is to create a column called "ALL". After each edit/add action I would dump all the other columns' date into this ALL column and do a search like this:
Select * From Table WHERE all like "%search"
Is it possible to do it like this? Anyone knows the right way to do it?
Thank you, Mike.
Yes, right
Commonly, there is another (distinct) column 'all' that is a tuple of all values of all columns and then you search through that column.
Another option is to add a different database just for a sake of fulltext
https://www.elastic.co/
https://www.algolia.com/

How to search either on id or name for certain purchase orders

We would like to filter purchase orders either based on purchase order id (primary key) or name of the purchase order using a single search box.
We used the like parameter to search on the name field, but it doesn't seem to work on the primary key. It works only when we use the equal operator for id(s). But it would be preferable if we can filter purchase orders using like for id(s). How to do this?
create table purchase_orders (
id int(11) primary key,
name varchar(255),
...
)
Option 1
SELECT *
FROM purchase_orders
WHERE id LIKE '%123%'; -- tribute to TemporaryNickName
This is horrible, performance-wise :)
Option 2a
Add a text column which receives a string version of id. Maybe add some triggers to populate it automatically.
Option 2b
Change the type of id column to CHAR or VARCHAR (I believe CHAR should be preferred for a primary key).
In both 2a. and 2b. cases, add an index (maybe a FULLTEXT one) to this column.
I think LIKE should work. I assume that your SQL wasn't correctly written.
Let's assume that you have order name "ABCDEF" then you can find this using the following query structure.
SELECT id FROM purchase_orders WHERE name LIKE '%CD%';
To explain it, % sign means it's a wildcard. As a result this query is going to select any String that contains "CD" inside of it.
According to the table structure, varchar can contain 255 characters. I think this is quite a large string and it's probably going to consume a lot of resources and going to take more time to search something using SQL functions like LIKE. You can always search it by id
WHERE id = something. This is much faster way btw
, but I don't think order id is an user friendly data, instead I would let users to use product name. My recommendation is to use apache Lucene or MySQL's full text search feature (which can improve search performance).
Apache lucene
MySQL Full text search function
These are tools built to search certain pattern or word through list of large strings in much faster way. Many websites use this to build their own mini search engines. I found mysql full text search function requires pretty much no learning curve and straight forward to use =D

How to search a word in MySQL table? MATCH AGAINST or LIKE?

I have a table for some companies that may have many branches in different countries. These countries are inserted in countries field.
Now, I have to make a searching system that allows users to find companies that have any branch in a specific country.
My question is: Which one do I have to use ? MATCH AGAINST or LIKE ? The query must search all records to find complete matched items.
attention: Records may have different country name separated with a comma.
MATCH AGAINST clause is used in Full Text Search.
for this you need to create a full text index on search column countries.
full text index search is much faster than LIKE '%country%' serach.
I would change the implementation: having a field that contains multiple values is a bad idea, for example, it's difficult to maintain - how will you implement remove a country from a company ?.
I believe that a better approach would be to have a separate table companies_countries which will have two columns: company_id and country_id, and could have multiple lines per company.
You should use LIKE . Because as #Omesh mentioned MATCH AGAINST clause is used for Full Text Search.. And Full Text Search need entire column for search.

MySQL MATCH for more than one field

SELECT * FROM portfolio
INNER JOIN translation
ON portfolio.description = translation.key
WHERE
MATCH(it_translation.*) AGAINST('test')
Why this code doesn't work?
If I do like this MATCH(it_translation.field) AGAINST('test') everything is ok, but I wanna search FULLTEXT via more than one field, and I don't know how many fields in table.
IIRC for FULLTEXT to work you need a FULLTEXT index that covers every field you want to use it for, so if you "don't know how many fields in table" you won't be able to MATCH it like that.

Search page engine PHP with MYSQL database?

I'm going to generalize this question so that other people can use the answers.
Let's say I have a website driven by a MYSQL database.
Database contains 5 tables:events,news,books,articles,tips.
Every table has among others 2 fields Title and Details in which I want to search
On every page of the site I have a search form (text field and button).
After I type a word or phrase I want to be redirected to a page called search where I should see the results as a list with links from the entire database.
e.g.
Book X (link on it to the book found in the database)
Event Y
Article Z
HELP: The tables are INNODB ENGINE so full text search didn't work also I'm having trouble in building a SELECT statement for searching multiple fields from multiple tables with LIKE. I've succeded with one table but with multiple tables and multiple fields I'm getting error or no data or duplicated data in some cases. Some help with this Select statement please.
Question: How do I build a search engine for all the tables in my MYSQL DB? Some SQL injection or other hacking prevention advice would be appreciated also.
My Approach to the situation is create a view based on all the tables with similar columns ( columns which we need to search only) and one more alias column with their table names/ entity name (Books, event etc)
It should look like this
EntityName Title Details
Books xxxx xxxxx
...............................
I am not explaining how to create views with union (dont use Union All if not expecting duplicates).
The next stop would be search using like statements
select * from vwSearchData where Title like '%keyword' or details like '%keyword'
Next step is to display the data along with their entity names.
Ofcourse, you need to get the keyword by filtering with html entities from the search form.
You can use UNION:
(SELECT * FROM events WHERE title LIKE '$key' OR details LIKE '$key')
UNION
(SELECT * FROM news WHERE title LIKE '$key' OR details LIKE '$key')
and so on.