find exact number in text field in sql - mysql

I am trying to query exact number in array inside text field in sql
so for example
SELECT tbl_campaigns.s_id,a_list_fk_i_id_tbl_affiliates
FROM tbl_campaigns
WHERE a_list_fk_i_id_tbl_affiliates LIKE '%1%'
can return
[3,8,7,11]
which is not good because i would like to get the arrays that contain 1

You could use JSON functions:
SELECT s_id,a_list_fk_i_id_tbl_affiliates
FROM tbl_campaigns
WHERE JSON_CONTAINS(a_list_fk_i_id_tbl_affiliates, 1)

Related

How to remove a word in a column which lies between two exact words

In a Mysql database table, The example row of a column looks like below,
/town=xxx/area=yyy/street=zzz/apartment_name=aaa/doorno=bbb
How to remove the word in between /street/ and /doorno/. So my expected output should be shown below,
/town=xxx/area=yyy/street=zzz/doorno=bbb
MySQL gives many useful String function e.g. LOCATE(), POSTION, SUBSTR, SUBSTRING etc.
Refer to : https://dev.mysql.com/doc/refman/5.7/en/string-functions.html
Query Solution to given Problem :
select concat ((SELECT SUBSTRING_INDEX('/town=xxx/area=yyy/street=zzz/apartment_name=aaa/doorno=bbb','/',4)),
(select SUBSTR('/town=xxx/area=yyy/street=zzz/apartment_name=aaa/doorno=bbb',
(select POSITION((SELECT SUBSTRING_INDEX('/doorno','/',4)) IN "/town=xxx/area=yyy/street=zzz/apartment_name=aaa/doorno=bbb")),100)
))

OrientDB use LIKE in an encapsulated field

I have an OrientDB table named CalculationFunctionGroup, in which I have a field named functions.
This field has JSON content like this:
[{"#type":"d","#version":0,"#class":"CalculationFunction","name":"AR_0015_0280","code":"//AR_0015_0280 OTTIMIZZATA / FORMATTATA\nvar worka01 = anagPay(\"AR_0015_0280\", -1);\nreturn worka01;","language":"Javascript"}]
I want to extract rows satisfying a LIKE condition on the code element of the JSON.
I've tried this query:
SELECT FROM CalculationFunctionGroup
WHERE functions.code LIKE '%OTTIMIZZATA%'
But the number of extracted rows is ZERO!
Try this:
select expand(distinct(rid))
from (select #rid,functions.code
from CalculationFunctionGroup unwind functions)
where functions like "%OTTIMIZZATA%"

return distinct text which is longtext format (but not returning the repeated text)

I have a simple scenario as follow:
I have 2 column : 1) id and 2) text(which is long text format)
I use the simple query to extract all info from mysql as follow:
select id,text from dbtest
but the problem is for different id I might have the same text but in retrieval time I do not want to return rows with the same text again and again so I do not want to return the repeated texts,I tried to use distinct but it was not working,
How can I do that , any idea?
One option is to use user-defined variables:
select id,
#text:=if(#text=text, '', text) text
from dbtest, (select #text:='') t
order by text
SQL fiddle demo
I would generally recommend doing this on the application side rather than the database though.
Distinct works on all selected columns. You must use a GROUP BY:
SELECT id,text FROM dbtest GROUP BY text
So I have two questions, can you post the code you are using for us? And are some of the fields for the text empty?
The query might be detecting a "Null" kind of event in which if the field is empty, it repeats the last one because the variable might not be getting unset. In which case you might just want to unset your variables at the end of your (I'm assuming) while loop.

How can i separate the data from selected data

I have a column in my table name as URL and it contains Multiple value like "https://www.google.com/#q=how+to+make+a+android+app"
and
http://www.bing.com/search?q=how+to+make+a+android+app&go=&qs=n&form=QBLH&pq=how+to+make+a+android+app&sc=8-15&sp=-1&sk=
I want to get data separately in output like
website = https://www.google.com
Keyword = how to make a android app.
Any Idea Plz, How can i get this in MySql.
you can use substr() function in php to cut string. In your case, you can get the characters up to the end of 'https://www.google.com'.
read more at http://ro1.php.net/substr
in case you want to do it directly in your query, you can use substr() as well. Sql syntax goes like this:
SELECT SUBSTR(column, start_position, desired_length) FROM table_name
*SELECT SUBSTR(column_name, 1, 20) FROM table_name;*

MySQL like reversed

I'm trying to get around pulling all the data from a table, and cycling through it with php. Here's my current Query:
SELECT
*
FROM
ExampleTable
WHERE
StringContains LIKE "%lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh%"
ExampleTable.StringContains has values that look like 'someuser#example.com', 'someuser2#example.com', etc.
This doesn't match because LIKE only finds sub strings of the column value, not the other way around. Any ideas on commands to find rows where the table value is a substring of the passed string?
SELECT
*
FROM
ExampleTable
WHERE
'lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh' LIKE
CONCAT('%', StringContains, '%')
Try this:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE
CONCAT("%",StringContains,"%")
The key is to recognize that the column variable just represents a string, and the LIKE statement is always comparing two strings in the form
"stringA" LIKE '%stringB%'
Usually people use it to search for a "part" of a string contained in the "whole" database field string, but you can easily switch them. The only extra tool you need is the CONCAT statement, since you want the database field to be the part instead of the whole. The CONCAT statement builds a string with the %'s around the database field string, and the string form of the argument is now equivalent to
"stringB" LIKE "%stringA%"
Just make the LIKE in the opposit order. Since you have to add those % you'll have to concatenate the field first:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE CONCAT('%', StringContains, '%');