how to categorize in mysql - mysql

just think I have a table like shown below
**id** **product**
2 chocolate
1 chocolate
2 pepsi
3 fanta
2 pepsi
4 chocolate
5 chips
3 pizza
1 coke
2 chips
6 burger
7 sprite
0 pepsi
and want to arrange the above table in the manner shown below using only mysql
**id** **product**
0 pepsi
1 chocolate, coke
2 chocolate,fanta,chips
3 fanta,pizza
4 chocolate
5 chips
6 burger
7 sprite
how do I do it?

select id, group_concat(distinct product) as products
from your_table
group by id
order by id

Related

MySQL query - find name of beverages with both size

I have a table called beverages. It has the attributes name, id, and size.
How would I make a query that finds all the drinks with both size "maxi" and size "medium"
In other words. I have to find the beverages with the same name that have both sizes, as each tuple can only have one size.
So you have a table:
CREATE TABLE beverages
(id INT,
NAME VARCHAR(50),
size VARCHAR(50));
And a sample data like this:
id
name
size
1
orange
medium
1
orange
medium
1
orange
maxi
1
orange
maxi
2
soda
medium
3
cola
maxi
4
lemon tea
medium
5
soy milk
medium
5
soy milk
medium
5
soy milk
medium
5
soy milk
maxi
5
soy milk
maxi
6
coffee
maxi
7
chocolate
maxi
And you want to find any drinks that have both size so you do a query like:
SELECT id, name, GROUP_CONCAT(DISTINCT size), COUNT(DISTINCT size)
FROM beverages
GROUP BY id, name
Whereby this query returns:
id
name
group_concat(size)
count(*)
1
orange
medium,maxi
2
2
soda
medium
1
3
cola
maxi
1
4
lemon tea
medium
1
5
soy milk
medium,maxi
2
6
coffee
maxi
1
7
chocolate
maxi
1
Now you found drinks where their GROUP_CONCAT(DISTINCT size) return more than one result and COUNT(DISTINCT size) return two.
Demo fiddle

how to display result with order by based parent and childs in one table

I got problem with how to display query result based by parent and child on one table
data for example:
ID
CATEGORY
PARENT
ROOT
1
FOOD & DRINK
0
NULL
2
CLOTHES
0
NULL
3
FRUIT
1
1
4
DESERT
1
1
5
PANTS
2
2
6
T-SHIRT
2
2
7
APPLE
3
1
8
BANANA
3
1
9
ICE CREAM
4
1
10
SHORTS
5
2
11
LONG T-SHIRT
6
2
and I want to sort and display to this:
ID
CATEGORY
PARENT
ROOT
2
CLOTHES
0
NULL
5
PANTS
2
2
10
SHORTS
5
2
6
T-SHIRT
2
2
11
LONG T-SHIRT
6
2
1
FOOD & DRINK
0
NULL
4
DESERT
1
1
9
ICE CREAM
4
1
3
FRUIT
1
1
7
APPLE
3
1
8
BANANA
3
1
is there any query script to display like above ?
here my code and have no luck
select
id, category, parent, root
from
tbl_menu
order by
coalesce(root,parent,id),
category
and thanks for help :)
I understand that you want to order the tree nodes depth-first. One option uses a recursive query to generate the path to each node, that you can then use for ordering:
with recursive cte as (
select t.*, cast(id as char) as path from mytable t where root is null
union all
select t.*, concat(c.path, '/', t.id)
from cte c
inner join mytable t on t.parent = c.id
)
select *
from cte
order by path

MYSQL - How can I get this left join to work?

SELECT
*
FROM
food_types ft
LEFT JOIN
member_has_food_types mhft ON (ft.food_id = mhft.food_id)
WHERE mhft.member_id = 3230
Okay, so here are the database tables
Food Types:
FOOD_ID | FOOD_TYPE | STATE
1 APPLE 1
2 ORANGE 1
3 BANANA 1
4 CAKE 1
5 BACON 1
member_has_food_types:
FOOD_ID | MEMBER_ID
1 3230
2 3230
4 3230
5 3230
So because member_has_food_types does not contain a number 3 food, then the report comes back like:
FOOD_ID | FOOD_TYPE | STATE | FOOD_ID | MEMBER_ID
1 APPLE 1 1 3230
2 ORANGE 1 2 3230
4 CAKE 1 4 3230
5 BACON 1 5 3230
But I want it to include a NULL like this
FOOD_ID | FOOD_TYPE | STATE | FOOD_ID | MEMBER_ID
1 APPLE 1 1 3230
2 ORANGE 1 2 3230
3 BANANA 1 NULL NULL
4 CAKE 1 4 3230
5 BACON 1 5 3230
My understanding is its not showing a NULL for those fields, because im joining on food_id. So is there any clever way I can get around this with a sub query??
move the filter to the join
SELECT * FROM food_types ft
LEFT JOIN member_has_food_types mhft ON ft.food_id = mhft.food_id
AND mhft.member_id = 3230

Create custom column in SQL Query

I have a table like this:
ITEM PICTURE CATEGORY STOCK
1 Mickey Shirt 3
2 Mickey Jacket 2
3 Donald Jacket 0
4 Donald Shirt 6
5 Donald Hoodie 1
6 Mickey Hoodie 3
7 Goofy Shirt 1
8 Goofy Hoodie 2
9 Goofy Jacket 3
The ITEM column is auto increment
I need an output like this:
PICTURE HOODIE JACKET SHIRT
Donald 1 0 6
Goofy 2 3 1
Mickey 3 2 3
Any idea how I can produce that output? Thank you
You can use CASE expression.
Query
select PICTURE,
sum(case CATEGORY when 'Hoodie' then STOCK else 0 end) as `HOODIE`,
sum(case CATEGORY when 'Jacket' then STOCK else 0 end) as `JACKET`,
sum(case CATEGORY when 'Shirt' then STOCK else 0 end) as `SHIRT`
from your_table_name
group by PICTURE;

SQLite COUNT and selecting from multiple tables

Consider the database (made up for example):
Table 'owner'
id name
1 john
2 andrew
3 peter
Table 'duck'
id name ownerID
1 duck1 1
2 duck2 1
3 duck3 1
4 duck4 2
5 duck5 2
6 duck6 3
7 duck7 3
8 duck8 1
9 duck9 3
10 duck10 3
11 duck11 1
12 duck12 2
Table 'food'
id name type duckID
1 beef meat 4
2 grass veg 8
3 lemon fruit 5
4 apple fruit 3
5 pizza snack 7
I wish to write some SQL that for each OWNER, COUNT the number of ducks which eat some kind of food. For example for owner John, there would be 2 ducks (ducks 3 and 8).
So far I am writing this code:
select owner.name, count(duck.ownerID)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id;
I am getting the result:
Peter | 5
Any suggestions are appreciated.
This is done with an group by clause:
select owner.name, food.name, count(duck.id)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id
group by owner.name, food.name;
This query gives you one row for each combination of owner name and food name with the number of the owner's ducks eating this food.