MySQL Finding data using IN and LIKE Wildcard - mysql

How would I go about doing something like:
SELECT * FROM mytable
WHERE people IN ('Jack', '%Bob%')
Finding all fields that either equal 'Jack' or contain 'Bob'? I don't think my example is the proper syntax because it's not pulling up any records.

Simply :
SELECT * FROM mytable WHERE people='Jack' OR PEOPLE LIKE '%Bob%'

Related

Multiple search and find in sql

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%')

MySQL - WHERE x IN ( column)

I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it

how do i write a sql query to select the names that may contain any one of the vowels?

I m trying to query a database with about 2000 entries. I want to select the entries in which the names may contain any one of the vowel.
I tried using the following query, but it gives me those entries that contain all the given characters in that order.
select * from myTable where name like '%a%e%i%';
How do I modify the above query to select those entries with names that may contain at least anyone of the vowels.
Try this for SQL Server:
SELECT * FROM myTable WHERE name LIKE '%[AEIOU]%';
I hope this helps you...
SELECT * FROM myTable WHERE name REGEXP 'a|e';
or.....
SELECT * FROM myTable WHERE name REGEXP 'a|e|i';
In SQL Server, you would do:
where name like '%[aeiou]%';
In MySQL, you would do something similar with a regular expression.
Use OR like this.
This will work for both SQL Server and MySql.
select * from myTable where name like '%a%' OR name like '%e%' OR name like '%i%';
Use LIKE and OR.
Query
select * from myTable
where name like '%a%'
or name like '%e%'
or name like '%i%'
or name like '%o%'
or name like '%u%'

MySQL - WHERE IN with a list into a field

I have a field ('roles') with this values
roles
row_1: 1,2,3,5
row_2: 2,13
I do this:
SELECT * FROM bloques WHERE 2 IN (roles)
and only find row_2, because it starts by 2
The LIKE option doesn't work because if I find 1
SELECT * FROM bloques WHERE roles LIKE '%1%'
, it gives me both rows.
FIND_IN_SET function cal helps you:
SELECT FIND_IN_SET('b','a,b,c,d');
For your code:
SELECT * FROM bloques WHERE FIND_IN_SET(2,roles);
Also, I suggesto to you that move your schema to 1NF
Could you use REGEXP? http://dev.mysql.com/doc/refman/5.1/en/regexp.html
Otherwise, you could add commas to the front and end of your rows.
select * from mytable where ',' + roles + ',' like '%,2,%'
SELECT
*
FROM
bloques
WHERE
roles LIKE '%,2,%'
OR roles LIKE '2,%'
OR roles LIKE '%,2'
The first case will give you all cases where 2 is in the middle of a set, the second case is when 2 starts the set, and the third case is when 2 ends the set. This is probably terribly inefficient, but it should work.
You can use this regex here:
SELECT * FROM bloques
WHERE roles REGEXP '[[:<:]]2[[:>:]]';
... or, in more generic case:
SELECT * FROM bloques
WHERE roles REGEXP CONCAT('[[:<:]]', :your_value, '[[:>:]]');
You need to use these weird-looking things, as they match word boundaries - preventing match for '2' to occur at '23' and '32'. )
The problem is that query is only the beginning of the troubles caused by using denormalized field. I'd suggest using either SET type (if the number of options is limited and low) or, way better, junction table to store the m-n relationships.
Use FIND_IN_SET() function
Try this:
SELECT * FROM bloques WHERE FIND_IN_SET(1, roles);
OR
SELECT * FROM bloques
WHERE CONCAT(',', roles, ',') LIKE '%,1,%';

MySQL Like multiple values

I have this MySQL query.
I have database fields with this contents
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
Why does this like query does not work?
I need the fields with either sports or pub or both?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
Faster way of doing this:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
is this:
WHERE interests REGEXP 'sports|pub'
Found this solution here: http://forums.mysql.com/read.php?10,392332,392950#msg-392950
More about REGEXP here: http://www.tutorialspoint.com/mysql/mysql-regexps.htm
The (a,b,c) list only works with in. For like, you have to use or:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
Why not you try REGEXP. Try it like this:
SELECT * FROM table WHERE interests REGEXP 'sports|pub'
You can also use REGEXP's synonym RLIKE as well.
For example:
SELECT *
FROM TABLE_NAME
WHERE COLNAME RLIKE 'REGEX1|REGEX2|REGEX3'
Don't forget to use parenthesis if you use this function after an AND parameter
Like this:
WHERE id=123 and(interests LIKE '%sports%' OR interests LIKE '%pub%')
Or if you need to match only the beginning of words:
WHERE interests LIKE 'sports%' OR interests LIKE 'pub%'
you can use the regexp caret matches:
WHERE interests REGEXP '^sports|^pub'
https://www.regular-expressions.info/anchors.html
Your query should be SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0
What I understand is that you store the interests in one field of your table, which is a misconception. You should definitively have an "interest" table.
Like #Alexis Dufrenoy proposed, the query could be:
SELECT * FROM `table` WHERE find_in_set('sports', interests)>0 OR find_in_set('pub', interests)>0
More information in the manual.
More work examples:
SELECT COUNT(email) as count FROM table1 t1
JOIN (
SELECT company_domains as emailext FROM table2 WHERE company = 'DELL'
) t2
ON t1.email LIKE CONCAT('%', emailext) WHERE t1.event='PC Global Conference';
Task was count participants at an event(s) with filter if email extension equal to multiple company domains.