find elements of a varchar in another varchar - mysql

i have a varchar field with the content like these:
a,b,c,d
e,d,a,c
b,q,d,e
i need to do a query that select only the rows with the field that has elements equals with an input string.
ex
input: c,a
rows selected:
a,b,c,d
e,d,a,c
is possible without use the OR (field like '%a%' OR field like '%c%') ?
thanks

Yes, you can use...
WHERE
myField LIKE '%a%' AND myField LIKE '%c%'
However, this does sound like you've got a nasty field in your SQL.
Normally, you would use a link-table to specify this kind of information:
Table1
id
otherdata...
Table2
id
letter
LinkTable
Table1Id
Table2Id
So in LinkTable you would have many entries that link your records. So instead of storing 'a,b,c,d' in a field, you have four link records:
Table1Id Table2Id
1 1 (a)
1 2 (b)
1 3 (c)
1 4 (d)

Yes but you need a trick: Create a second column which contains the same value but sorted:
a,b,c,d
a,c,d,e
b,d,e,q
Now, you can query for %a%c%
This won't work if the query string can be a substring of any unwanted value. If this is the case, you need quotes:
"a","b","c","d"
"a","c","d","e"
"b","d","e","q"
and query for %"a"%"c"%

Related

Compare comma-separated string in MySQL column where column is also comma separated

Compare comma-separated string in MySQL column where column is also comma separated
For Example:
Id name catid
1 abc 4,5,2
2 bcd 5
3 efg 9,1,7
SELECT * FROM TABLE WHERE catid IN (2,5,6)
Here 2,5,6 I have to compare with the catid value to get the result.
Any Help to get the right out put, I used FIND_IN_SET, but could not make it work for my case
according to your problem you can use LIKE operator instead of IN
SELECT * FROM testchintan WHERE CONCAT(',',catid,',') LIKE '%,5,%'
OR CONCAT(',',catid,',') LIKE '%,2,%' OR CONCAT(',',catid,',') LIKE '%,6,%'
OR YOU can use REGEXP
SELECT *
FROM testchintan
WHERE catid REGEXP '[4,9,5]'
OR
SELECT *
FROM testchintan
WHERE
REPLACE(catid,',','|') REGEXP '[9,7]'
Note that I'm not seriously advocating this as a solution...
SELECT * FROM my_table WHERE FIND_IN_SET(5,catid) OR FIND_IN_SET(2,catid);

found integer in text data with mysql

I have a database table,in one Column I have data Like:
id tags
1 1.2,2.2,22.2
2 20.1,30.1,45.2,46.0,55.3
3 1.3,6.1,7.2,9.4,10.2
I want to search ids having num 1 in tag's Column
Use LIKE operator to search the data
SELECT id
FROM tableA
WHERE tags LIKE '%1%'
Just another using INSTR when wildcard is on both sides in a LIKE search!
SELECT id
FROM tableA
WHERE INSTR(tags,'1') > 0 ;

Sql query returns only one row (LIKE)

SELECT *
FROM customers
WHERE Firstname LIKE 'George'
The problem is that i have more than 1 rows in the table with tha name Geoge and the result of the query shows only one row
You will want to include the wildcard % character to include the rows the have George present in the name:
SELECT *
FROM customers
WHERE Firstname LIKE '%George%';
If George will always appear at the beginning, then you can include the wildcard on the end:
SELECT *
FROM customers
WHERE Firstname LIKE 'George%';
you need to add a wildcard character % to match any value that contains george
SELECT *
FROM customers
WHERE Firstname LIKE '%George%'
MySQL LIKE Operator
the statement
WHERE Firstname LIKE 'George'
is equivalent with
WHERE Firstname = 'George'
that is why you are only getting one record which firstname is george.
UPDATE 1
SQLFiddle Demo
try
LOWER(Firstname) LIKE '%george%'
handles partial values and avoids case sensietivity issues.

MySQL Tricky Regex

I have a field which has strings (commas part of string) like "X1,X2,X3,X4,X5".
Let's take example MySQL table-
id FIELD1
1 X1,X3
2 X1,X3,X4
3 X2,X3,X4
4 X2,X4
5 X1,X3,X4,X5,X6
let Q = "X1,X3,X4,X5"
Now, what is the query to get rows where all the characters of FIELD1 value are contained in Q. Q is such that there are only 8 characters(X1,X2..) of such kind, and characters in a string are not repeated.
Select id FIELD WHERE "...Some regex on characters of Q...."
In other words,
it should return, row 1, 2 as X1, X3, X4 present in Q.
It should not return row 3,4,5 as X2,X6 is not present in Q.
Thank you so much
As juergen d said before me, your DB structure is faulty.
If you are using this structure, the ugly SQL query you'll get is the following:
SELECT id FROM Crooked_Table WHERE (FIELD1 LIKE '%X1%' OR FIELD1 LIKE '%X3%' OR FIELD1 LIKE '%X4%' OR FIELD1 LIKE '%X5%') AND NOT (FIELD1 LIKE '%X2%' OR FIELD1 LIKE '%X6%')
And you'll need to cover all possible comma-separated values in the query, otherwise it won't work.

How to search using an sql statement?

How can I find all the ids of an SQL table? For example I want to search for the word "key" and return the ids in which this word was found.
Assuming that id is the name of a column in your table you would need to use LIKE
SELECT id
FROM YourTable
WHERE id LIKE '%key%'
The % is a wildcard meaning match any set of zero or more characters so this would find rows where the id value contains the substring (not necessarily word) "key".
SELECT [id field name] FROM [Tabel Name] WHERE [field to seach in for key] LIKE '%key%';
Would this be what you are looking for?
INSERT INTO dest_table (dest_id)
SELECT source_id
FROM source_table
WHERE source_column LIKE '%key%';