I've searched in mysql query a word in multiple column in java program. The number of column is variable.
It is correct this query:
select * from customer with (city, name) like%'adelaide'%
You can use CONCAT() function:
select * from customer WHERE concat(city,name) like '%adelaide%'
You can add as many columns to the concat function as you like.
Also as you see I changed your like syntax, '%WORD%' and used a simple where clause.
It's better to use CONCAT_WS() function.
CONCAT() returns NULL if any argument is NULL.
CONCAT_WS() skip any NULL values after the separator argument.
multiple like statements can not be used with or directly. You have to use column name for each like statement.
Use multiple like as mentioned below.
Select *
from animals
where
(
[animalscolumn] like ('%dog%') or
[animalscolumn] like ('%cat%') or
[animalscolumn] like ('%gerbil%') or
[animalscolumn] like ('%hamster%') or
[animalscolumn] like ('%guinea pig%')
)
select * from customer with city like%'adelaide'% or name like%'adelaide'%
Related
I'm new to SQL and I'm stuck on a multiple find in SQL.
Here I'm trying to search table1 in the three columns. But I'm stuch in trying to find two phrases. I'm trying with an OR statement.
If I remove OR LIKE '%paris%' it works but how do I find multiple words/phrases. And would this statement be case sensitive?
I'm also using MySQL to run the above.
SELECT * FROM `table1`
WHERE
CONCAT_WS('|',`target_1`,`target_2`,`target_3`)
LIKE '%london%' OR LIKE '%paris%'
In your code your second condition is sintactically wrong because is missing the a part for the match
so you should repeat the condition as
SELECT *
FROM `table1`
WHERE CONCAT_WS('|',`target_1`,`target_2`,`target_3`) LIKE '%london%'
OR CONCAT_WS('|',`target_1`,`target_2`,`target_3`) LIKE '%paris%'
One option is to use regular expression, then you can also have case insensitive matching (3rd parameter to REGEXP_LIKE)
SELECT *
FROM table1
WHERE REGEXP_LIKE(CONCAT_WS('|',`target_1`,`target_2`,`target_3`), 'london|paris', 'i');
You should repeat the left operand and use (maybe?) multiple conditions i/o concatenating:
SELECT * FROM `table1`
WHERE (`target_1` like '%london%' OR `target_1` like '%paris%')
AND (`target_2` like '%london%' OR `target_2` like '%paris%')
AND (`target_3` like '%london%' OR `target_3` like '%paris%')
I have a database with columns with the same name and a number after it, like:
med1, med2, med3, med4..... etc.
Is it posible to do something like this:
Select medewerker.med*
FROM table
Instead of this?:
Select medewerker.med1,
medewerker.med2,
medewerker.med3,
medewerker.med4
FROM table
Nope. you can use LIKE in a where clause but definitely not when specifying the columns to select
Use this instead if the only fields are the ones specified above.
SELECT *
FROM table
if not you cannot use a LIKE statement on column SELECT's.
if you are refering to values inside the medewerker column then use
SELECT *
FROM table
WHERE medewerker LIKE 'med%'
I have a table where I extract some values, one column values can contain "value1|value2|value3", but I only want to get the characters before the | - "value1".
This is what I tried, but it doesn't work.. What am I doing wrong?
Thanks!
$sql = "SELECT * LEFT('Like', LOCATE('|', 'Like')-1) FROM $tablename
WHERE Parent = '0' AND Type LIKE 'top' ORDER BY Order ASC";
I want to use this for ALL values, not just one field..
you need the following statement to get that portion of [ColName]:
LEFT([ColName],INSTR([ColName],"|")-1)
If you want to select multiple columns into the same recordset column you can union all with something like the following:
SELECT LEFT(ColName,INSTR(ColName,"|")-1) AS FirstValue From $TableName;
UNION ALL
SELECT LEFT(ColName2,INSTR(ColName2,"|")-1) AS FirstValue From $TableName;
If you want to use this on multiple columns, script the creation of the sql.
Two things: (1) you don't have a comma between your * and the expression you're trying to do with LEFT and (2) you're putting like in quotes, so the functions are working on the constant value like instead of your column named like. Try putting like in backticks.
SELECT *, LEFT(`Like`, LOCATE('|', `Like`)-1)
...
You can also use the MySQL SUBSTRING_INDEX function for this:
SELECT *, SUBSTRING_INDEX(`Like`, '|', 1)
...
I have a MySQL table column rubrics which contains string value '61,80,112,256'. So I try execute that query:
select * from table where 256 in (rubrics) and 61 in (rubrics)
And no result returns. Any suggestions?
Since your rubrics column is a comma separated list the IN operator will not work.
MySQL does have a function that can find a value in a string list so you should be able to use FIND_IN_SET():
select *
from yourtable
where find_in_set(61, rubrics)
or find_in_set(256, rubrics)
See SQL Fiddle with Demo
Something like WHERE rubrics LIKE '%,256,%' OR rubrics LIKE '256,%' OR rubrics LIKE '%,256'. Using parenthesis you can also filter on the 61, but the resulting query will be come messy. You'd better normalize your data, so you can use subqueries and joins using keys (the real deal).
(see also bluefeet's answer as FIND_IN_SET is a better approach)
Try this
select * from table where rubrics like '%'+'256,'+'%' and rubrics like '%'+'61,'+'%'
IN operator does not work with strings
use correct substring matching operator like LIKE or LOCATE
one advice - update your rubics column to begin and end with , character, that will make your LOCATE(",62,", rubics) operations unambiguous as opposed to LOCATE("62", rubics) which will match also 622 and 262 and other combinations. Locating ,62, wil fail if your rubics has value of 62,blah,foo,bar because it doesn't start with ,
I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.