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 ;
Related
here is mysql query which displays the entire table.
select * from datas;
name id dept
sen 1 cs
der 2 td
rest 3 ui
My query is i want to search by using table content(i.e in select statement i want to use the word rest and then display the table as follows).How can i do it in mysql?
Expected output:
name id dept
rest 3 ui
From your question, I don't understand why you can't just use the '=' :
SELECT * FROM datas
WHERE name = 'rest'
Although if you're truly looking for a grep equivalent, the LIKE statement is where it's at :
SELECT * FROM datas
WHERE name LIKE '%rest%' --to select lines where rest is contained in the string
Use '=' or LIKE to compare strings in SQL?
There many ways to do this
1.Use the WHERE clause just like this
SELECT * FROM datas WHERE id>2
2.Use LIMIT clause like this
SELECT * FROM datas LIMIT 2,1
I have 'tags' table with columns (id, link). Link possible values:
id link
1 index
2 index/index
3 index/.*
when I got index, I need to select id 1. And:
index -> 1
index/index -> 2
index/test -> 3
I use something like this:
SELECT * FROM tags WHERE 'index/test' REGEXP link LIMIT 1
But it's return me id 1, if I remove LIMIT second row will be id 3. I need just full math - only id 3.
Also for regexp, the column identifier comes first, then the search value. When I get you right, you want to get id -> 3 whenever the search term is not index or index/index. So your regex could be something like
SELECT * FROM tags WHERE link REGEXP '[^index/index|^index]$'
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);
I need to find rows that contain a specific number in a set of numeric values that are stored in a table. I'm using the WHERE IN() function of mysql, but I'm having problems with the proper format.
Basically I have the following query:
SELECT id,category, text
FROM ws_cat
WHERE '11' IN (category)
The category field is a VARCHAR and looks like the following:
id category
1 11
2 12,11
3 1,13,9
So I need to find the rows with id 1 and 2 in this case. Unfortunately it doesn't work and I'm guessing it's because of the missing quotes, but all the ideas of reformating with QUOTES() or just changing the format of category to something like '12','11' wouldn't work either. Both would be possible for me as long as it works...
Use the FIND_IN_SET function:
SELECT id, category, text
FROM ws_cat
WHERE FIND_IN_SET('11', category) <> 0;
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"%