I have a table of 13 columns , out of those i have 5 columns that contains let say string data or VARCHAR data.
Now i have a string let say "abc".
I want to write a sql query to get all the rows that have this "abc" string in those 5 columns. "abc" can be the data of the column of part of the data of the column.
I used LIKE function
SELECT * FROM table WHERE col1 OR col2 OR col3 OR col4 OR col5 LIKE '%abc%';
but it didnt worked n got back all the rows.
I dont know how use MATCH function either, I m nt good in sql. so can anyone help.
You can specify condition multiple times:
SELECT *
FROM table
WHERE col1 LIKE '%abc%'
OR col2 LIKE '%abc%'
OR col3 LIKE '%abc%'
OR col4 LIKE '%abc%'
OR col5 LIKE '%abc%';
This will be really slow because you have multiple OR and non-SARGable condition.
Alternatively:
SELECT *
FROM table
WHERE CONCAT_WS('^', col1,col2,col3,col4,col5) LIKE '%abc%';
Using MATCH (preferred solution that utilizes full-text index):
SELECT *
FROM table
WHERE MATCH(col1, col2,col3,col4, col5) AGAINST ('abc');
SqlFiddleDemo
Keep in mind that to use MATCH you need to create index first:
ALTER TABLE tab ADD FULLTEXT ft_index_name (col1,col2,col3,col4,col5);
Related
For example if I want to search below.
> lav'ro (I can't search it by "lav'r" because of ' in the string)
> am[op]st (I can't search it by "am[o" because of [ in the string)
My question is how can I use select query so I will help me to bring output?
Below are my SELECT query.
SELECT * FROM TableName
WHERE (Col1 LIKE '%$value%' OR Col2 LIKE '%$value%')
ORDER BY
CASE WHEN (Col1 LIKE '$value' AND Col2 LIKE '$value') THEN 1
WHEN Col1 LIKE '$value' THEN 2
WHEN Col2 LIKE '$value' THEN 3
WHEN Col1 LIKE '$value%' THEN 4
WHEN Col2 LIKE '$value%' THEN 5
WHEN Col1 LIKE '%$value' THEN 6
WHEN Col2 LIKE '%$value' THEN 7
WHEN Col1 LIKE '%$value%' THEN 8
WHEN Col2 LIKE '%$value%' THEN 9
ELSE 10
END asc,
length(Col1 or Col2) limit 15
Here "$value" dynamic word for search.
It's working well In normal search, problem with symbolic search.
You have to include \ before the single quote to make it work
SELECT * FROM `table` WHERE `column` LIKE 'lav\'ro'
For the 2nd issue try % to append before or after or both
SELECT * FROM `table` WHERE `column` LIKE '%am[o%'
You don't have to use like. You can just use instr() or =:
where instr(col1, ?) > 0
Use like and regular expressions when you need them for their pattern matching capabilities.
And -- for the record -- there is no problem searching for single quotes in a string. The only problem is expressing the string. To do this, double up on the single quote:
where col1 like concat(?, '%')
works when you pass a parameter with a single quote. This also works:
where col1 like 'lav''r%'
Is there a more elegant SQL query for selecting rows in which ID can be in col1 or col2 or col3, etc. without a long condition containing plenty of OR operators ?
I'd like to produce the same result as the following query:
SELECT *
FROM tab
WHERE (col1 = _x_ OR col2 = _x_ OR col3 = _x_);
You can use something like :
select * from tab where 'x' in (col1, col2, col3...)
I am using node.js with squel.js for SQL database.
I am storing array of string into one table field.
Let us say, My table name is like XYZ.
And my table data is like
id col2
1 '["a", "b", "x", "y"]'
2 '["x", "y", "q"]'
3 '["q", "e"]'
4 '["p", "q"]'
Now i want to know how to make query for search like
if i want to how to make query if i want to fetch records like
If i want to fetch all records which contains like ["x", "y"]. In this scenario i want expect result id 1 and 2.
If i want to fetch all records which contains like ["q"]. In this scenario i want expect result 2 and 4.
Please help me here i stuck here. Thanks in advance.
If I understand your question correctly,just use LIKE can do it
For the first requirement,you can use
SELECT * FROM yourtable WHERE col2 LIKE `%"x"%` AND col2 LIKE `%"y"%`
OR
SELECT * FROM yourtable WHERE col2 LIKE `%"x","y"%`
Since I do not know the format exactly,is it just check match x and y? Or just match "x","y" exactly?
For the second requirement,you can use
SELECT * FROM yourtable WHERE col2 LIKE `%"q"%`
col2 can be a JSON column, thus you can do something like:
SELECT * FROM XYZ WHERE JSON_CONTAINS(col2, JSON_ARRAY('x', 'y'));
SELECT * FROM XYZ WHERE JSON_CONTAINS(col2, JSON_ARRAY('q'));
The second query would returns rows 2, 3 and 4, but I guess your expectation might be incorrectly stated, since 'q' exists in the col2 array for all of those.
My database has 2 columns that contain text. When it has say 3 records as follows:
rec# col1 col2
1 my name is fred is
2 john mike
3 today not sat not it sat
I would appreciate help constructing a regular expression that will return record numbers:
1 -> because "is" matches
3 -> because "'not" and "sat" match (i.e. at least one match exists)
I think you can do this as:
select t.*
from table t
where col1 rlike replace(col2, ' ', '|');
This turns col2 into a regular expression. So, note that this answer will be very sensitive to the contents of col2. If it contains regular expression special characters, then this probably will not work.
What do you mean by "matches"?
This SELECT will find the rows where col1 contains at least one of the words is or not or sat:
SELECT rec_num
FROM tbl
WHERE col1 REGEXP '[[:<:]](is|not|sat)[[:>:]]';
This says that at least one of those words exists in both col1 and col2:
SELECT rec_num
FROM tbl
WHERE col1 REGEXP '[[:<:]](is|not|sat)[[:>:]]'
AND col2 REGEXP '[[:<:]](is|not|sat)[[:>:]]';
Change AND to OR to ask if one of the words exists in either (or both) column.
If you need the same word (is/not/sat) to match both col1 and col2, that is more complex:
SELECT rec_num
FROM tbl
WHERE ( col1 REGEXP '[[:<:]]is[[:>:]]'
AND col2 REGEXP '[[:<:]]is[[:>:]]' )
OR ( col1 REGEXP '[[:<:]]not[[:>:]]'
AND col2 REGEXP '[[:<:]]not[[:>:]]' )
OR ( col1 REGEXP '[[:<:]]sat[[:>:]]'
AND col2 REGEXP '[[:<:]]sat[[:>:]]' );
If you mean something else, practice asking precise questions.
Addenda
It is not practical in SQL to discover which words (if any) are in common between two text fields. Such a task would be better done in an application programming language (PHP, Java, Perl, ...).
I've got a table with two columns like
column1 column2
01234 56789
33333 883737
What I need is something like this, but I've no idea how to sql this:
SELECT * FROM table1 WHERE (value1|+|value2)='0123456789'
Any ideas?
Use the CONCAT function, as below:
SELECT *
FROM table1
WHERE CONCAT(column1, column2) = '0123456789';
Reference:
CONCAT function on MySQL Reference Manual
Something like
SELECT * FROM table1 WHERE CONCAT(value1,value2)='0123456789'
note, you lose the indexing advantages by using this function.
I assumed value1 and value2 are tge column names vs column1 column2.