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.
Related
I would like to know how to write sql query to search string in all columns in a table.
i.e in single where condition
I have column1, column2,... column50 fields in a table
Right now am using query like
select * from tblist where column1 like '%searchstr%'OR column2 like '%searchstr%' OR ....it goes on
Is there anyway to write sql query to search string in all columns
In the case of your exact query as given, we can try using IN:
SELECT *
FROM tblist
WHERE 'searchstr' IN (column1, column2, ...);
If you really need to use LIKE here, then there is no real shortcut available.
you could save on the grunt work by using the metadata tables to generate your query and then run it. Eg:
select concat('%searchstr% like ',COLUMN_NAME,' OR ')
from information_schema.columns t
where table_name='t' /*change to the table name*/
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);
I would like to rename the columns in the results of a SELECT expression. Of course, the following doesn't work:
SELECT * AS foobar_* FROM `foobar`
As I'm new to SQL, I think I'm just missing a concept, tool, or keyword that would lead to the answer. A hint in the right direction would be appreciated. Thanks!
UPDATE
I'm looking for a generic way to do this, and MySQL-specific techniques are absolutely fine.
In short, I'm writing a tool that "exports" the results of MySQL queries to Google Spreadsheets (via the Google Data API). Some queries are joins, so to make columns unique I wanted to prefix all column names with their respective table names.
You can alias the column names one by one, like so
SELECT col1 as `MyNameForCol1`, col2 as `MyNameForCol2`
FROM `foobar`
Edit You can access INFORMATION_SCHEMA.COLUMNS directly to mangle a new alias like so. However, how you fit this into a query is beyond my MySql skills :(
select CONCAT('Foobar_', COLUMN_NAME)
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'Foobar'
you have to rename each column
SELECT col1 as MyCol1,
col2 as MyCol2,
.......
FROM `foobar`
select column1 as xyz,
column2 as pqr,
.....
from TableName;
I always thought that you could use OR in a LIKE statment to query things in MySQL. So, if I wanted to compare multiple fields in a row to 1 keyword or term:
SELECT * FROM MyTable WHERE Column1 OR Column2 LIKE '%keyword%';
and if I had an array of words to compare:
SELECT * FROM MyTable WHERE Column1 OR Column2 LIKE '%keyword1%'
AND Column1 OR Column2 LIKE '%keyword2%';
I don't believe that syntax is correct, however. Is there an efficient method of writing this aside from something like:
SELECT * FROM MyTable WHERE Column1 LIKE '%keyword1%' OR Column2 LIKE
'%keyword1%' AND Column1 LIKE '%keyword2%' OR Column2 LIKE '%keyword2%';
Am I going about this correctly?
Use this::
SELECT * FROM MyTable WHERE (Column1 LIKE '%keyword1%' OR Column2 LIKE
'%keyword1%') AND (Column1 LIKE '%keyword2%' OR Column2 LIKE '%keyword2%');
The closest to the syntax you are desiring is:
SELECT * FROM MyTable
WHERE (CONCAT(Column1, Column2) LIKE '%keyword1%')
AND (CONCAT(Column1, Column2) LIKE '%keyword2%')
Note: that the "%" at the start of your search string precludes the use of indexes. If there are any large number of records to search, it would be best to rethink the implementation.
If you cannot guarantee that each column is not NULL, then use CONCAT_WS instead:
SELECT * FROM MyTable
WHERE (CONCAT_WS("-", Column1, Column2) LIKE '%keyword1%')
AND (CONCAT_WS("-", Column1, Column2) LIKE '%keyword2%')
This CONCAT_WS solution also has the possible benefit of assuring that matches of your "keyword" where in only in Column1 OR Column2, if you select a separator character that is never present in your keywords.
You can compare a field to multiple strings at once in MySQL with REGEXP_LIKE()
SELECT *
FROM MyTable
WHERE REGEXP_LIKE(Column1, 'keyword1|keyword2') OR REGEXP_LIKE(Column2, 'keyword1|keyword2')
is there a way i can use like function in a set of data?
i tried
select * from table1 where column1 like ('%1%','%2%','%3%','%4%');
but it didn't work.
my scenario is having 2 select statements such as select * from class1 where firstname in (select name from class2 where firstname = 'greg');
but instead of having class1.firstname = class2.firstname i wanted it to be class1.firstname like concat('%',class2.firstname,'%');
You could "OR" them:
... WHERE (column1 like '%1%') OR (column1 like '%2%') OR ...
like takes one argument
select *
from table1
where column1 like ('%1%');
if you want to check for many then use OR or UNION operators
select *
from table1
where column1 like ('%1%')
or column1 like ('%2%');
Normally you would use the IN function when you have a list of strings to compare to. But here you need a combination of LIKE and IN, and as far as I know there is no function in SQL that does this, so you'll need to use the OR operator.
You can use the execute to build the dynamic sql, but you must beware of safety and performance issues that may arise.
Another options is using regex