Like case in MySQL without using OR or Union - mysql

I want to fetch all the employee's name which are started from A or B or C.
Also i don't want to use Union or OR conditions.
NOTE: I don't want these two solution
CASE 1. (like '%A' or like'%B' or like '%C').
CASE 2. fetching separately and using union combining.
I have created sample data here sample link Please find it.

Try this
Select * from employee where substring(emp_name, 1, 1) in ('A', 'B', 'C');

You can try the following:
select * from employee where emp_name REGEXP '^[a-c]'
SQLFIDDLE DEMO

Use this
select * from employee where emp_name REGEXP '^[abc]'
Fiddle Example

Another way you can use or
select * from employee where emp_name REGEXP '^[a or b or c]'
SQL Fiddle

You can use the Regular expression for this. The solution to your question is
select * from employee where emp_name REGEXP '^[ABC]'
SQL FIDDLE DEMO

Related

Write a SQL query to list all employee names and jobs, whose job title includes M or P?

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

get number between two string by using substring

I m trying to get number in between "sims_7009_alaira", i want 7009.
SELECT sno,dbase, SUBSTRING_INDEX(dbase, 'sims_', -1)temp
FROM school
How should i do that in SQL
Give this a try:
select substring_index(SUBSTRING_INDEX(dbase, '_', 2),'_',-1) from school;
Check this here:
SQL Fiddle
Just use substring_index() two times:
SELECT sno, dbase, substring_index(substring_index(dbase, 'sims_', -1), '_alaira', 1) as number FROM school
Do this instead:
SELECT sno,dbase, SUBSTRING_INDEX(SUBSTRING_INDEX(dbase, "_", 2),'_',-1) temp
FROM school;
For more insight see this.

how to write a select query in mysql?

I have a table with column named place which holds data like 1,2,3(comma separated values). I need to get the details of the table where place = 1
select * from schools where place = 1
Try this:
SELECT * FROM schools WHERE FIND_IN_SET('1',place);
For more information about FIND_IN_SET refer this: FIND_IN_SET
Try this,
select * from schools where place LIKE '%1%'

Select substring group by

I have data in column which I want to select with substring_index and group by the result of substring. Is it possible to make in one query?:
Example
code:
R0001.10
R0001.20
R0002.10
R0002.30
If use
SELECT SUBSTRING_INDEX(code, '.', 1) FROM products;
It goes like this:
R0001
R0001
R0002
R0002
But when I use
SELECT SUBSTRING_INDEX(code, '.', 1) FROM products GROUP BY code;
It gave some strange result
01
01
010210000
0103020
etc.
There is no issue using GROUP BY clause check SQL Fiddle. Might be other issue.

Finding language in string (doing substring?) with MySQL

I have a table with languages which s_name value looks like this:
'en_UK'
'en_US'
'de_CH'
'de_AT'
I want to get all the distinct languages, without the country part. So for example, in case I just had those of the example, I would need to get:
en
de
What would be the best way of doing so?
I have this right now:
SELECT DISTINCT SUBSTRING(name,1,2)
FROM my_languages
Try this:
SELECT DISTINCT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
OR
SELECT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
GROUP BY langName
Check this link MySQL STRING Functions
Here is a simple way:
select distinct left(s_name, 2)
from t
This assumes the language name is the left two characters.
This will work in Oracle as well as in MySql I think:
SELECT SUBSTR('en_UK',INSTR('en_UK','_')+1) country FROM dual
/