CROSSTAB REPORT - MYSQL - php

I want generate a HTML report, using PHP and the following Mysql data:
VIDEOS
id | title
01 | video1
02 | video2
03 | video3
04 | video4
JURORS
id | Name
01 | juror1
02 | juror2
03 | juror3
REVIEWS
id_video | id_juror | grade
01 | 01 | 5,2
02 | 01 | 4,5
03 | 01 | 2,7
04 | 01 | 7,0
01 | 02 | 3,2
02 | 02 | 9,6
03 | 02 | 4,7
04 | 02 | 8,3
The OUTPUT should be something like that:
| juror1 | juror2 | juror3
video1 | 5,2 | 3,2 | NULL
video2 | 4,5 | 9,6 | NULL
video3 | 2,7 | 4,7 | NULL
video4 | 7,0 | 8,3 | NULL
I have tried many different querys and crosstab tutorials, but they would use CASE for a limited quantity of videos and jurors, but I need it to be dynamic.

In MySQL, you can't do this with just a query. One approach, described at http://www.databasejournal.com/features/mysql/article.php/3871556/article.htm, is to create a stored-procedure that finds all the possible videos and all the possible jurors and then dynamically builds a SQL query. But since you're writing in PHP, I think you'd find it easier to do that in PHP than in a stored procedure. Alternatively, you can run a straightforward SELECT videos.title, jurors.name, reviews.grade FROM reviews JOIN videos ON videos.id = reviews.id_video JOIN jurors ON jurors.id = reviews.id_juror and handle the translation in PHP.

Using MySQL Pivot table generator you can do this report instantly .
You will be asked to enter the data source of your columns , Rows and Values of your report .
In the columns : You select the JURORS table and the name column as in the following screenshot:
Similarly you should select the Videos table and Title column as the data source for the rows then finally the reviews table and grade column for the values and you get your report , which will be fed automatically from your database
For more information you can check the following tutorial :
http://mysqlpivottable.net/Tutorial.html

Related

Exclude records from Query Builder Laravel

My table have these data:
ID | company_id | code | description
01 | NULL | CD01 | Standard description CD01
02 | NULL | XYZU | Standard description XYZU
03 | 1 | CD01 | Custom description CD01 for company 1
04 | 2 | CD01 | Custom description CD01 for company 2
I need to extract all 'code' from this table but showing a single product code only once.
If exists a record witht company_id <> '' I show that, instead if doesn't exists I show the record with standard description.
Starting from the sample data, if I wanted to show the articles for the company_id = 1, I expect to have this output:
ID | company_id | code | description
02 | NULL | XYZU | Standard description XYZU
03 | 1 | CD01 | Custom description CD01 for company 1
Have you got some suggest to do it?
Thank you
for removing duplicate entries from database results by group by you should disable ONLY_FULL_GROUP_BY for mysql. don't do that in mysql and don't disable strict mode! laravel itself sets some modes.
this is the overall solution for disabling this mode.
but in laravel you should also try another thing:
go to YouProjectFolder/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php
at the end of the file fin strict_mode() function and just remove ONLY_FULL_GROUP_BY fr0m the string within the function.
(i just saw this solution in a stackoverflow post. unfortunately i didn't find that post)

MySql row to be sorted for performance

I have a MySQL server that keeps events in DB, the DB looks like this:
id | epoch_time | type | event_text | ....
---|------------|------|-------------|-----
01 | 1487671205 | 0 | user-login | ....
02 | 1487671284 | 0 | user-logout | ....
03 | 1487671356 | 1 | sys_error | ....
04 | 1487671379 | 0 | user-logout | ....
05 | 1487671389 | 2 | user_error | ....
06 | 1487671397 | 1 | sys_error | ....
On the web UI, there is a summery of the last 24 hours events by type, since the DB is keeping 1 year back log of data, there are over 1M records at the moment which makes the site loads very slow (from the obvious reasons).
The SQL query is simple,
SELECT COUNT(id) as total FROM `eventLog` WHERE `epoch_time` >= (UNIX_TIMESTAMP() - 86400)
My question is - Is there a way to to "tell" MySQL that the epoch_time column is sorted so that once it hits a raw that has:
epoch_time < (UNIX_TIMESTAMP() - 86400)
The query will end.
Thanks
[UPDATE]
Thank you all for your help, I tried to add the index but the performance is still bad (~ 7 - 12 sec' to load the page)
Does it make sense to just keep statistical information just for that ?
You can set epoch_time as index using:
ALTER TABLE `eventLog` ADD INDEX epoch_time (`epoch_time`)
That will make your query runs much faster,

Gathering rows to 1 row and adding column

Hello I am currently learning programming and I am working on a project of php and mysql, but I have a problem.
What I am doing is I have a table called marks and every time people add a mark to a student, they add a column to the table, the table looks like this:
|------------------|
| subject | marks |
|------------------|
| math | 16 |
| english | 18 |
| history | 15 |
| math | 14 |
| english | 20 |
|------------------|
But I want to change it like this when I treat it:
|-------------------------|
| math | 16 | 14 |
| english | 18 | 20 |
| history | 15 | |
|-------------------------|
How can I do this with mysql's query, or php if needed?
Well my final goal is to make html table with this but I already know how to do it.
I am sorry if it looked simple, but I am really a beginner at databases and programming and I apologize if the title confuses you guys.
You can try
SELECT subject, GROUP_CONCAT(marks ORDER BY marks) marks
FROM table
GROUP BY subject
ORDER BY subject
It will give you two columns, one with a comma-separated list of subjects.
english 18, 20
history 15
math 14, 16

MySQL Categories List vs Sepperate Categories Table

I'm creating a blogging website which lets you assign a category to a blog. The original table that I had looked like this:
+----+----------+-------+---------+
| ID | CATEGORY | TITLE | ARTICLE |
+----+----------+-------+---------+
| 01 | cars | Title | Article |
| 02 | cars | Title | Article |
| 03 | sport | Title | Article |
| 04 | music | Title | Article |
| 05 | music | Title | Article |
+----+----------+-------+---------+
So as you can see, the category is a written attribute. The subject came up of having a sepperate categories list which I created to look like:
+-------------------+
| CAT_ID | CATEGORY |
+-- --+----------+
| 01 | cars |
| 02 | sport |
| 03 | music |
+--------+----------+
Which alters the original table to:
+----+--------------------+----------+-------+---------+-----------+
| ID | BELONGS_TO_BLOG_ID | CATEGORY | TITLE | ARTICLE | AUTHOR_ID |
+----+--------------------+----------+-------+---------+-----------+
| 01 | 01 | 01 | Title | Article | 01 |
| 02 | 01 | 01 | Title | Article | 01 |
| 03 | 02 | 02 | Title | Article | 02 |
| 04 | 02 | 03 | Title | Article | 03 |
| 05 | 02 | 03 | Title | Article | 03 |
+----+--------------------+----------+-------+---------+-----------+
Now if I wanted to delete cars which is category 01, rather than delete it (because obviosly 2 articles are in this category) I would probably change this to unnassigned. So no problems here but if I was then to delete music (03) and change this to unnassigned I would end up with ID's 01 & 03 being unnassigned. So now when I run searhes for unnassigned articles, which should return 4, I am only going to see 2 or an error.
Wouldn't it be better to have the original table and when someone changes category cars they simply UPDATE the record?
Well, you can validate it . If you need to follow the database rules definitely you have to check parent-child relationship between 2 tables. There are 3 ways
1. FOREIGN KEY : This will prevent parent deletion , until child record is there
ALTER TABLE items
ADD FOREIGN KEY (category)
REFERENCES category(categoryTable)
2. Validation from php script :Some storage engine may not support point 1. In that case you can simply validate using php. Before deleting category just count if it has some product are not
$sql="select count(*) as cnt from items where category=CATEGORY_TO_BE_DELETED";
if(mysql_query('cnt')>0) //means it has some items
//do not delete or throw some error
else
//delete
3 Logical deletion/soft delete:
Sometimes you need to delete category (suppose you have 1000 products under it and you can't delete it). Then you can have some flags like is_deleted. You can on or off that flag to delete undelete it

PHP MYSQL - combining 2 while loops

I want to populate a table form a MYSQL database.
I have defined the tables and the mysql_query's so that this should be a lot cleaner.
Separately I am able to get the results, but I need to combine them since I am making a 4 column table.
looks like this:
-------------------------------------------------------------
| | | | |
| Menu 01 info | Menu 01 info | Menu 06 info | Menu 06 info |
| | | | |
-------------------------------------------------------------
while ($menu01 = mysql_fetch_array($order01)) AND while ($menu06 = mysql_fetch_array($order06))
{
//TableStuff
It is the line with while that I need to be without errors. Any help would be great. PHP is not my strongest point of knowledge sorry:-)
Boolean and, and mind the grouping.
while (($menu01 = mysql_fetch_array($order01)) && ($menu06 = mysql_fetch_array($order06)))

Categories