Mysql - How to get ranking of choice base on table value - mysql

I have many users that can select their choice from rank 1-4.
The table look like this:
username rank1 rank2 rank3 rank4
Perter Apple Orange Grape Melon
Adam Orange Melon Apple Grape
James Melon Grape Orange Apple
How do I get a table like this in php mysql
username Apple Orange Grape Melon
Peter 1st-choice 2nd-choice 3rd-choice 4th-choice
Adam 3rd-choice 1st-choice 4th-choice 2nd-choice
Jame 4th-choice 3rd-choice 2nd-choice 1st-choice

Related

List customers that has order in the exact following order

i have 1 table transaction
here the table transaction
order_id
Customer_id
fruit_order
0001
aaa
Strawberry
0002
bbb
apple
0003
aaa
apple
0004
aaa
apple
0005
ccc
strawberry
0006
aaa
mango
0007
ccc
strawberry
0008
ccc
apple
0009
aaa
mango
0010
ccc
mango
0011
ccc
apple
how can i write query to get List customers that has transactions in the exact following order of fruit: Strawberry, apple, then Mango ?
(If customer have transactions in the same fruit multiple times in sequential order then it counts as one for example: ​Strawberry, apple, apple, mango, apple will be Strawberry, apple, mango, apple.)
anyone can help ?
Thank you
A simple method is to keep only those rows where the fruit changes. Then use group_concat() and like:
select customer_id
from (select t.*,
lag(fruit_order) over (partition by customer_id order by order_id) as prev_fruit_order
from t
) t
where not prev_fruit_order <=> fruit_order
group by customer_id
having group_concat(fruit_order order by order_id) like 'strawberry,apple,mango')

How to add a category to the results of a query based on the output?

Extending this question for a more general case:
I have a table called fruits with a column fruit that has three possible values: apple, orange, and mango. I used the following query to get the counts of each fruit by date:
SELECT date, fruit, COUNT(id) AS count
FROM fruits
WHERE date BETWEEN '2020-10-02' AND '2020-10-05'
GROUP BY date, fruit
that gives the following results:
date fruit count
--------------------------------
2020-10-02 apple 3
2020-10-02 orange 5
2020-10-03 orange 23
2020-10-03 mango 1
2020-10-04 mango 9
2020-10-05 apple 10
2020-10-05 orange 7
2020-10-05 mango 6
As you can see, not every date contains each type of fruit.
Is it possible, and if so, how, to add the missing fruit by each date to the above results with a default count=0, as shown below?
date fruit count
--------------------------------
2020-10-02 apple 3
2020-10-02 orange 5
2020-10-02 mango 0
2020-10-03 orange 23
2020-10-03 mango 1
2020-10-03 apple 0
2020-10-04 mango 9
2020-10-04 apple 0
2020-10-04 orange 0
2020-10-05 apple 10
2020-10-05 orange 7
2020-10-05 mango 6
Use a cross join ot generate the row and then a left join to bring in the results:
select d.date, f.fruit, coalesce(ff.count, 0) as count
from (select distinct date from fruits) d cross join
(select distinct fruit from fruits) f left join
fruits ff
on ff.date = d.date and ff.fruit = f.fruit;

Trying to limit the amount be shown in a mysql result

I have a table that stores id, item, food type, image and price
I want to limit the result to one of each type sold in the store e.g. dog food, cat food, dvd in descending order
Example of what is held in mysql database
table: Nunca
id item type image price
=========================================================
1 Pedigree Pouches dog food img1 $4.99
2 Cesar Classic Selection dog food img2 $3.99
3 Meaty Strips Chicken dog food img3 $2.99
4 Jelly Fish Selection cat food img4 $1.99
5 Bananas fruit img5 $2.84
6 Celery vegetables img6 $5.99
7 Apple fruit img7 $0.95
8 Pineapple Juice Soft drink img8 $0.99
9 Oranges fruit img9 $1.99
10 Cabbage vegetables img10 $0.99
11 Suicide Squad dvd img11 $12.99
The final result should look like this from the query
id item type image price
==========================================================
11 Suicide Squad dvd img11 $12.99
10 Cabbage vegetables img10 $0.99
9 Oranges fruit img9 $1.99
8 Pineapple Juice Soft drink img8 $0.99
4 Jelly Fish Selection cat food img4 $1.99
3 Meaty Strips Chicken dog food img3 $2.99
I have tried various mysql queries, but to no avail
select * from t where
id in(select max(t.id) id from t group by t.type)
order by id desc
I guess there can be window or join alternatives but this one seemed fitting for this purpose.
A typical method uses a correlated subquery:
select n.*
from nunca n
where n.id = (select max(n2.id) from nunca n2 where n2.type = n.type);
This will choose the last row of each type.

how to categorize in 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

Group by using local storage in HTML5

I am creating my first mobile application using HTML5 and JqueryMobile. Storing all datas in localstorage. Anyone can suggest me the easy way to retrieve grouped data count from following resultset,
1 mango
2 orange
3 mango
4 apple
5 mango
6 apple
7 orange
8 orange
9 apple
10 mango
Expected result
mango 4
orange 3
apple 3
you can do the same using query lik
SELECT Fruit_Name, COUNT(*) as "Fruit Count"
FROM Fruits
GROUP BY Fruit_Name;
Selecting data and rendering in HTML5 is explained here.