i trying to fetch records from database as.
select * from emp_marks where sub_id all(2,4);
you can't use all with where clause, if you want to get all the records that have sub_id = 2 or 4 you can use:
select * from emp_marks where sub_id in (2,4)
If you want the records where sub_id is either 2 or 4, you need
SELECT * FROM emp_marks WHERE sub_id IN (2,4);
if you want to records where sub_id is both 2 and 4, you don't have to perform query at all ;-)
Related
I have a table with an id column and a source column.
I want to return only the source values that all ids share.
E.g. in the table below id 1,2,3 all share 10 and 20, but id 3 is missing the source value 30, so 30 is not valid and I want to return 10 and 20.
I'm using MySQL and want to put this in a stored procedure.
How do I do this?
id
source
1
10
1
20
1
30
2
10
2
20
2
30
3
10
3
20
You may use COUNT(DISTINCT) function as the following:
SELECT source FROM
table_name
GROUP BY source
HAVING COUNT(DISTINCT id)=(SELECT COUNT(DISTINCT id) FROM table_name)
To do this within a stored procedure:
CREATE PROCEDURE getSourceWithAllIds()
BEGIN
SELECT source FROM
table_name
GROUP BY source
HAVING COUNT(DISTINCT id)=(SELECT COUNT(DISTINCT id) FROM table_name);
END
The idea is to select the count of distinct id values for each source, which is done by COUNT(DISTINCT id)... GROUP BY source, then match this count with the distinct count of all id values existed in the table; HAVING COUNT(DISTINCT id)=(SELECT COUNT(DISTINCT id) FROM table_name).
If the two counts are equal, then the source have all the distinct ids existed in the table.
i.e. All distinct ids in the table are (1, 2, 3) count = 3, and distinct ids for a source =10 are (1, 2, 3) count=3. For source = 30, the distinct ids are (1, 2) count=2 so it will not be returned by the query (2<>3).
See a demo.
I apologize for the possible incorrectness in the presentation, I use a translator. Let's say there is a users table in which there is an id field. And there is a list that lists the id numbers and some of them are repeated. My query
select id, count(*)
from users
where id in (3, 10, 10, 10)
group by id;
returns the following 3 - 1, 10 - 1. And I would like to get 3 - 1, 10 - 3, and so on. Is it possible to get it somehow?
UPD.
The data in the list (3, 10, 10, 10) is just an example, the exact number of digits is not known because they are returned from another question.
You would need to use a join. You can put the values in a derived table for this:
select id, count(*)
from users u join
(select 3 as id union all
select 10 as id union all
select 10 as id union all
select 10 as id union all
) i
using(id)
group by id;
I have to get ID by two values.
So let's say when i call: id - 2 i get two values 7 and 6.
I want to call 7 and 6 to get value 2
i have table recipeingredient where i should find recipe_id 2 using ingredient_id 6 and 7
example:
SELECT * FROM recipeingredient WHERE recipe_id=2
returns: 6 7
but i want reversed.
i think it should be equivalent of:
SELECT * FROM recipeingredient WHERE ingredient_id=6 AND ingredient_id=7
should return: 2 but returns nothing
to sum up:
on my php web page, i have to select 2 elements and get 1 displayed value.
its TAG system. i cant use OR, i need to get value that has as value A so value B.
You can use aggregation and a HAVING clause to implement the filter, like :
SELECT recipe_id
FROM recipeingredient
GROUP BY recipe_id
HAVING
SUM(ingredient_id=6) > 0
AND SUM(ingredient_id=7) > 0
If you are looking for a recipe that contains the two ingredients
you could use IN
and chek for just 2 ingredient
SELECT id
FROM recipeingredient
WHERE ingredient_id IN (6 ,7)
group by id
having count(distinct ingredient_id ) = 2
You actually want this:
-- all recipes that could be prepared with 6 and/or 7
SELECT recipe_id
FROM recipeingredient
GROUP BY recipe_id
HAVING COUNT(CASE WHEN ingredient_id IN (6, 7) THEN 1 END) = COUNT(*)
Or this:
-- all recipes that could be prepared with exactly 6 and 7
SELECT recipe_id
FROM recipeingredient
GROUP BY recipe_id
HAVING COUNT(CASE WHEN ingredient_id IN (6, 7) THEN 1 END) = 2
AND COUNT(*) = 2
Both queries skip recipes that require an ingredient other than 6 and 7.
SELECT * FROM recipeingredient WHERE ingredient_id=6 OR ingredient_id=7
AND will show you results when both conditions are true. You should use OR to check for both values for same column. A row cannot have two values for same column at the same time.
How can an ingredient be at the same time be 6 and 7 (unless your data is non structural or deliminated non atomic values) thats why while using AND clause you received o/p as no rows selected.
Better option is to use IN or OR clause both are almost similar but IN would avoid usage of again and again writing as its a function with parameters and not an operator like OR
SELECT * FROM recipeingredient
WHERE ingredient_id IN (6,7)
--using IN clause
SELECT * FROM recipeingredient
WHERE ingredient_id=6 OR
ingredient_id=7
-- using OR
I am super new to access and having difficulty with a query. My table (tblpacks) is the following:
id user_id group_id quota_id
1 1 1 1
2 1 2 1
3 2 1 1
4 3 1 1
Ideally, what I now is to get hte number of unique users and groups for quota_id=1
The result will be:
total_users = 3
total_groups = 2
If you only wanted to count one field, there would by a simple solution, but since you want to count 2 separate fields, you in fact need at least 2 separate queries.
My answer is to use a UNION query as the source for counting. This UNION query returns the distinct user_id values (with Null as group_id) and the distinct group_id values (with Null as user_id). I omitted the DISTINCT keyword, because UNION (without ALL) does a DISTINCT query automatically. As the datatypes where not recognized correctly when using a constant Null field in the first SELECT statement of the UNION query, I added a third SELECT statement as the first one, which selects both fields from the table but returns no records:
SELECT Count(user_id) AS total_users, Count(group_id) AS total_groups
FROM (
SELECT user_id, group_id FROM tblpacks WHERE Yes=No
UNION
SELECT user_id, Null FROM tblpacks WHERE quota_id=1
UNION
SELECT Null, group_id FROM tblpacks WHERE quota_id=1
) AS qsub;
Below is my table, how can i get the item_id with selecting multiple item_subcategory_id.
For example i need to get the item_id with an item_subcategory_id of 86,51 and 1. I should only get the item_id 1 even though item_id 2 has an item_subcategory_id of 51
Please note that the number of item_subcategory_id is dynamic.
I tried the query
SELECT item_id
FROM item_specs
WHERE item_subcategory_id = 86 AND
item_subcategory_id=51 AND
item_subcategory_id = 1
but it displays an empty result
You can use the count and compare the result of count to the no of distinct item_subcategory_ids you provide in in() clause like in your case distinct item_subcategory_ids is 3 ,so this will result only those item_ids which has all of these provided item_subcategory_ids
select * from table
where item_subcategory_id in(1,51,86)
group by item_id
having count(distinct item_subcategory_id) =3
Use AND operator to combine your conditions
select item_category_id
from mytable
where item_id = 1 and item_subcategory_id in (86, 51, 1);