A table, named strings, contains various strings, and unique id's:
Question is, how to write a query that takes an input string, and return the id of the longest matching (sub) string in the strings table?
The matching string, may, or may not be a substring of the input string. Comparison starts at string index 0.
Inputs and expected output:
INPUT -> OUTPUT
ABC -> 1
ABCD -> 2
ABCKK -> 1
ABCDDD -> 2
DAB -> NULL
CDE -> NULL
Doing this:
SET #theString = 'ABCBBB';
SELECT id FROM strings WHERE a_string LIKE #theString;
only returns the correct result when the input string exactly matches a string in the table, and don't work when the 'a_string' is a substring.
You can use:
select s.*
from strings s
where #theString regexp a_string
order by length(a_string) desc
limit 1;
In regards of using LIKE, you need to set the wildcards for it to work as the filter you want. If you are not required to set a variable, you can use the following query.
SELECT id FROM strings
WHERE a_string LIKE '%ABC%'
ORDER BY length(a_string) DESC LIMIT 1;
or if you need a variable, it can be done with the CONCAT function
SELECT id FROM strings
WHERE a_string LIKE CONCAT('%',#theString,'%')
ORDER BY length(a_string) DESC LIMIT 1;
This just is an alternative to #Gordon Linoff's answer.
Related
I have the following Varchar data in a column in my MYSQL table:
Blank_Person_ID_776
Person_999
I want to extract the final number after the underscore to a variable (in this case 776) in order to use it in a query. I.e. ignore any underscore but the last one.
How can I do so?
I would like my final query to be as follows:
SET #personId= //query to get id;
Update Person set tracking_id = #personId where tracking_id is null;
If you want the final value after the last '_', use substring_index():
select substring_index(<whatever>, '_', -1)
If you specifically want the final number in the string, even when there are characters after:
select regexp_replace(<whatever>, '.*_([0-9]+)[^0-9]*$', '$1')
I want to search in my database "emp" for names which have the alphabet "A" as third alphabet from left in their names.
This is what I tried:
select empname from emp
where left(empname,3)="a"
order by empname;
It returns an empty set.
Can you check my syntax, whether it is correct or not!
(Upvote promised ;) )
left(empname,3) will return the three leftmost characters in the string.
You need substring(empname,3,1), which will start at the third character and return a string of length 1.
The reference is here
Your query becomes:
select empname from emp
where substring(empname,3,1)="a"
order by empname;
The INSTR() function returns the position of the first occurrence of a string in another string. This function performs a case-insensitive search.
Select INSTR(first_name, 'm') from Tablename where first_name = 'monika';
Reference:- https://www.w3schools.com/sql/func_mysql_instr.asp
I have a db table with a field with values stored in the value1,value2,value3,value4 format.
I want to find all the rows where this field contains a defined value, eg. value3.
How can I perform a query to search a value in a field like this one?
use FIND_IN_SET()
SELECT *
FROM tableName
WHERE FIND_IN_SET('value3', 'comma separated value here') > 0
SQLFiddle Demo
SOURCE
MySQL FIND_IN_SET
Description from MySQL Docs:
Returns a value in the range of 1 to N if the string str is in the
string list strlist consisting of N substrings. A string list is a
string composed of substrings separated by “,” characters. If the
first argument is a constant string and the second is a column of type
SET, the FIND_IN_SET() function is optimized to use bit arithmetic.
Returns 0 if str is not in strlist or if strlist is the empty string.
Returns NULL if either argument is NULL.
You can do this using like:
where concat(',', field, ',') like '%,value3,%'
SELECT *
FROM tableName
WHERE lower('comma separated values') LIKE 'value3'
I have an ajax-search on a mysql-db. Example: Search for "man" which I query with:
SELECT id FROM table WHERE name LIKE '%man%;
I now want to sort the result to have all results starting with the search in alphabetical order:
man
mankind
after that I want to have all results width the search INSIDE in alphabetical order, like:
iron man
woman
How can I do that?
You can order by the position of your search term in the string:
SELECT id
FROM table
WHERE name LIKE '%man%'
ORDER BY INSTR(name, 'man'), name
See also: INSTR(), LOCATE()
You could also change the expression to only distinguish between start of the string or anywhere else:
ORDER BY IF(INSTR(name, 'man'), 1, 0)
You can construct your ORDER BY using a CASE statement to verify the substrings. Note: I am using UPPER() here to convert both the search value and the column value to uppercase, for a case-insensitive match. If that is not your need, remove the UPPER().
ORDER BY
CASE
/* Matches the start of the string */
WHEN UPPER(LEFT(name, 3)) = 'MAN' THEN 1
/* Doesn't match the end or the start (in the middle) */
WHEN UPPER(RIGHT(name, 3)) <> 'MAN' THEN 2
/* Matches the end of the string */
WHEN UPPER(RIGHT(name, 3)) = 'MAN' THEN 3
ELSE 4
END,
/* Then order by the name column */
name
This method should be fairly portable, but I like the INSTR() answer below better.
Try this
SELECT id FROM table WHERE name LIKE 'man%';
UNION
SELECT id FROM table WHERE name LIKE '%man%';
I have a MySQL table which contains comma-separated values like this:
first row=(3,56,78,12)
second row=(6,44,2,3)
third row=(67,4,2,7,1)
fourth row=(88,55,22,33)
fifth row=(88,55,3,1,5)
I want to select the rows which have 3 in their set. How can I do this?
Try:
SELECT * FROM TABLE_NAME WHERE FIND_IN_SET( 3, COLUMN_NAME )
How about something like
SELECT *
FROM Table
WHERE Field LIKE '3,%'
OR Field LIKE '%,3'
OR Field LIKE '%,3,%'
OR Field = '3'
Just Use Mysql Function FIND_IN_SET(str,strlist) .
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.
SELECT * FROM table_name WHERE FIND_IN_SET('3', column_name);
use this condition
WHERE firstrow LIKE '3,%'
OR firstrow like '%,3'
OR firstrow like '3'
SELECT *
FROM Table
WHERE Field IN ($first_row)
OR Field IN ($second_row)
OR Field IN ($third_row)
OR Field IN ($fourth_row);
you can use IN statement in your query.Please try this.
SELECT *
FROM Table
WHERE Field IN ($first_row)
OR Field IN ($second_row)
OR Field IN ($third_row)
OR Field IN ($fourth_row);