i am selecting data from sql by this command:
the command can contain more then one words like
select * from table where title like '%deadpool%' ////1
or
select * from table where title like '%deadpool%' && like '%world%' ////2
so this show results like :
*****************1*************
thenewdeadpoolishere
deadpool
*****************2*************
.deadpool.world.
deadpool world
now, i want to order the list as follows :
*****************1*************
deadpool
thenewdeadpoolishere or whatever
*****************2*************
deadpool world
cdsghjvjdhgdeadpoolvworlddsbvs or whatever
the thing is i want to order it somehow that if the whole word of searched term is present in the result then it should show up first.
I suspect you are looking for:
select t.*
from table t
where title like '%deadpool%' and
title like '%world%'
order by ((title like '%deadpool%') + (title like '%world%')) desc;
However, it is quite likely that you really want full text search, with order by relevancy. You can check the documentation to see if this is what you really want.
Related
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.
I am making an eCommerce website I am using PHP and Mysql and I have some products name in table named product. How can I make a search system by which if I type "Wallnuts", it should return all results having word "wallnut, walnut, walnuts" and word with same pronunciation like "valnuts" and sorted as best matching result first. For this I am trying this by this query :
select product_name,photo,in_stock,sell_price
from $tbl_product
where product_name like '%".$q."%'
ORDER BY (CASE WHEN product_name LIKE '".$q."%' THEN 1
WHEN product_name LIKE '%".$q."%' THEN 2 ELSE 3
END) limit 0,10
Where $q is search string. By this query I am getting result but with only exact word match.
I need result like on Bigbasket.com for word wallnuts, wallnut, walnts, valnuts.
The closest you can try is SOUNDEX() function in MYSQL.
select * from mytext
where soundex(val) LIKE CONCAT('%',SOUNDEX('wallnut'),'%')
This gives me wallnuts, wallnut, walnts.Check Demo here
I am creating a search function and take an input parameter and query my database using the following query:
select * from people where title like '{param}%' or title like '%{param}%';
my question is that this returns all the records as they are meant to but I want to order the results so that the results of the where statement 'title like '{param}%'' return first then the following where statement.
Is there any way to do this?
Try that :
select * from people where title like '{param}%' or title like '%{param}%'
ORDER BY CASE when title like '{param}%' then 0
when title like '%{param}%' then 1 end asc
I would write this as:
select *
from people
where title like '{param}%' or title like '%{param}%'
order by (title like '{param}%') + (title like '%{param}%') desc;
MySQL treats boolean results as integers so you can add them together. Clearly, if something starts with {param}, then it also contains it, so those will have a value of 2.
In my MSSQL table I have two fields, first one is post_name (position at job) and another filed is org_name (names or location of organizations). I'm writting a PHP script to seach through those two fields. I use Select2 Bootstrap plugin which is basically a seach line with dropdown options fetched from the database based on what user is typing in.
A user usually search for full job title including post_name and org_name. Let's say "Chief Sales Manager Toronto" where first 3 words are from first field and the last word is from second field. When a user start typing Chief Sales Man..." he should be alble to get the whole list of such managers in Toronto, Cupertino or whatever.
The SELECT query I use for this is:
SELECT post_name, org_name
FROM table
WHERE post_name LIKE 'searchTerm%';
That gives me the needed job titles only if I didn't start typing the org name as obviously then it tries to find the whole sentence in post_name filed and gives an empty string.
I also tried:
SELECT post_name, org_name
FROM table
WHERE post_name LIKE 'searchTerm%' OR org_name LIKE '%searchTerm%';
or I was trying to split the searchTerm and try to seach in org_name field by using end of searchTerm:
SELECT post_name, org_name
FROM table
WHERE post_name LIKE 'searchTerm%'
AND org_name LIKE '%.substr(searchTerm, -6).%';
But the last ones are even more pathetic. I was thinking of using CONCAT to unite those two fields so I can search in those two as one, something like
SELECT CONCAT(post_name, org_name) as full_title
FROM table
WHERE full_title LIKE 'searchTerm%';
But in MSSQL Server Express 2005 I get an error of non existing function.
Is there any way I could search such a one sentence in those two fields simultaniously?
The query you want in SQL Server seems to be:
SELECT post_name, org_name
FROM table
WHERE post_name + org_name LIKE 'searchTerm%';
You might want to include a space between the columns as well.
That said, you might want to look into full text search capabilities. It might be a better solution to your problem.
Your last query would work like this
SELECT post_name, org_name
FROM (
SELECT post_name, org_name,
CONCAT(post_name,' ', org_name) as full_title
FROM table
) z
WHERE full_title LIKE 'searchTerm%';
I have a table that contains 3 text fields, and an ID one.
The table exists solely to get collection of ID's of posts based on relevance of a user search.
Problem is I lack the Einsteinian intellect necessary to warp the SQL continuum to get the desired results -
SELECT `id` FROM `wp_ss_images` WHERE `keywords` LIKE '%cute%' OR `title` LIKE '%cute%' OR `content` LIKE '%cute%'
Is this really enough to get a relevant-to-least-relevant list, or is there a better way?
Minding of course databases could be up to 20k rows, I want to keep it efficient.
Here is an update - I've gone the fulltext route -
EXAMPLE:
SELECT `id` FROM `wp_ss_images` WHERE MATCH (`keywords`,`title`,`content`) AGAINST ('+cute +dog' IN BOOLEAN MODE);
However it seems to be just grabbing all entries with any of the words. How can I refine this to show relevance by occurances?
To get a list of results based on the relevance of the number of occurrences of keywords in each field (meaning cute appears in all three fields first, then in 2 of the fields, etc.), you could do something like this:
SELECT id
FROM (
SELECT id,
(keywords LIKE '%cute%') + (title LIKE '%cute%') + (content LIKE '%cute%') total
FROM wp_ss_images
) t
WHERE total > 0
ORDER BY total DESC
SQL Fiddle Demo
You could concatenate the fields which will be better than searching them individually
SELECT `id` FROM `wp_ss_images` WHERE CONCAT(`keywords`,`title`,`content`) LIKE '%cute%'
This doesn't help with the 'greatest to least' part of your question though.