How do I pivot MySQL INNER JOIN results in PHP? - php

I have the following 2 MySQL tables:
players:
| id | name |
|----|---------|
| 1 | Player1 |
| 2 | Player2 |
| 3 | Player3 |
scores:
| key | id | round | score |
|-----|----|-------|-------|
| 1 | 1 | Rd1 | 20 |
| 2 | 1 | Rd2 | 22 |
| 3 | 1 | Rd3 | 19 |
| 4 | 2 | Rd1 | 18 |
| 5 | 2 | Rd2 | 23 |
| 6 | 2 | Rd3 | 19 |
where scores.id=players.id
I will have upwards of 90 players in my 'players' table, what's the best way to query this and insert it into an HTML table to make it easier to view? I'm hoping to have an output similar to this:
| Player | Round 1 | Round 2 | Round 3 |
|---------|---------|---------|---------|
| Player1 | 20 | 22 | 19 |
| Player2 | 18 | 23 | 19 |
This is my first attempt at normalizing data in tables. Am I going to have to do number of cases? I'm not sure what the best way to pivot the data is with an INNER JOIN.

This is my solution, hope it helps :
SELECT
name as Player,
SUM(CASE WHEN (s.round='Rd1') THEN s.score ELSE 0 END) AS Round1,
SUM(CASE WHEN (s.round='Rd2') THEN s.score ELSE 0 END) AS Round2,
SUM(CASE WHEN (s.round='Rd3') THEN s.score ELSE 0 END) AS Round3
FROM
players p
JOIN scores s
on s.id=p.id
GROUP BY
name
This will output :
| Player | Round1 | Round2 | Round3 |
|---------|---------|---------|---------|
| Player1 | 20 | 22 | 19 |
| Player2 | 18 | 23 | 19 |
This Fiddle for you to test!

I have a sligthly alternative solution which uses subqueries with the following benefit that players with no no score gets listed too!!
SELECT
p.name,
ifnull((select score from scores where id = p.id and round='Rd1' limit 1), 0) as Round1,
ifnull((select score from scores where id = p.id and round='Rd2' limit 1), 0) as Round2,
ifnull((select score from scores where id = p.id and round='Rd3' limit 1), 0) as Round3
FROM players p
GROUP BY p.name, p.id

Related

sql ranking with custom ties

Hello can you help me get the rank with custom ties?
i have a table of Scores stores all the scores given by the judges.
+----+----------+-------------+--------+
| Id | judge_id |performer_id | score |
+----+----------+-------------+--------+
| 1 | 1 | 1 | 98 |
| 2 | 1 | 2 | 98 |
| 3 | 1 | 3 | 94 |
| 4 | 1 | 4 | 96 |
| 5 | 2 | 1 | 93 |
| 6 | 2 | 2 | 80 |
+----+----------+-------------+--------+
heres what the code i have searched.
SELECT
id
, judge_id
, performer_id
, score
, FIND_IN_SET(
score
, (SELECT
GROUP_CONCAT(DISTINCT score ORDER BY score DESC)
FROM
scores
WHERE
judge_id = 1
)
) AS rank
FROM
scores
WHERE
judge_id = 1
ORDER BY rank ASC
and the output of this is:
+----+----------+-------------+--------+------+
| Id | judge_id |performer_id | score | rank |
+----+----------+-------------+--------+------+
| 1 | 1 | 1 | 98 | 1 |
| 2 | 1 | 2 | 98 | 1 |
| 3 | 1 | 4 | 96 | 3 |
| 4 | 1 | 3 | 94 | 4 |
+----+----------+-------------+--------+------+
it is working but the output is not what i want.
i want to get the ranking and ties like this.
+----+----------+-------------+--------+------+
| Id | judge_id |performer_id | score | rank |
+----+----------+-------------+--------+------+
| 1 | 1 | 1 | 98 | 1.5 |
| 2 | 1 | 2 | 98 | 1.5 |
| 3 | 1 | 4 | 96 | 3 |
| 4 | 1 | 3 | 94 | 4 |
+----+----------+-------------+--------+------+
where get all the rank of the tie then divide it by how many performer ties in the rank.
ex.
performer 1 score 98 rank 1
performer 2 score 98 rank 1
suppose that performer 2 should get rank 2
i want to compute it like
1+2 = 3 then divide it by 2 since 2 performers are tie in rank 1
1=2 = 3 / 2
answer is 1.5
im sorry for my english
but please can any one help me? im stuck at this problem.
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(performer_id SERIAL PRIMARY KEY
,score INT NOT NULL
);
INSERT INTO my_table VALUES
(1,98),
(2,98),
(3,94),
(4,96);
SELECT x.*
, y.rank
FROM my_table x
JOIN
( SELECT score
, SUM(i)/COUNT(*) rank
FROM
( SELECT score
, #i:=#i+1 i
FROM my_table x
ORDER
BY score DESC
, performer_id
) a
JOIN
( SELECT #i:=0 ) vars
GROUP
BY score
) y
ON y.score = x.score;
+--------------+-------+--------+
| performer_id | score | rank |
+--------------+-------+--------+
| 1 | 98 | 1.5000 |
| 2 | 98 | 1.5000 |
| 3 | 94 | 4.0000 |
| 4 | 96 | 3.0000 |
+--------------+-------+--------+
Note: For newer versions of MySQL, you would use Windowing functions/CTE (I'm not really sure of the correct terminology). I've written this for older versions, although MySQL can have issues when initializing variables this way. If that's a problem, here's another (slightly 'hackier' - and theoretically incorrect, but practically fine) way of initialising the variable...
SELECT x.*
, y.rank
FROM my_table x
JOIN
( SELECT score
, SUM(i)/COUNT(*) rank
FROM
( SELECT score
, #j:=#j+1 i
FROM my_table x
JOIN ( SELECT #j:=0 ) vars
ORDER
BY score DESC
, performer_id
) a
GROUP
BY score
) y
ON y.score = x.score;
+--------------+-------+------+
| performer_id | score | rank |
+--------------+-------+------+
| 1 | 98 | 1.5 |
| 2 | 98 | 1.5 |
| 3 | 94 | 4 |
| 4 | 96 | 3 |
+--------------+-------+------+

How to group or marge SQL rows via PHP after query

I have a MySQL table like bellow
id | roll | exam_id | course_id | marks | status
----------------------------------------------------------
1 | 01001 | 1 | 1 | 56 | 1
2 | 01002 | 1 | 1 | 68 | 1
3 | 01003 | 1 | 1 | 55 | 1
4 | 01004 | 1 | 1 | 67 | 1
5 | 01001 | 1 | 2 | 54 | 1
6 | 01002 | 1 | 2 | 59 | 1
7 | 01003 | 1 | 2 | 62 | 1
8 | 01004 | 1 | 2 | 63 | 1
9 | 01001 | 2 | 3 | 61 | 1
10 | 01002 | 2 | 3 | 48 | 1
11 | 01003 | 2 | 3 | 22 | 1
12 | 01004 | 2 | 3 | 39 | 1
Now I want to have all the row with exam_id = 1
SELECT * FROM result WHERE exam_id=1 ORDER BY course_id
After that I need to display this table inside HTML after grouping it by roll means one row for each roll that have row-span according to the course number of the result table
Roll | course_id | marks
-----------------------------
01001 | 1 | 56
| 2 | 68
-----------------------------
01002 | 1 | 55
| 2 | 67
-----------------------------
01003 | 1 | 55
| 2 | 62
-----------------------------
01004 | 1 | 67
| 2 | 63
I'm using Codeigniter framework to doing this project. Any suggestions about how I can I do this?
Thank you in advance.
[EDIT]
Current SQL I'm using to do this:
SELECT * FROM `exam_result` JOIN `course` ON `course`.`course_tab_id`=`exam_result`.`result_course` WHERE `exam_id` = '1' AND `result_status` = 1 GROUP BY `exam_result`.`exam_roll`, `course`.`course_tab_id` ORDER BY `exam_result`.`exam_roll` ASC, `course`.`course_id` ASC
Try this but the difference in this table on my first query is that the course title will be included on a single table, I have set the marks as AVG due to so if ever you have duplicate data you will see their average result for the course and roll of a school or whatever you use this structure for
SELECT roll, exam_id, a.course_id, marks, status, course_title, course_credit
FROM
(SELECT roll, exam_id, course_id, AVE(marks) as marks, status FROM result) as a
LEFT JOIN
(SELECT course_id, course_title, course_credit FROM course) as b
ON
a.course_id = b.course_id
WHERE exam_id = '1' -- you can remove this if you wanted all exam appear on your list
GROUP BY roll, exam_id, a.course_id, marks, status, course_title, course_credit
ORDER BY roll, course_id, marks
Try this sql query
SELECT Roll, course_id, AVG(marks)
FROM result WHERE exam='1'
GROUP BY Roll, course_id
ORDER BY roll, course_id, marks

sum the quantity from 3rd relational table

I have following tables, i want to fetch the purchase_order and his order_quantity and sum of received quantity for each purchase_order. i know how to sum the quantity from single table but from multiple tables, it is confusing me a lot...
mysql> select * from purchase_order;
+-------------------+-------------------------+-------+---------------------+
| purchase_order_id | purchase_order | cost | created_on |
+-------------------+-------------------------+-------+---------------------+
| 1 | Dell Computer 000001256 | 10000 | 2015-02-19 22:14:52 |
| 2 | HP Computer 000001256 | 50000 | 2015-02-19 22:14:52 |
+-------------------+-------------------------+-------+---------------------+
2 rows in set (0.00 sec)
mysql> select * from purchase_order_detail;
+--------------------------+-------------------+---------+------------------+
| purchase_order_detail_id | purchase_order_id | item_id | ordered_quantity |
+--------------------------+-------------------+---------+------------------+
| 1 | 1 | 279 | 100 |
| 2 | 1 | 286 | 100 |
| 3 | 2 | 279 | 200 |
| 4 | 2 | 286 | 300 |
+--------------------------+-------------------+---------+------------------+
4 rows in set (0.00 sec)
mysql> select * from delivery_order;
+-------------------+--------------------------+-------------------+---------------------+
| delivery_order_id | purchase_order_detail_id | recieved_quantity | recieved_on |
+-------------------+--------------------------+-------------------+---------------------+
| 1 | 1 | 50 | 2015-02-19 22:22:51 |
| 2 | 2 | 50 | 2015-02-19 22:24:59 |
| 3 | 1 | 50 | 2015-02-19 22:34:14 |
| 4 | 3 | 70 | 2015-02-20 11:11:31 |
| 5 | 4 | 150 | 2015-02-20 11:11:31 |
| 6 | 3 | 90 | 2015-02-20 11:12:20 |
| 7 | 4 | 100 | 2015-02-20 11:12:20 |
| 8 | 3 | 40 | 2015-02-20 11:12:55 |
| 9 | 4 | 50 | 2015-02-20 11:12:55 |
+-------------------+--------------------------+-------------------+---------------------+
So far, i have this query, but id doesn't returns correct record..
SELECT po.purchase_order_id, SUM(pod.ordered_quantity) AS Sum_of_ordered_quantity, SUM(dor.recieved_quantity) AS Sum_of_recieved_quantity
FROM purchase_order AS po
INNER JOIN purchase_order_detail AS pod ON po.purchase_order_id = pod.purchase_order_id
INNER JOIN delivery_order AS dor ON dor.purchase_order_detail_id = pod.purchase_order_detail_id
GROUP BY po.purchase_order_id
it returns this,
+-------------------+-------------------------+--------------------------+
| purchase_order_id | Sum_of_ordered_quantity | Sum_of_received_quantity |
+-------------------+-------------------------+--------------------------+
| 1 | 300 | 150 |
| 2 | 1500 | 500 |
+-------------------+-------------------------+--------------------------+
you can see in the question that , purchase_order_id 1 has 200 ordered quantity and 150 received quantity while purchase_order_id 2 has 500 ordered_quantity and 500 received quantity.
Please try this code:
SELECT po.purchase_order_id,
SUM(pod.ordered_quantity) AS Sum_of_ordered_quantity,
(SELECT `mySelect`.`desired_sum` FROM
(SELECT B.`purchase_order_id` AS myID, SUM( `A`.`recieved_quantity` ) AS desired_sum
FROM `delivery_order` AS A
LEFT JOIN `purchase_order_detail` AS B ON A.purchase_order_detail_id = B.purchase_order_detail_id
GROUP BY B.`purchase_order_id` ) AS mySelect
WHERE `mySelect`.`myID` = `po`.`purchase_order_id`) AS Sum_of_received_quantity
FROM purchase_order AS po
INNER JOIN purchase_order_detail AS pod ON po.purchase_order_id = pod.purchase_order_id
GROUP BY po.purchase_order_id
This is usual while doing aggregate sum function with multiple many-to-many tables with different joining condition.
One way is to use correlated subquery to get the aggregate value and then do the join. Something as
select
po.purchase_order_id,
pod.Sum_of_ordered_quantity,
do.Sum_of_received_quantity
from purchase_order po
join
(
select purchase_order_id,sum(ordered_quantity) as Sum_of_ordered_quantity
from purchase_order_detail
group by purchase_order_id
)pod on pod.purchase_order_id = po.purchase_order_id
join
(
select
t1.purchase_order_id,
sum(t2.recieved_quantity) as Sum_of_received_quantity
from purchase_order_detail t1
join delivery_order t2 on t1.purchase_order_detail_id = t2.purchase_order_detail_id
group by t1.purchase_order_id
)do on do.purchase_order_id = po.purchase_order_id
DEMO

Custom MySQL Ordering

I have a query that needs a custom sorting, trimmed down to the bare minimums something like:
SELECT u.*, p.*, p.id as product_id
FROM users u, products p
WHERE u.id = p.user_id
ORDER BY product_id DESC
And I get returned a set of rows like:
UserID ProductID
2 5
2 4
3 3
1 2
1 1
But I want it to actually sort SOMETHING like this (so no 2 UserIDs are adjacent to eachother):
UserID ProductID
1 2
2 4
3 3
2 5
1 1
Is this even possible with MySQL, or do I need some PHP magic?
A canonical way of solving this problem is by enumerating the duplicate rows and then ordering by that value:
select t.*
from (SELECT u.*, p.*, p.id as product_id,
row_number() over (partition by u.id order by (select NULL)) as seqnum
FROM users u join
products p
on u.id = p.user_id
) t
order by seqnum, id;
This will work, as long as no one user has a really long sequence (as in your example).
There is no "always-works" solution, because it is easy to come up with a situation where your goal is not possible.
Here fetch your sorted results into an array. Then do something like this.
$records = $res->fetchAll();
$count = count($records);
$records = array_chunk($records, ceil(count($records)/2);
$unsorted = array();
for($x = 0; $x < $count; $x++){
$unsorted[] = $records[$x%2][floor($x/2)];
}
Consider the following...
CREATE TABLE sortable(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,player_id INT NOT NULL);
INSERT INTO sortable(player_id) VALUES (1),(1),(2),(3),(4),(3),(3),(2),(1),(2),(4),(4);
SELECT * FROM sortable;
+----+-----------+
| id | player_id |
+----+-----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
| 6 | 3 |
| 7 | 3 |
| 8 | 2 |
| 9 | 1 |
| 10 | 2 |
| 11 | 4 |
| 12 | 4 |
+----+-----------+
SELECT x.*,COUNT(*) rank FROM sortable x JOIn sortable y ON y.player_id = x.player_id AND y.id <= x.id GROUP BY x.id ORDER BY player_id,rank;
+----+-----------+------+
| id | player_id | rank |
+----+-----------+------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 9 | 1 | 3 |
| 3 | 2 | 1 |
| 8 | 2 | 2 |
| 10 | 2 | 3 |
| 4 | 3 | 1 |
| 6 | 3 | 2 |
| 7 | 3 | 3 |
| 5 | 4 | 1 |
| 11 | 4 | 2 |
| 12 | 4 | 3 |
+----+-----------+------+
SELECT x.*,COUNT(*) rank FROM sortable x JOIn sortable y ON y.player_id = x.player_id AND y.id <= x.id GROUP BY x.id ORDER BY rank;
+----+-----------+------+
| id | player_id | rank |
+----+-----------+------+
| 1 | 1 | 1 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 4 | 1 |
| 2 | 1 | 2 |
| 8 | 2 | 2 |
| 6 | 3 | 2 |
| 11 | 4 | 2 |
| 9 | 1 | 3 |
| 10 | 2 | 3 |
| 7 | 3 | 3 |
| 12 | 4 | 3 |
+----+-----------+------+
So if your problem is just that you dont want two records with same id should not come next to each other wha I can think simplest is use
SELECT u.*, p.*, p.id as product_id
FROM users u, products p
WHERE u.id = p.user_id
ORDER BY user_id%2 DESC
Or you can even use other number than 2 to meet any certain order you want....

Select tag with count from multiple table join containing multiple tags for each id

My table has 4 tags linked to the to each id. I want to select the position(rank) of that value(tag_name or tag_id) and the number of times the value(tag_name or tag_id) displays in that same position(rank).
Here's what it would look like in mysql:
> +--------+------------+--------+------+
> | id | tag_name | tag_id | rank |
> +--------+------------+--------+------+
> | 2345 | cookie | 2 | 1 |
> | 2345 | bar | 1 | 2 |
> | 2345 | cereal | 3 | 3 |
> | 2345 | milk | 4 | 4 |
> | 2346 | cereal | 3 | 1 |
> | 2346 | milk | 4 | 2 |
> | 2346 | cookie | 2 | 3 |
> | 2346 | hot dogs | 5 | 4 |
> | 2347 | chocolate | 6 | 1 |
> | 2347 | bar | 1 | 2 |
> +--------+------------+--------+------+
Here's what my current code looks like:
SELECT m.*, tr.tag_id, t.tag_name, #rownum:=#rownum + 1 AS rank
FROM meals AS m
RIGHT JOIN tags_rel AS tr ON tr.meal_id = m.id
JOIN tags AS t ON tr.tag_id = t.id
JOIN (SELECT #rownum:=0) AS r
ORDER BY m.id DESC
Please keep in mind that I'm actually using a pagination so my ORDER BY actually looks like this:
ORDER BY id DESC LIMIT $start_from, 12
If I select either tag_name=bar or tag_id=1 and rank=2 I should get the bellow results. Plus I would like the count of rows returned for that value.
> +--------+------------+--------+------+
> | id | tag_name | tag_id | rank |
> +--------+------------+--------+------+
> | 2345 | bar | 1 | 2 |
> | 2347 | bar | 1 | 2 |
> +--------+------------+--------+------+
Thanks!
SELECT tag_name, tag_id, rank, COUNT(tag_id) AS rankcount
FROM table
GROUP BY tag_id, rank;
results in:
+-----------+--------+------+-----------+
| tag_name | tag_id | rank | rankcount |
+-----------+--------+------+-----------+
| bar | 1 | 2 | 2 |
| cookie | 2 | 1 | 1 |
| cookie | 2 | 3 | 1 |
| cereal | 3 | 1 | 1 |
| cereal | 3 | 3 | 1 |
| milk | 4 | 2 | 1 |
| milk | 4 | 4 | 1 |
| hot dogs | 5 | 4 | 1 |
| chocolate | 6 | 1 | 1 |
+-----------+--------+------+-----------+
9 rows in set (0.00 sec)
If you want your original table enhanced by a count of lines (although that seems to be quite senseless, as every sql result returns the number of rows) you'll have to do a subquery:
SELECT id, tag_name, tag_id as t_id, rank,
(SELECT count(tag_id) FROM table WHERE tag_id = t_id) as subqueryCount
FROM table;
+------+-----------+------+------+---------------+
| id | tag_name | t_id | rank | subqueryCount |
+------+-----------+------+------+---------------+
| 2345 | cookie | 2 | 1 | 2 |
| 2345 | bar | 1 | 2 | 2 |
| 2345 | cereal | 3 | 3 | 2 |
| 2345 | milk | 4 | 4 | 2 |
| 2346 | cereal | 3 | 1 | 2 |
| 2346 | milk | 4 | 2 | 2 |
| 2346 | cookie | 2 | 3 | 2 |
| 2346 | hot dogs | 5 | 4 | 1 |
| 2347 | chocolate | 6 | 1 | 1 |
| 2347 | bar | 1 | 2 | 2 |
+------+-----------+------+------+---------------+
10 rows in set (0.00 sec)
Extending that query would result in your desired result, extended by a column where the count of rows is inserted (which is IMO still rather senseless).
SELECT id, tag_name, tag_id as t_id, rank,
(SELECT count(tag_id) FROM table WHERE tag_id = t_id) as subqueryCount
FROM table
WHERE tag_id = 1;
results in
+------+----------+------+------+---------------+
| id | tag_name | t_id | rank | subqueryCount |
+------+----------+------+------+---------------+
| 2345 | bar | 1 | 2 | 2 |
| 2347 | bar | 1 | 2 | 2 |
+------+----------+------+------+---------------+
2 rows in set (0.00 sec)
how about this:
SELECT DISTINCT meal_id as MealId, t2name as TagName, t2id as TagId, Rank
FROM
(SELECT t1id as t2id, t1name as t2name, rnk as rnk2, count(*) as Rank
FROM
tags_rel AS r,
(SELECT m1id, t1id, t1name,
#rnk := CASE WHEN #id <> m1id THEN 1 ELSE #rnk + 1 END AS rnk, #id := m1id as idx
FROM
tags_rel AS r,
(SELECT #id := 0) idx,
(SELECT #rnk := 0) rxx,
(SELECT m.id AS m1id, t.id AS t1id, name AS t1name
FROM tags_rel r
JOIN (meals AS m, tags AS t ) ON ( m.id = r.meal_id AND t.id = r.tag_id )
) AS rn
WHERE r.tag_id = t1id AND r.meal_id = m1id
ORDER BY m1id, t1name
) AS rno
WHERE r.tag_id = t1id AND r.meal_id = m1id
group BY t1id, t1name, rnk
) as rn2
join (meals, tags_rel) on (meals.id = tags_rel.meal_id and tags_rel.tag_id = t2id)
ORDER BY meal_id, t2id
which results in:
2345 bar 1 2
2345 cookie 2 1
2345 cereal 3 1
2345 milk 4 2
2346 cookie 2 1
2346 cereal 3 1
2346 milk 4 2
2346 hotdog 5 1
2347 bar 1 2
2347 chocolad 6 1

Categories