I have two use cases.
For all the columns in my table I am allowing user to search for contains. My query will be
Select * from table where column like "%value%";
When it comes to boolean columns and if they are searching for t/tr/tru/true I should be showing the results with
Select * from table where column is true;
Currently I am checking the entered string with a regular expression and hard coding the query. Is there a better way to achieve this ?
When the user query for equals to and the string contains special characters
Select * from table where column = "string with backslash escaped";
This query does not work but if I use like instead of = it works.
But like doesn't work for boolean columns.
You can use something like this to match against a boolean expression:
SELECT *
FROM my_table
WHERE IF(boolean_column, 'true', 'false') LIKE '%tr%';
The Like clause can be used only on text type of data (i.e VARCHAR, CHAR etc). Your logic should be more of finding out column name being searched and based on that define which operators to use.
If you have option to use a view in place of this table, you can create a view on this table and convert Boolean column to text based column which would allow you to search that column like all others.
Related
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.
I'm looking for a way to get a row from a tabla which have a column data type of string. This column could have values as follows:
1. "1,2,3,4,5"
2. "X,3,4,5,8"
3. "X,X,3,4,5"
4. "1,2,3,4,X"
5. "1,3,4,X,X"
and so on, ...
I want to accomplish a search for a String like
"1,2,3,4,5"
I tried with a
SELECT *
FROM *table_name*
WHERE *column* LIKE '%1,2,3,4,5%';
hoping this query could retrieve at least three results (in the example, first, third and forth strings) but it returns only the first string, because of course it's the only string that matches with the specified criteria. Anyone knows a way for me to accomplish this achievement?
I assume the X listed is literally the X character - if so, try
SELECT * FROM table WHERE '1,2,3,4,5' REGEXP REPLACE(column, 'X', '.')
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?
I'm trying to write the following statement:
WHERE field LIKE 'Pandora' AND field Not Like 'radio', 'digital', 'internet';
Translation: Select where field is like Pandora and not like radio, digital, or internet.
Is there a way to write this statement without writing Not Like 3 times with ANDs in between?
Thank you
If "field" is not just single words, you would need to do something like this:
SELECT * FROM table WHERE field LIKE '%Pandora%' AND field NOT LIKE '%radio%' AND field NOT LIKE '%internet%' and field NOT LIKE '%digital%';
First of all, your query is redundant, in that if field is LIKE 'pandora', then the other conditions will by default return false.
There is no possible way that field can be equal to 'Pandora', 'radio', 'digital', and 'internet'.
As a result, you can simplify your query using the following example:
SELECT *
FROM example
WHERE field = 'Pandora';
If the two conditions represent two separate fields, then you can use the REGEXP operator to enforce the DRY principle while still allowing for further pattern matching:
SELECT *
FROM example
WHERE field_1 = 'Pandora'
AND field_2 NOT REGEXP '^(radio|digital|internet)$';
If you're searching for specific words, you can use NOT IN()
WHERE field LIKE 'Pandora' AND field NOT IN('radio', 'digital', 'internet');
If you need the wildcard % in your search you'll need to use multiple LIKEs.
I have this table under user_name='high'
function_description :
akram is in a date
test
akram is studying
test4
kheith is male
test3
I want a query that returns results of field that have at least an 'akram'
SELECT *
FROM functions
WHERE 'isEnabled'=1
AND 'isPrivate'=1
AND user_name='high'
AND function_description LIKE '%akram%'
and this returns absolutely nothing!
Why?
You are listing the column names as if they are strings. This is why it returns nothing.
Try this:
SELECT *
FROM functions
WHERE user_name='high'
AND function_description LIKE '%akram%'
edit: After trying to re-read your question... are isEnabled and isPrivate columns in this table?
edit2: updated.. remove those unknown columns.
You are comparing strings 'isEnabled' with integer 1, which likely leads to the integer being converted to a string, and the comparison then fails. (The alternative is that the string is converted to an integer 0 and the comparison still fails.)
In MySQL, you use back-quotes, not single quotes, to quote column and table names:
SELECT *
FROM `functions`
WHERE `isEnabled` = 1
AND `isPrivate` = 1
AND `user_name` = 'high'
AND `function_description` LIKE '%akram%'
In standard SQL, you use double quotes to create a 'delimited identifier'; in Microsoft SQL Server, you use square brackets around the names.
Please show the schema more carefully (column names, sample values, types if need be) next time.