I have table emloyee with following data.
Id Name Country
1 John USA
2 Smith USA
3 Jack IND
4 Lory UK
5 Miller USA
I want to get result by calling stored procedure like this
call getEmployeeDetailsByCountry('IND,UK');
Result:
Id Name Country
3 Jack IND
4 Lory UK
My Procedure is
select * from employee
where if( (LOCATE(',','IND,USD')>0),
Country in (concat('\'',REPLACE('IND,USD', ',', '\',\''),'\''))
, Country in ('IND,USD'));
Here it replaces 'IND,USD' to 'IND','USD'.
But result is no rows.... Can anyone help me to find..... thank you
Use FIND_IN_SET instead of IN
SELECT *
FROM product
WHERE FIND_IN_SET(country, 'IND,USD');
Try this i think this will solve your problem
Why dont you use a query with a simple IN statement in the where clause?
SELECT * FROM employee WHERE Country IN ('IND', 'UK');
Related
I have a data set with a nametsql field
jimmy
jimmy
leti
joe
joe
joe
joe
I am trying to find the average length of unique names. I.e for this data set the average will be 4 since the unique names are joe, leti, jimmy and that has an average length of 4.
I tried SELECT avg(len(name)) FROM table GROUP BY name
but that returns
5
4
3
and im not sure why. How can i structure my query to get the average length of my name?
You could use subquery to get distinct names and then calculate average length:
SELECT avg(len(name)) FROM (SELECT DISTINCT name FROM table) s
ID NAME Email
1 John john#example.com
2 Mathew mathew#example.com
3 John jon#example.com
4 Johnson johns#example.com
5 Peter pete#example.com
How can I create a query that will return
1 John john#example.com
3 John jon#example.com
4 Johnson johns#example.com
Ansers like that for Find rows that have the same value on a column in MySQL returns rows with same value. Here I need similar values
Get all the records with similar email first and then only print those records that match the inner condition.
select * from table_name where email in (
select email from table_name
group by email
having count(*)>1
)
Tested and working fine.
id points year country
-----------------------------------
1 45 1998 Mexico
2 45 2000 Germany
3 47 2010 Russia
4 45 1970 China
5 49 2010 Austria
I wonder how can I take row results considering only 2 items from country column. For example only records where country is Germany and Mexico. When I try to get results where only 1 country is criterion the thing is easy:
SELECT * FROM List WHERE Country='Mexico';
the result is:
id points year country
-----------------------------------
1 45 1998 Mexico
but when I try to get results where 2 country items are criteria problems start. I tried:
SELECT * FROM List WHERE country='Mexico' AND Country='Germany';
SELECT * FROM List WHERE country='Mexico' AND 'Germany';
SELECT * FROM List WHERE country='Mexico','Germany';
SELECT * FROM List WHERE country='Mexico'AND WHERE country='Germany';
but no desired result:
id points year country
-----------------------------------
1 45 1998 Mexico
2 45 2000 Germany
I understand that maybe I committed logical error because there is no single record where country is Mexico and Germany at same time, and sql maybe understands claim exactly that way, but, how to write correctly in sql language: Give me results for records where countries are Mexico and Germany. Thanks.
You are looking for IN operator
SELECT * FROM List WHERE Country in ('Mexico','Germany');
Just use OR.
So instead of
SELECT * FROM List WHERE country='Mexico' AND Country='Germany';
it would be
SELECT * FROM List WHERE country='Mexico' OR country='Germany';
IN is also a good function to use, especially if you've got multiple values that you want to check against but that's been covered in the other answers.
You need to use or or in, you have been using and and asking mysql to find a row where country is both Mexico and Germany which is not true.
SELECT * FROM List WHERE Country in ('Mexico','Germany');
try this:
SELECT * FROM List WHERE country='Mexico' OR Country='Germany';
SQL is using logic. Natural language is not.
When you say that you want the results for a list of countries you need to specify so. This request corresponds to an logical or. Since the name can be one or the other, both are correct.
SELECT * FROM List WHERE Country = 'Mexico' OR Country = 'Germany'
To prevent further mistakes like these, I recommend that you look up logical operations in the docs (they are very good). MySQL or the PostGres, both should be fine.
I have two tables, follow and followed. I want to get all the rows in the follow table such that follow.screen != followed.following_screen_name.
follow table
ID screen_name
-----------------
1 eddie
2 jason
3 omar
4 jonathan
5 jack
followed table
ID my_screen_name following_screen_name
-------------------------------------------
1 john eddie
2 kenny eddie
3 kenny omar
4 john jason
5 john omar
Query I tried which didn't work
SELECT follow.screen_name from follow, followed where followed.my_screen_name='john'
AND follow.screen_name != followed.following_screen_name
Expected results
ID screen_name
-----------------
1 jonathan
2 jack
you can get this by doing a LEFT JOIN
SELECT F.screen_name FROM follow F
LEFT JOIN followed FD
on F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
WHERE FD.my_screen_name IS NULL
and FD.following_screen_name IS NULL
Another way is to use NOT EXISTS, get all rows that exists in followed and do NOT EXISTS clause to get desired result.
SELECT F.screen_name FROM follow F
WHERE NOT EXISTS
(
SELECT 1 FROM followed FD
WHERE F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
)
There are plenty of ways to solve this, but common to all is that you need to compare the follow.screen_name to both followed.my_screen_name and followed.following_screen__name.
One way is to use NOT IN with a UNION:
select screen_name
from follow
where screen_name not in (
select following_screen_name
from followed
where following_screen_name is not null
union all
select my_screen_name
from followed
where my_screen_name is not null
)
While this approach is nice for clarity, it may not be as good for performance as using a left join or not exists.
A nice place to pick up mysql syntax and logic is here.
But try this code, it selects every row where the screen_name is not identical to anything produced in the next two queries:
SELECT * from follow WHERE screen_name
not in (select screen_name from followed)
AND not in (select followed_screen_name from followed);
The last two queries would look this and the WHERE filters all of the rows out with screen names identical to the fields below.
my_screen_name following_screen_name
-------------------------------------
john eddie
kenny eddie
kenny omar
john jason
john omar
I have 2 tables in Access with these fields
Student:
ID(PK) Name Family Tel
Lesson:
ID StudentRef(FK(Student)) Name Score
Imagine we have these records
Student :
1 Tom Allen 09370045230
2 Jim leman 09378031380
Lesson:
1 1 Math 18
2 1 Geography 20
3 2 Economic 15
4 2 Math 12
How can I write a query that result will be this (2 fields)?
Tom Math : 18 , Geography 20
Jim Economic :15 , Math :12
SELECT s.Name, l.Name, l.Score
INNER JOIN tbl_lessons as l ON s.student_id = l.student_id
FROM tbl_students as s
That won't give you your formatting, but it'll get you the data.
The most tricky part of your problem is how to aggregate strings in your sub-query. MS Access does not have any aggregation function that is applicable to strings (except for Count()) and there is no way to define your own function. This means you can't just get the desired "subject:score , subject:score" concanetation. As long as you can go without you can easily take the solution provided in the answer by Corith Malin.