Need MYSQL Query to select specific child rows only - mysql

FX., Given tables for baskets and fruits, looking for a query that returns the basket_id that contains ONLY the two specific fruit.
EX
table: fruits
basket_id | fruit
_________________
1 | apple
1 | orange
1 | pear
2 | apple
2 | orange
3 | apple
3 | pear
Given the above table, I'm looking for a query that returns the basket_id that ONLY contains the fruits "apple" and "orange", which would be row 2 only. Basket id 1 should be excluded because it also contains "pear".
Help with this would be most appreciated.

Please note for the example table, I added one more basket which is basket_id = 4, that has two apples only, and it will be a strong test sample for the query.
WITH ABC --sample table
AS
(
SELECT 1 as basket_id, 'Apple' as fruit
UNION ALL
SELECT 1 as basket_id, 'Orange' as fruit
UNION ALL
SELECT 1 as basket_id, 'Pear' as fruit
UNION ALL
SELECT 2 as basket_id, 'Apple' as fruit
UNION ALL
SELECT 2 as basket_id, 'Orange' as fruit
UNION ALL
SELECT 3 as basket_id, 'Apple' as fruit
UNION ALL
SELECT 3 as basket_id, 'Pear' as fruit
UNION ALL
SELECT 4 as basket_id, 'Apple' as fruit
UNION ALL
SELECT 4 as basket_id, 'Apple' as fruit
)
--main query:
SELECT basket_id FROM
(
SELECT *,CASE WHEN fruit in ('Apple','Orange') THEN 1 ELSE 0 END AS row_check
FROM ABC
) as A
GROUP BY basket_id
HAVING COUNT(DISTINCT row_check) =1 -- make sure there is no other fruit
AND COUNT(DISTINCT fruit) >1 -- make sure there are at least one apple and one orange
output: basket_id 2

select
f.basket_id,
(select count(1) from fruits where basket_id = f.basket_id) as fruits_in_basket,
(select count(1) from fruits where basket_id = f.basket_id and fruit in ("apple", "orange")) as specific_fruits_in_basket
from
fruits as f
group by f.basket_id
having fruits_in_basket = specific_fruits_in_basket
Here is a fiddle: http://sqlfiddle.com/#!9/f4ae3c/37/0

SELECT DISTINCT basket_id
FROM fruits f
WHERE
NOT EXISTS (SELECT 1 FROM fruits f1 WHERE f.basket_id=f1.basket_id AND f1.fruit NOT IN ('apple', 'orange'))
AND
(SELECT count(*) FROM fruits f2 WHERE f.basket_id=f2.basket_id AND f2.fruit='apple')=1
AND
(SELECT count(*) FROM fruits f3 WHERE f.basket_id=f3.basket_id AND f3.fruit='orange')=1;
I have tested the query on your example. Demo

If it is possible to use the list of items as an ordered list you want to select like 'apple,orange', then you can use following.
SELECT f.basket_id, f.fruit
FROM fruits f
INNER JOIN (
SELECT basket_id, GROUP_CONCAT(fruit order by fruit) as a
FROM fruits
GROUP BY basket_id
HAVING a = 'apple,orange'
) f1 ON f.basket_id = f1.basket_id
Here is the fiddle.

SELECT DISTINCT(basket_id)
FROM basket
where fruit IN ('apple', 'orange');

Related

Select the first occurrence of each id

I have table like this
id
name
1
Apple
1
Banana
1
Guava
2
Cassava
2
Carrot
2
Potato
3
Almond
3
Soybeans
3
Peanuts
I want to select only the first one from each id
id
name
1
Apple
2
Cassava
3
Almond
What's the query like?
you can try this way
SELECT id, name FROM table_name GROUP BY id ORDER BY id ASC;
You can achieve your goal by using row_number(). You can check my query in db-fiddle: https://www.db-fiddle.com/f/g4MrUTTTGDFfqjAKYnkFxn/1
WITH CTE as
(
SELECT id, name, ROW_NUMBER() OVER (ORDER BY null) as rowNumber FROM Fruits
)
SELECT id, name FROM CTE WHERE rowNumber IN(SELECT min(rowNumber) FROM CTE Group by id)

Group by values in one column

I have a table with this data in it:
ID Name Color
1 Kyle Blue
1 Susan Orange
1 Steven Orange
2 Susan Blue
I want to use a query like this:
Select * from table group by ID, Top1(Name), Top1(Color)
So I get these results:
ID Name Color
1 Kyle Blue
2 Susan Blue
I don't care if it's Kyle Blue, or Steven Orange as long as the color matches the name.
Eg
SELECT x.*
FROM my_table x
JOIN (SELECT id, MIN(name) min_name FROM my_table GROUP BY id) y
ON y.id = x.id
AND y.min_name = x.name;
select *
from (
select *,
row_number() over (partition by Name order by Name) as row_number
from table
) as rows
where row_number = 1

sql statement select constraints

I have a table like the following
table_fruit:
account_id fruit
1 apple
1 banana
2 apple
2 orange
2 banana
3 apple
I'm trying to make a list of everyone that has an apple and no other fruit
This seems like an easy idea but its passing me right now...
You can use not exists as
select * from table_fruit t1
where t1.fruit = 'apple'
and not exists(
select 1 from table_fruit t2
where t2.account_id = t1.account_id
and t2.fruit <> 'apple'
);
try this:
Select * From table_fruit Where fruit = 'apple'
or
Select * From table_fruit Where fruit like '%apple%'

Add Results of Three Subqueries

I have this table:
id item_name_1 quantity 1 item_name_2 quantity_2 item_name_3 quantity_3
-----------------------------------------------------------------------------------
1 Apple 2 Pear 3 Orange 5
2 Pear 1 Apple 4
3 Orange 6
4 Apple 1 Pear 2 Orange 3
I want this result:
item total
--------------
Apple 7
Pear 6
Orange 14
I tried this:
SELECT
(SELECT item_name_1, SUM(quantity_1) AS count FROM table1
GROUP BY item_name_1) AS item,
(SELECT item_name_2, SUM(quantity_2) AS count FROM table1
GROUP BY item_name_2) AS item,
(SELECT item_name_3, SUM(quantity_3) AS count FROM table1
GROUP BY item_name_3) AS item,
SUM(count) AS total
FROM table1
GROUP BY item;
Error Code: 1241. Operand should contain 1 column(s)
Any suggestions?
You're selecting two columns in your sub-queries when there can only be one column.
Something like this will get you what you are looking for:
SELECT item_name, SUM(quantity) AS quantity
FROM
(SELECT item_name_1 AS item_name, SUM(quantity_1) AS quantity
FROM table1
GROUP BY item_name_1
UNION ALL
SELECT item_name_2 AS item_name, SUM(quantity_2) AS quantity
FROM table1
GROUP BY item_name_2
UNION ALL
SELECT item_name_3 AS item_name, SUM(quantity_3) AS quantity
FROM table1
GROUP BY item_name_3) AS ABB1
GROUP BY item_name;
I would try it like this.
select t.item_name_1, s1+s2+s3
from
(
select * From
(
select sum(quantity_1) as s1, item_name_1
from
table1
group by item_name_1
) t1
full outer join
(
select sum(quantity_2) as s2, item_name_2
from
table1
group by item_name_2
) t2 on t1.item_name_1 = t2.item_name_2
full outer join
(
select sum(quantity_3) as s3, item_name_3
from
table1
group by item_name_3
) t3 on t2.item_name_2 = t3.item_name_3
) t

SQL Counting sums of rows

I have 2 tables.
Table 1: Fruits
id (int, autoincreasing, primary key)
some other junk
Table 2: Customers
has a 'fruit' column, this column contains an id from the fruit table.
i want to query all the customers, and come up with a list of all the fruit ID's and the number of times they are in use.
So this set up:
Fruits has
id name
1 orange
2 banana
3 apple
Customers has 6 rows like:
id fruit
1 1
2 1
3 2
4 2
5 2
6 3
Trying to write a query that Will give me:
fruit id purchase count
1 2
2 4
3 1
Try this
SELECT f.id as fruit_id, COUNT(1) AS purchase_count
FROM FRUITS f LEFT JOIN CUSTOMERS c
ON F.ID = c.fruit
GROUP BY f.id
Should just be a simple aggregate...
SELECT fruit_id, COUNT(fruit_id) AS purchase_count
FROM customers
GROUP BY fruit_id
ORDER BY fruit_id ASC;
untested
SELECT id, SUM(fruit) as "fruit id", "purchase count"
FROM Customers
GROUP BY id;