I want to write a query that title start with A or B
is this correct?
I dont want use OR
I want use it in mysql ,
select * from table where title like `[AB]%`
Use REGEXP instead of LIKE:
SELECT * FROM table
WHERE title REGEXP '^[AB]'
DEMO
Or just use a substring:
SELECT * FROM table
WHERE LEFT(title, 1) IN ('A', 'B');
you can do it like
select * from table where title like `A%` OR title like `B%`
another way is to use regular expression
select * from table where title REGEXP '^(A|B)';
This is correct way:
SELECT * FROM table WHERE title LIKE 'A%' or title LIKE 'B%';
What you wrote should work. or you can use,
select *
from table_name
where title LIKE 'A%' OR title LIKE 'B%'
Unfortunately, the LIKE operator in SQL only supports a limited syntax. It doesn't support a regex style syntax.
You have to do divide it into two expressions:
select * from table where (title like 'A%' or title like 'B%')
Note:
In this case the parenthesis are superfluous, but since OR has a lower precedence than AND I think it is good practice to routinely add parenthesis around ORexpressions in SQL.
Related
I have to display the names of jobs which contains letters m or p . does'nt matter the position of letters where they are at .they can be either at starting or middle or what ever position.
thank you
Hi you can use like query for this type result-
SELECT column_name FROM table_name WHERE job like ('%m%') OR job like ('%p%');
Try this one:
select name,job from employee where jobtitle like '%M%' or '%P%'
As an alternative to LIKE, we can use REGEXP here:
SELECT job
FROM jobs
WHERE job REGEXP 'm|p';
Demo
TRY This one
SELECT * FROM `TABLENAME` WHERE 'JOBNAME' LIKE '%p%' OR 'JOBNAME' LIKE '%m%'
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%'
I have a select statement like this:
Select * from A where name like 'a%' or name like 'b%' or name like 'j%' or name like ... etc
Is it possible to store a%, b%, j% in a table somewhere so I can more easily manage this list and convert my query to something like:
Select * from A where name like (Select * from StringPatternToMatch)
Try this:
SELECT * FROM A
JOIN StringPatternToMatch patt ON A.name LIKE '%' + patt.pattern + '%';
Replace patt.pattern with the name of the column in your StringPatternToMatch
You can do a regexp search instead.
select *
from A where name regexp '^[abjf]';
It's easier query to maintain than a ton of or'd likes.
demo here
'^[abjf]' means match the start of the string (^), followed by any of the characters in the list ([abjf]). It doesn't care what comes after that.
Just add more letters to the list if you find names starting with them.
Can you do a "SELECT * FROM TABLE WHERE attribute has last character which is 'A' or 'B'"
I see, that everyone who answered here suggest LIKE.
However, the following would be faster than LIKE, so I suggest you use it.
SELECT *
FROM t1
WHERE substring(attribute, -1)= 'A'
OR substring(attribute, -1)= 'B';
SQLFiddle
You can try:
SELECT * FROM TABLE WHERE attribute LIKE '%A' OR attribute LIKE '%B'
Try
SELECT * FROM TABLE WHERE attribute LIKE '%A' or attribute LIKE '%B'
Try this query:
SELECT * FROM TABLE1 WHERE attribute LIKE '%A' OR attribute LIKE '%B';
You can also use Mysql's RIGHT() function
SELECT
*
FROM
`table`
WHERE RIGHT(attribute, 1) = 'A'
OR RIGHT(attribute, 1) = 'B'
Small Fiddle Demo
Yes, it's possible. For instance you could:
SELECT * FROM table WHERE attribute LIKE '%[AB]'
Which would select all rows in table with field 'attribute' ending in either 'A' or 'B'
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.