mysql match string with start of string in table - mysql

I realise that it would be a lot easier if I could modify the table when it was created, but assuming I can't, I have a table that is such as:
abcd
abde
abdf
abff
bbsdf
bcggs
... snip large amount
zza
The values in the table are not fixed length.
I have a string to match such as abffagpokejfkjs .
If it was the other way round, I could do
SELECT * from table where value like 'abff%'
but I need to select the value that matches the start of a string that is provided.
Is there a quick way of doing that, or does it need an itteration through the table to find a match?

Try this:
SELECT col1, col2 -- etc...
FROM your_table
WHERE 'abffagpokejfkjs' LIKE CONCAT(value, '%')
Note that this will not use an index effectively so it will be slow if you have a lot of records.
Also note that some characters in value (e.g. %) may be interpreted by LIKE as having a special meaning, which may undesirable.

LIKE can be avoided, by truncating the comparison string to each value's length:
... WHERE LEFT('abffagpokejfkjs', LENGTH(value)) = value

Related

Column that starts with AST and ends in only number

I suck at REGEX, but I need to pull all the records from a table column that stats with AST, and the rest only contains numbers after. I am assuming this can be done with just REGEX and not LIKE but I'm not sure.
For instance AST000001
and not AST99XXH011
SELECT * FROM table WHERE column LIKE 'AST%' AND column REGEXP '[0-9]$'
You can use REGEXP/RLIKE on the whole column value (using start-of-string (^) and end-of-string ($) anchors to ensure you match the entire column):
SELECT *
FROM `table`
WHERE `column` REGEXP '^AST[0-9]+$'
Demo on dbfiddle

MySQL - WHERE x IN ( column)

I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it

MySQL Search for Value (Integer or String) in a Database Array Field

I have two fields in my MySQL database that are arrays. The datatype is shown as LONGTEXT with a Comment of (DC2Type:array).
For example, the integer values stored in this field would look like this:
a:4:{i:0;i:9;i:1;i:10;i:2;i:11;i:3;i:12;}
And the String values would look like this:
a:2:{i:0;s:6:"Value1";i:1;s:6:"Value2";}
I need these fields this way so I can store columns that are filterable. E.g. the first one may be age groups so ages 9,10,11,12 are represented.
My query must then get all records that say are relevant for age 10 or in some cases say I want to find those that are 10 and 11.
I've tried the IN and FIND_IN_SET syntaxes but neither is returning any results.
Using IN
SELECT *
FROM table_name
WHERE MyField IN (10)
Using FIND_IN_SET
SELECT *
FROM table_name
WHERE FIND_IN_SET(MyField,'Value1') > 0;
I know arrays are probably not the best field to store values in but I didn't want to have separate fields for each AgeGroup e.g. Age1, Age2, etc. or each category e.g Value1, Value2, etc.
Any thoughts on how I can find a value or values from a database array field, please?
Thanks!
You can use a pattern match.
Integer:
WHERE MyField LIKE '%i:10;%'
String:
WHERE MyField LIKE '%s:6:"Value1";%'
6 has to be replaced with the length of the string you're searching for.
If you want to search for multiple numbers or strings, you can use a regular expression with alternation:
WHERE MyField RLIKE 'i:(10|11);'
WHERE MyField RLIKE 's:(6:"Value1"|10:"LongValue2");'
Note that none of these methods can make use of an index on the table. It's generally a bad idea to store arrays in database columns, you should store them as separate rows in a many-to-many table.

If value is present in stored text string

I have a table, one of the columns contains a text values, some of which are comma separated string, like this:
Downtown, Market District, Warehouse District
I need to modify my query to see is a given value matches this column. I decided that using IN() is the best choice.
SELECT *
FROM t1
WHERE myValue IN (t1.nighborhood)
I am getting spotty results - sometimes I return records and sometimes not. If there's a value in t1.nighborhood that matches myValue, I do get data.
I checked and there are no MySQL errors. What am I missing?
You can use FIND_IN_SET() to search a comma-delimited list:
SELECT *
FROM t1
WHERE FIND_IN_SET(myValue, REPLACE(t1.nighborhood, ', ', ','));
The REPLACE() is necessary to remove the extra spaces.
Another solution is to use regex to match your search value surrounded by commas if necessary:
SELECT *
FROM t1
WHERE t1.nighborhood REGEXP CONCAT('(^|, )', myValue, '(, |$)');
In general, it's bad design to store distinct values in a single column. The data should be normalized into a related table with a foreign key.

SQL - Query to find if a string contains part of the value in Column

I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?