I am working on a database that has been made in a terrible way. I have to find an ID in a comma'd column.
The column values could be:
6154
5145,6154,4562
161545
My query is:
SELECT resource_id,filename,id FROM image WHERE other_vendors = '".$vendor_id."'
$vendor_id will be 6154
Now how do I match with a LIKE REGULAR EXPRESSION in a SQL QUERY where I get all the values which have a 6154 or 6154 in a comma'd string.
Just use build in function find_in_set, which search string in comma separated list:
SELECT resource_id,filename,id FROM image WHERE find_in_set('6154',other_vendors)
I think this is the easiest method:
SELECT resource_id,filename,id FROM image WHERE (','+other_vendors +',') like '%,$vendor_id,%'
Try something like:
SELECT resource_id,filename,id FROM image WHERE other_vendors REGEXP '.*(6154){1}.*'
SELECT resource_id,filename,id FROM image WHERE other_vendors = '%6154%'
Related
I'm having issue retrieving info from a DB by using this query with the lower() function:
SELECT DISTINCT "column_name" FROM "table" WHERE lower('car', 'house', 'plane'...) like '%owner%'
The query works with 1 attribute like for exmaple 'car' but when I try to use 100 I get the following error:
Error Code: 1582. Incorrect parameter count in the call to native function 'lower'
What should I change in order to be able to use and output more than a single attribute?
Thanks.
I think the query you want is something like this:
SELECT DISTINCT column_name
FROM yourTable
WHERE owner REGEXP 'car|house|plane'; -- and maybe other terms in the alternation
That is, you want to match all records where the owner column contains one of the substrings car, house, or plane.
I have a bunch url that has a string either has
hotel+4 digit number: hotel1234
or slash+4digit.html: /1234.html
Is there a regex to extract 4 digit number like 1234 either use python or mysql?
I'm thinking 'hotel'[0-9][0-9][0-9][0-9],sth like this
Thanks!
You can try the REGEXP
SELECT * FROM Table WHERE ColumnName REGEXP '^[0-9]{4}$'
or
SELECT * FROM Table WHERE ColumnName REGEXP '^[[:digit:]]{4}$';
The following is a stackoverflow.com link that might be useful showing
how to extract a substring from inside a string in Python?
Unfortunately, MySQL regexp simply returns true if the string exists. I have found substring_index useful if you know the text surrounding the target...
select case when ColumnName like 'hotel____' then substring_index(ColumnName,'hotel',-1)
when ColumnName like '/____.html' then substring_index(substring_index(ColumnName,'/',-1),'.html',1)
else ColumnName
end digit_extraction
from TableName
where ...;
The case statement above isn't necessary because of the way substring_index works (by returning the entire string if the search string isn't found).
select substring_index(substring_index(substring_index(ColumnName,'hotel',-1),'/',-1),'.html',1)
from TableName
where ...;
I would like to know if it's possible to make a query to check the first two chars of a string?
For example, I have these entries in my table:
toto
topo
poto
I want to get the entries when the two first chars are to.
As simple as this:
SELECT *
FROM TABLE
WHERE `name` LIKE "to%"
to% means that the word should start by to and can be followed by other character
Try this:
SELECT * FROM myTable WHERE RowName LIKE 'to%'
select fieldName
from tableName
where fieldName like 'to%'
if you need to check the first 2 chars of a string but return all results..
SELECT fieldname,
if(left(fieldname,2)='XX',1,0) as XCheck
FROM tablename
I have a MySQL table column rubrics which contains string value '61,80,112,256'. So I try execute that query:
select * from table where 256 in (rubrics) and 61 in (rubrics)
And no result returns. Any suggestions?
Since your rubrics column is a comma separated list the IN operator will not work.
MySQL does have a function that can find a value in a string list so you should be able to use FIND_IN_SET():
select *
from yourtable
where find_in_set(61, rubrics)
or find_in_set(256, rubrics)
See SQL Fiddle with Demo
Something like WHERE rubrics LIKE '%,256,%' OR rubrics LIKE '256,%' OR rubrics LIKE '%,256'. Using parenthesis you can also filter on the 61, but the resulting query will be come messy. You'd better normalize your data, so you can use subqueries and joins using keys (the real deal).
(see also bluefeet's answer as FIND_IN_SET is a better approach)
Try this
select * from table where rubrics like '%'+'256,'+'%' and rubrics like '%'+'61,'+'%'
IN operator does not work with strings
use correct substring matching operator like LIKE or LOCATE
one advice - update your rubics column to begin and end with , character, that will make your LOCATE(",62,", rubics) operations unambiguous as opposed to LOCATE("62", rubics) which will match also 622 and 262 and other combinations. Locating ,62, wil fail if your rubics has value of 62,blah,foo,bar because it doesn't start with ,
Anyone know the syntax to search a MySQL table column by hex value or CHAR() value? Do I have to use a LIKE or IN()?
Something like:
SELECT * FROM `myWordTable` WHERE `definition` LIKE 0xE28098;
OR
SELECT * FROM `myWordTable` WHERE `definition` LIKE CHAR(128);
Thanks!
The second one is close.
Try this:
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE concat('%',CHAR(128),'%');
In the first example consider pattern match '%' == 0x25
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE 0x25E2809825;
You need to UNHEX() values you want to match on.
For example, finding all accented e's that were corrupted into 0xC3A9:
SELECT id, FirstName
FROM Person
WHERE FirstName
RLIKE UNHEX('c3a9');