Mysql find row with longest field value as beginning of a text - mysql

I have something like this in a mysql table.
ID Text
1 a
2 b
3 a-b
4 b-a
5 a-b-c
6 a-b-d
Usually you search a text inside a column, i just want the opposite, i want a query to search all columns inside a text. I dont know if it is possible.
if text is "a-b-f" It must return query with ID 3 ("a-b")
if text is "a-c" It must return query with ID 1 ("a")
if text is "b-a" It must return query with ID 2 ("b")
if text is "b-b" It must return query with ID 2 ("b")
if text is "a-b-c-d-e-f" It must return query with ID 5 ("a-b-c")
if text is "c-a-a" It must return an empty query
Thanks for the help.
PD: I was looking for something like this but all ways I found was search a text inside a column value and it is the opposite way as I said.

Could be using like
select ID from my_table
where 'your_text_seach' like concat ( text, '%')
order by length(text) desc
limit 1

Related

MySQL strip ' on where queries

I have a large table which contains, or not, records that have ' tags like (martin's, lay's, martins, lays, so on).
Actually to search the client can be write exactly text, for example: martin's, to search all records that contains "martin's" but it is complicate, then, I need the client can to search by "martins" or "martin's".
This is a simple example:
A mysql table like:
ID | Title
---------------
1 lays
2 lay's
3 some text
4 other text
5 martin's
I need a sql query to search by lays or lay's and both need show me a Result like:
ID | Title
---------------
1 lays
2 lay's
I'm tried with many post solutions but I cant do that :-(
Appreciate any help.
Just remove the single quote:
select t.*
from t
where replace(t.title, '''', '') = 'lays';
To search if the word contains:
select t.*
from t
where replace(t.title, '''', '') LIKE '%lays%';

List all value in a column which is having 5 consecutive number, it can have any other characters as well before or after the sequence

i need only those results which is having at least one set of 5 consecutive digits in it.
This was my original query
SELECT [Field]
FROM [testPackage].[dbo].[Table_1]
where Field like '%[0-9]{5}%'
Below is the table
Field
---------------------
fhjsfh4324kjkjk
45654rewrwejug
g,nerht54535fjklrejltkj
fhdjfhjh425435
hjlwrjtljr424556fslfj
kljrkj67587598347rerjwlej
esd980rewrkw456
the query should list only
45654rewrwejug485345
g,nerht54535fjklrejltkj
Just continue in the direction you were already heading, and repeat [0-9] five times in sequence inside the LIKE expression:
SELECT *
FROM [testPackage].[dbo].[Table_1]
WHERE Field LIKE '[0-9][0-9][0-9][0-9][0-9][a-zA-Z,]' OR
Field LIKE '[a-zA-Z,][0-9][0-9][0-9][0-9][0-9][a-zA-Z,]' OR
Field LIKE '[a-zA-Z,][0-9][0-9][0-9][0-9][0-9]' AND
Field NOT LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%'

Get rows that contain less than 4 digits only

I need to fetch the rows that contain less than 4 consecutive digits. I tried using this :
select mycolumn where mycolumn REGEXP '[0-9]{1,3}'
but this still returned rows such as
"Test Text 1234-1234"
I need to return these only:
Test 12
Test 234 Text
Test /2 Text
text 123 text 1234 text
Thanks to #BenM and #lad2025, the correct answer is:
'[[:<:]][0-9]{1,3}[[:>:]]'
http://sqlfiddle.com/#!9/af95e/3/0

Mysql searching multiple words using wildcard like and ordering by count of words matched

I have an innodb table 'not supporting fulltext' that i search using like statments
Select text,id from searchTable where text like '%sub1%' or text like '%sub2%' or text like '%sub3%'
group by text,
order by (CASE
when text LIKE 'sub1 %' then 1
when text LIKE 'sub1%' then 2
when text LIKE '% sub1%' then 3
when text LIKE '%sub1%' then 4
else 5
end),id
this returns results more or less as expected, yet i was wondering if i can also order it by the count of substrings that matched. for example order rows that has all 3 substrings first, followed by rows that matchs 2 out of 3 substrings, etc..
is it possible ? would it impact performance that much ?
my table contains around 50k rows, this current statement takes around 0.6ms to execute.
thanks
You can order by the number of matching substrings by doing:
order by ((test like '%sub1%') +
(text like '%sub2%') +
(text like '%sub3%')) desc
In an integer context, MySQL treats boolean values as integers, with 1 for true and 0 for false. So this counts the number of matches.

Having a number in

Can someone give me a query that will return as a result rows ID 1 & 3?
ID Name Hidden
1 Mika 1,4,2
2 Loca 0
3 Nosta 4
4 Like 2
Something like this
SELECT * FROM table WHERE Hidden HAVING(4)
SELECT * FROM table WHERE FIND_IN_SET('4',Hidden);
docs for FIND_IN_SET
SELECT * FROM table WHERE CONCAT(',',Hidden,',') LIKE '%,4,%'
or you can avoid using LIKE like this
SELECT * FROM table WHERE INSTR(CONCAT(',',Hidden,','), ',4,') > 0
this will not get things like 40, 14, etc, but you need to make sure there are no spaces in the Hidden field (eg, 1, 4, 5 or update the concat and LIKE function accordingly.
SELECT * FROM table WHERE Hidden LIKE '%4%'
the % are wildcards.
Full Text Search might be a reasonable solution for this as long as you use the correct word breaks.
Either go with Full Text Search, as suggested, or
Spin the Hidden values off into a separate table, with the ID of current row.
Eg, Mika would have three entries in this table
ID = 1, Hidden =1
ID = 1, Hidden =4
ID = 1, Hidden =2
Then you could return results against this spin off table.
You may also want to consider normalizing the table and storing these "hidden" values in a separate table with an index on the apropriate column. Depending on the number of rows you have that would be much faster:
ID Hidden
1 1
1 4
1 2
3 4
4 2
and:
SELECT DISTINCT table.* FROM table, hidden_table WHERE table.ID = hidden_table.ID AND hidden_table.hidden = 4