mysql search weight to result - mysql

Database entries
table test
name
========================
Sunae Kasimov
Obsop Natty
Preem Kuman
Mysql query
select *
from test
where name like 'na%' or
name like '% na%' or
name like '%na%';
return in order
1 and then 2
but i wanted in order 2 and 1. That is want to give more weight to '% na%' then '%na%'
Any way this can be done in mysql?

select * from
(
select * from name where name like '% na%'
union
select * from name where name like 'na%'
union
select * from name where name like '%na%'
) a

You should create virtual filed in your select and order by it. For example:
select test.*,
case when name like '% na%'
then 1
else 2
end as OrderField
from test
where name like 'na%' or
name like '% na%' or
name like '%na%'
order by OrderField

Related

MySQL find pairs in the same column

I have a table that looks something like this:
id
id_dlry
0
12345.67
1
12345-68
2
12345
3
12345-67
I need to find pairs in column id_dlry where id's numbers are the same, but the sign changes from '.' to '-'
I added another temporary column to the table, where I can identify the sign like this:
update products set check_sign =
(case
when id_dlry REGEXP '[0-9]{5}[.][A-Z0-9]+' THEN '1'
when id_dlry REGEXP '[0-9]{5}[-][A-Z0-9]+' THEN '2'
else '0'
end);
so it looks like this:
id
id_dlry
check_sign
0
12345.67
1
1
12345-68
2
2
12345
0
3
12345-67
2
Now I would like to create another table only with pairs of records like "12345.67" and "12345-67", so it might look like this:
id
id_dlry
check_sign
0
12345.67
1
3
12345-67
2
I tried using inner join but I got stuck.
You don't need the new column. Use count(*) over(partition by replace(id_dlry, '.', '-')):
select id, id_dlry
from
(select id,
id_dlry,
count(*) over(partition by replace(id_dlry, '.', '-')) cnt
from products) t
where cnt > 1;
Fiddle

how to write a query for scenario?

how to write a single query for my scenario?
I have three where conditions ?
select * (select * ,'sony' as searchItem from dg_deal_product where name like '%sony%' or
select * , 'samsung' as searchItem from where dg_deal_product where name like '%samsung%' or
select * , 'wip' as searchItem from where dg_deal_product where name like '%wip%') order by column desc,column2 desc,column3;
Just guessing here, but perhaps you are looking for something like this:
SELECT p.*
,CASE WHEN p.name LIKE '%sony%' THEN 'sony'
WHEN p.name LIKE '%samsung%' THEN 'samsung'
WHEN p.name LIKE '%wip%' THEN 'wip'
ELSE NULL
END AS searchItem
FROM dg_deal_product p
ORDER BY 1 DESC, 2 DESC, 3

Change an ID with a String on SQL Query view

I have an SQL query to retrieve a few columns of a table and export them on a CSV file. However, one column use an ID (integer number). Is there any way to include something in SQL query and replace the IDs with specific strings?
For example:
If ID = 1 then replace it with "Dog"
If ID = 2 then replace it with "Cat"
If ID = 3 then replace it with "Apple"
I have only four IDs so, it will not be huge.
Here is what I have done until now to retrieve the columns I want
SELECT col_id, check_in, start_hour, price_total, email FROM reservations LIMIT 10
SELECT case when col_id = 1 then 'Dog'
when col_id = 2 then 'Cat'
when col_id = 3 then 'Apple'
end as id,
check_in, start_hour, price_total, email
FROM reservations
LIMIT 10
You could use case/when/then like this:
select case when ID = 1 then 'one' else 'two' end from reservations

how to join fetch data from two table with using (like, limit and order by)

I have two table at same database live_search and php_test .
live_search having 4 columns
id | name| email | date |
"(some data in name column)"
php_test having 2 columns
id | name |
"(some data in name column)"
I want to fetch only name value from both table and with use of like , order by and limits.
I m using this query:
SELECT DISTINCT name
FROM live_search
UNION ALL
SELECT DISTINCT name
FROM php_test
WHERE name LIKE '%$q%' order by name ASC LIMIT 10"
Please help me if it is possible
Make it like this instead
SELECT name from
(
SELECT name FROM live_search
UNION
SELECT name FROM php_test
) tab
WHERE name LIKE '%$q%' order by name ASC LIMIT 10

Two LIKE conditions but retrieving from one first then the other

SELECT *
FROM address
WHERE name LIKE 'a%' OR name LIKE '% a%' LIMIT 10
This query retrieves names that start with a either at the beginning 'a%'
or in a word in the middle '% a%'. How can I retrieve results from LIKE 'a%' first
then LIKE '% a%'?.
add ORDER BY clause,
SELECT *
FROM address
WHERE name LIKE 'a%' OR name LIKE '% a%'
ORDER BY CASE WHEN name LIKE 'a%' THEN 0 ELSE 1 END
LIMIT 10
Here it is:
SELECT t1.*
FROM (
SELECT *
FROM address
WHERE name LIKE 'a%'
LIMIT 10
) t1
WHERE t1.name LIKE '% a%'
A union query may be in order here. Per MySQL documentation
To cause rows in a UNION result to consist of the sets of rows retrieved by each SELECT one after the other, select an additional column in each SELECT to use as a sort column and add an ORDER BY following the last SELECT:
(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;
So for your case something like
(Select *, 1 as sortcol from addresses where name like 'a%')
Union
(Select *, 2 as sortcol from addresses where name like '% a%')
Order by sortcol
Link: http://dev.mysql.com/doc/refman/5.0/en/union.html
One approach is to add an ORDER BY clause to your query:
ORDER BY IF(name LIKE 'a%',1,2)
Something like this:
SELECT *
FROM address
WHERE name LIKE 'a%' OR name LIKE '% a%'
ORDER BY IF(name LIKE 'a%',1,2)
LIMIT 10
To avoid a "Using filesort" operation on large sets (i.e. LOTS of rows in address), and if you only want to return 10 rows, a more complex looking query will likely perform better, by limiting the number of rows to be sorted:
SELECT c.*
FROM ( SELECT a.*
FROM (
SELECT *
FROM address
WHERE name LIKE 'a%'
LIMIT 10
) a
UNION ALL
SELECT b.*
FROM address b
WHERE b.name LIKE '% a%' AND b.name NOT LIKE 'a%'
LIMIT 10
) c
ORDER BY c.name LIKE 'a%' DESC
LIMIT 10