Query order results according to search results - php

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.

Related

Internal site search is matching exact word only

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

ordering sql select results

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.

SQL Query: Get String with highest number | Parse?

I have a database which looks like this:
Do not ask my why, but this is written by a WordPress plugin so I have to handle with it. What I want is to get every link where the meta_key = product_shops_X_link. But every post has a variable amount of product shop links.
In this picture I tried Where meta_key Like "%_link%". But if I do a query like that I also get all meta_keys where the product_shops_X_link field has a _ before the product...
What I want is just all product_shops_X_link fields. Now there a two ways I think.
Way 1: Get the highest number of the meta key fields Where meta_key Like "%product_shops_X_link%" OR meta_key Like "%_product_shops_X_link%". It doesn't matter of _product or just product. The numbers are equal. If there is just a product_shops_0_link field there must be also a _product_shops_0_link field.
Way2: Create a pattern which only searches for all product_shops_X_link fields without the beginning of _product and give me ALL product_shops_X_link fields. Whatever number is contained in the field.
But in both cases I do not know how to code that..
Does anyone knows how to code that or is there maybe a better way?
Greetings and Thank You!
If you don't want entries starting with _product then you can exclude _ or % from your LIKE pattern, e.g.:
SELECT *
FROM links
WHERE meta_key LIKE 'product_%';
But how can I add some more attributes to the query like post_id or
meta_key
Those can be combined with AND, for example to query items with post_id = 21179 and meta_key starts with product_..:
SELECT myFields
FROM myTable
WHERE (post_id = 21179) AND (meta_key LIKE 'product_shops_%')
Sorry if I am misunderstanding but are you trying to capture the number from those rows? If so, you can use the native REPLACE() function like this:
SELECT *, REPLACE(REPLACE(meta_key, 'product_shops_', ''),'_link','') as captured_number
FROM mytable
WHERE meta_key LIKE 'product\_shops\_%\_link'
ORDER BY captured_number DESC
This will get you the rows you want with the highest number first. Alternately you can use this regex library for mysql and then use regex to capture the number:
SELECT *, PREG_CAPTURE( '/product_shops_([0-9]+)/', meta_key) as captured_number
FROM mytable
WHERE meta_key LIKE 'product\_shops\_%\_link'
ORDER BY captured_number DESC

How to use like query with multiple column

We are filtering our record based on two columns group_is and enumeration. We have total 8 groups and 12 enumerations. while processing a filtration for record as group_is like Group 1 and enumeration like Enumeration 1. we get the result which contains all record with group_is like Group 1 and Enumeration like Enumeration 1 as well as enumeration like Enumeration 10,Enumeration 11 and Enumeration 12 also as we have used like in our query.
We have used following query in order to get result.
$query = "SELECT * FROM geodata WHERE group_is like 'Group 1%'
and enumeration like 'Enumeration 1%'";
as we have many other filtration with group and enumerations like batch,site and many other that's why it is important to use like query while processing.
how we can alter query so that we will get result which are only there in Group 1 and Enumeration 1
What we have tried?
we try change our query little bit by changing, instead of using enumeration like 'Enumeration 1%' , we used enumeration='Enumeration 1' . but it doesn't help us. as by changing this we unable to get filtration result for other filter fields.
Did you try using enumeration in ('Enumeration 1') ?

How to select rows from a table where a word appears? (MySQL + PHP)

I have to make a search for keywords as part of my Computer Science work.
I have the names and descriptions of several DVD's.
The user has to search a word, and then displayed are all the names of DVD's where the word appeared in either the title or description.
Let's say my columns in my table
were "dvd title" and "description", and the word the person has entered is $keyword.
How would i select rows in mysql where $keyword appears at least once in either columns "dvd title" and "description".
Thanks for reading. Any help appreciated.
You could create a full text index on those columns, but that probably isn't what they want you do do.
You need wildcards, and to you wildcards compare with the keyword LIKE instead of =. A wildcard in mysql is %
SELECT * FROM mutable WHERE dvdtitle like '%keyword%' or description like '%keyword%';
As for using PHP variable and creating the string, you've got to do some of your own homework.
$sql = "SELECT * FROM table_name WHERE dvdtitle LIKE '%".$keyword."%' OR description LIKE '%".$keyword."%'";
Executing the above SQL query would return all the rows in the table that has the specified keyword in either the column dvdtitle or description.
Use this query:
SELECT *
FROM table_name
WHERE dvd_title like '%$keyword%'
OR description like '%$keyword%'
You can use following code sample, it's not a full code but will give you an idea:
$query_str = "select * from dvd_table_name where dvd_title like '%$keyword%' or description like '%$keyword%'";
$qh = mysql_query($query_str);
Then use mysql_fetch_assoc($qh) to retrive data using while loop;

Categories