How to display dynamic mysql vertical data to horizontal using php - php

I want to display dynamic mysql vertical data to horizontal in html table using PHP. And my table is like
mysql> select * from role_perm;
-------------------------------------------
| id | userID | roleID | permID | value |
--------------- ---------------------------
| 1 | 2 | 1 | 1 | 1 |
|------------------------------------------
| 2 | 2 | 1 | 2 | 0 |
|------------------------------------------
| 3 | 2 | 1 | 3 | 0 |
|------------------------------------------
| 4 | 2 | 2 | 4 | 0 |
-------------------------------------------
| 5 | 2 | 2 | 1 | 1 |
|------------------------------------------
| 6 | 2 | 2 | 2 | 1 |
|------------------------------------------
| 7 | 2 | 2 | 3 | 0 |
|------------------------------------------
| 8 | 2 | 2 | 4 | 1 |
-------------------------------------------
| 9 | 5 | 1 | 1 | 1 |
|------------------------------------------
| 10 | 5 | 1 | 2 | 0 |
|------------------------------------------
| 11 | 5 | 1 | 3 | 0 |
|------------------------------------------
| 12 | 5 | 1 | 4 | 0 |
-------------------------------------------
and so on...
and i want to display in html table like
----------------------------
| role | permissions |
----------------------------
| 1 | 1 | 2 | 3 | 4 |
----------------------------
| 2 | 1 | 2 | 3 | 4 |
----------------------------
| 3 | 1 | 2 | 3 | 4 |
-----------------------------
| 4 | 1 | 2 | 3 | 4 |
----------------------------
could you pls help me. Thank you in advance.

Try this:
SELECT
roleID AS role,
GROUP_CONCAT(DISTINCT permID ORDER BY permID ASC SEPARATOR '|') AS permissions
FROM role_perm
GROUP BY roleID
ORDER BY roleID

I suppose you could do this with a nifty MySQL query as well (which would supposedly be better for performance), but since I'm not that good at SQL, here's the PHP solution:
$arrRoles = array();
while ($row = mysql_fetch_assoc($result)) {
if (!isset($arrRoles[$row['roleID']])) {
$arrRoles[$row['roleID']] = array();
}
$arrRoles[$row['roleID']][$row['permID']] = $row['value'];
}
var_dump($arrRoles);

Related

Getting an average rating of a TV show using average ratings of its seasons from ratings table (Laravel 9)

I have 3 tables: shows, seasons and ratings. I would like to get average ratings of specific seasons and then make an average of this for every show and sort them by this value asc or desc. I was able to get this result by this raw query:
$shows = Show::selectRaw('shows.*, avg(x.seasons_avg_points) as ratings_avg_points')
->fromSub(function($query){
$query->selectRaw('shows.id, avg(ratings.points) as seasons_avg_points')
->from('shows')
->join('seasons', 'shows.id', '=', 'seasons.show_id')
->leftJoin('ratings', 'ratings.item_id', '=', 'seasons.item_id')
->groupBy('seasons.id');
}, 'x')
->join('shows', 'shows.id', '=', 'x.id')
->groupBy('x.id')
->get()->sortBy('shows_avg_points');
However I prefer to use Laravel eloquent functions so I tried also this:
$shows = Show::with(['seasons' => function($query){
$query->withAvg('ratings', 'points');
}])->withAvg('seasons', 'ratings_avg_points')->get();
Unfortunately, it only works without ->withAvg('seasons', 'ratings_avg_points'), as there can't be made an average of a computed column (ratings_avg_points) and it can't be found.
shows
| id | name |
|----|--------|
| 1 | show 1 |
| 2 | show 2 |
| 3 | show 3 |
seasons
| id | show_id | item_id | name |
|----|---------|---------|----------|
| 1 | 1 | 1 | season 1 |
| 2 | 1 | 2 | season 2 |
| 3 | 2 | 3 | season 1 |
| 4 | 2 | 4 | season 2 |
| 5 | 3 | 5 | season 1 |
| 6 | 3 | 6 | season 2 |
ratings
| id | item_id | points |
|----|---------|--------|
| 1 | 1 | 0 |
| 2 | 1 | 20 |
| 3 | 2 | 0 |
| 4 | 2 | 30 |
| 5 | 3 | 0 |
| 6 | 3 | 40 |
| 7 | 4 | 0 |
| 8 | 4 | 50 |
| 9 | 5 | 0 |
| 10 | 5 | 60 |
| 11 | 6 | 0 |
| 12 | 6 | 70 |
result
| id | name | ratings_avg_points|
|----|--------|-------------------|
| 3 | show 3 | 32.5 |
| 2 | show 2 | 22.5 |
| 1 | show 1 | 12.5 |
Is there a way I can get Show models with their average rating values without using that 'ugly' query above?

generate sequential reference numbers

I am generating sequential reference numbers like complaint/1, complaint/2 when an insertion is made. I get what the previously generated value is (complaint/2) and then increment.
But when two users submit at the same time, I sometimes get same reference nos for both complaints.
How do I prevent this?
SELECT RIGHT(Date_format(from_financial_year_,'%Y'),2),
RIGHT(Date_format(to_financial_year_,'%Y'),2)
INTO from_financial_year_,
to_financial_year_;SELECT rec.receipt_ref_no
INTO last_receipt_ref_num_
FROM svk_apt_receipts rec
WHERE Replace(Substring_index(rec.receipt_ref_no, '/', 2),'REC/','') = Concat(from_financial_year_,to_financial_year_)
AND rec.customer_id = customer_id_
AND rec.association_id = association_id_
ORDER BY rec.receipt_id DESC limit 1;IF(last_receipt_ref_num_ IS NULL) then
SELECT 1
INTO max_ref_id_;
else
SELECT (replace(last_receipt_ref_num_, concat('REC/',from_financial_year_,to_financial_year_,'/'),'')+1)
INTO max_ref_id_;ENDIF;SELECT Concat('REC/',from_financial_year_,to_financial_year_,'/',max_ref_id_)
INTO receipt_ref_no_;INSERT INTO svk_apt_receipts
(
receipt_ref_no, paid, payable, is_paid, master_receipt_to_id, receipt_from_id, receipt_to_id, receipt_date,
receipt_mode, transaction_ref_no, customer_id, association_id, is_active, created_by, created_on, receipt_status_id, remarks
)
VALUES
( receipt_ref_no_, _total_amount, 0,1,3, receipt_from_id_, receipt_to_id_, Cast(Now()AS DATE), 3, _transaction_ref_no,
customer_id_, association_id_, 1, _created_by, Now(), 2, 'Paid through Payment Gateway'
);
As mentioned in comments, just store an autoincrementing id. All the other stuff can be handled by trivial queries and/or your presentation layer.
By way of example...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,user INT NOT NULL
);
INSERT INTO my_table (user) VALUES
(1),(1),(3),(3),(5),(2),(1),(8),(4),(5),(7),(5),(5),(4),(1),(2),(3),(6),(4),(6),(1),(5),(1),(8);
SELECT * FROM my_table;
+----+------+
| id | user |
+----+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 3 |
| 4 | 3 |
| 5 | 5 |
| 6 | 2 |
| 7 | 1 |
| 8 | 8 |
| 9 | 4 |
| 10 | 5 |
| 11 | 7 |
| 12 | 5 |
| 13 | 5 |
| 14 | 4 |
| 15 | 1 |
| 16 | 2 |
| 17 | 3 |
| 18 | 6 |
| 19 | 4 |
| 20 | 6 |
| 21 | 1 |
| 22 | 5 |
| 23 | 1 |
| 24 | 8 |
+----+------+
24 rows in set (0.01 sec)
SELECT x.*
, COUNT(*) complaint
FROM my_table x
JOIN my_table y
ON y.user = x.user
AND y.id <= x.id
GROUP
BY x.id
ORDER
BY user
, id;
+----+------+-----------+
| id | user | complaint |
+----+------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 7 | 1 | 3 |
| 15 | 1 | 4 |
| 21 | 1 | 5 |
| 23 | 1 | 6 |
| 6 | 2 | 1 |
| 16 | 2 | 2 |
| 3 | 3 | 1 |
| 4 | 3 | 2 |
| 17 | 3 | 3 |
| 9 | 4 | 1 |
| 14 | 4 | 2 |
| 19 | 4 | 3 |
| 5 | 5 | 1 |
| 10 | 5 | 2 |
| 12 | 5 | 3 |
| 13 | 5 | 4 |
| 22 | 5 | 5 |
| 18 | 6 | 1 |
| 20 | 6 | 2 |
| 11 | 7 | 1 |
| 8 | 8 | 1 |
| 24 | 8 | 2 |
+----+------+-----------+
24 rows in set (0.00 sec)

yii: count value array depend on index group number

I have table from mysql like below:
id | name | grade | k1 | k2 | k3 | s1| s2 | s3| e1 | e1 | e3 |
1 | Aa | 5 | 1 | 0 | 5 | 3 | 2 | 1 | 0 | 6 | 1 |
2 | Bb | 1 | 1 | 3 | 5 | 3 | 5 | 3 | 4 | 6 | 1 |
3 | Cc | 2 | 1 | 4 | 2 | 2 | 2 | 4 | 0 | 6 | 1 |
4 | Dd | 4 | 1 | 3 | 5 | 3 | 3 | 1 | 0 | 6 | 1 |
5 | Ee | 3 | 1 | 5 | 2 | 1 | 0 | 5 | 0 | 6 | 1 |
6 | Ff | 2 | 1 | 3 | 1 | 3 | 4 | 2 | 0 | 6 | 1 |
7 | Gg | 5 | 1 | 1 | 5 | 5 | 2 | 1 | 0 | 6 | 1 |
Using FOR and FOREACH looping, i do able to show all row. But in final view/table i want to
SUM [k1,k2,k3], SUM[s1,s2,s3] and SUM[e1,e2,e3]
So it will place new colum as K, S and E in each row.
Here are my code:
for ($i = 0; $i < Evaluation::model()->count(); $i++) {
foreach (Yii::app()->db->createCommand()
->from('evaluation')
->queryAll() as $item) {
// Row-Column start here
// id | name | grade | K | S | E |
}
}
thanks.
I love the query builder. So here is one of the solution.
$result = Yii::app()->db->createCommand()->
select('id, name, grade, (k1+k2+k3) AS K, (s1+s2+s3) AS S, (e1+e2+e3) AS E')->
from(MyModel::model()->tableName())->
queryAll();
var_dump($result);
select k1+k2+k3, s1+s2+s3, e1+e2+e3 from t
and it is not yii question, it is about MySql
If you want to express it in Yii, you can
Yii:app()->createCommands()
->select('k1+k2+k3, s1+s2+s3, e1+e2+e3')
->from('t')
->queryAll();

Concatenate and Count the multiple rows in single rows in mysql

I want to concatenate and count data of the same column, so I can concatenate but I can not count the repeated data.
Here's my table of data:
| ID | bills | class |
|-----|-------|-------|
| 1 | 0.5 | 2 |
| 2 | 1 | 1 |
| 3 | 0.5 | 2 |
| 5 | 1 | 3 |
| 6 | 0 | 2 |
| 7 | 0.5 | 1 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 0.5 | 1 |
| 11 | 0 | 2 |
| 12 | 1 | 1 |
| 13 | 0 | 3 |
| 14 | 1 | 2 |
| 15 | 0 | 1 |
| 16 | 0 | 1 |
| 17 | 0.5 | 3 |
| 18 | 0 | 3 |
| 13 | 0.5 | 3 |
Here's my sql query I'm using to concatenate data:
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
FROM tb_presence
GROUP BY class;
Here's my result below:
| class | bills |
|-------|------------------|
| 1 | 1,0.5,0.5,1,0,0 |
| 2 | 0.5,0,1,0,1 |
| 3 | 1,1,0,0.5,0,0.5 |
Now I would like to count the data that are equal, but continue with the same concatenation.
I want to "count" the data with the same values ​​and display concatenated (column observation and only to help understanding)
| class | bills | observation |
|-------|-------|-----------------------------|
| 1 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
| 2 | 2,1,2 | (2=0+0) (1=0.5) (2=1+1) |
| 3 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
Is this really possible?
Here is a solution (thanks to #wchiquito for the sqlfiddle) See http://sqlfiddle.com/#!2/2d2c8/1
As you can see it cannot dynamically determine the bills' values and count them. But there is a count per bill value that you want.
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
,SUM(IF(bills=0,1,0)) AS Count0
,SUM(IF(bills=0.5,1,0)) AS Count05
,SUM(IF(bills=1,1,0)) AS Count1
,COUNT(*) AS totalRecords
,COUNT(*)
- SUM(IF(bills=0,1,0))
- SUM(IF(bills=0.5,1,0))
- SUM(IF(bills=1,1,0))
AS Missing
FROM tb_presence GROUP BY class;
I added an extra record to show how the 'missing' column could show if you were not taking all values into consideration.
Results
| LESSON | BILLS | COUNT0 | COUNT05 | COUNT1 | TOTALRECORDS | MISSING |
|--------|-----------------------------------------|--------|---------|--------|--------------|---------|
| 1 | 0.00,0.00,0.50,0.50,1.00,1.00,1.00,4.00 | 2 | 2 | 3 | 8 | 1 |
| 2 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |
| 3 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |

select data from database Mysql query

tbl_pack_service;
+-------+---------+------------+
| ps_id | pack_id | service_id |
+-------+---------+------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 2 | 3 |
| 6 | 2 | 4 |
| 7 | 2 | 5 |
| 8 | 3 | 1 |
| 9 | 3 | 2 |
| 10 | 3 | 3 |
| 11 | 3 | 4 |
+-------+---------+------------+
ps_id is primary key
i am tying to make dynamic select list in php
i want a mysql query that can give service id which do not match with particular pack_id
like i have service_id 1,2,3,4,5
when i should select pack_id=1 then 3,4,5 should be displayed
and when i should select pack_id=2 then nothing should be displayed as it has all the 5 services.
thanks..
There are a few ways to handle this. The easiest is with a NOT IN subquery:
SELECT DISTINCT service_id
FROM
tbl_pack_service
WHERE service_id NOT IN (SELECT service_id FROM tbl_pack_service WHERE pack_id = 1)

Categories