I have created a table in the database which displays Date, User, Value. The following is a revised design:
Date | User | Value
---------- | ----- | -----
So each user has a count of what submissions they have done matching via the date. The results are displayed from this query from the database
SELECT * FROM testreport
Please see the new design below as mentioned from the comments, I have added the row total at the end.
Date | User1 | User2 | User3 | Value
---------- | ----- | ----- | ----- | -----------
23-02-2017 | 2 | 5 | 9 | 16
24-02-2017 | 5 | 7 | 11 | 23
25-02-2017 | 6 | 13 | 1 | 20
But what I am trying to achieve is by selecting the table and have total to show in the last row like this, how do I go about doing this:
Date | User1 | User2 | User3 | Value
---------- | ----- | ----- | ----- | -----------
23-02-2017 | 2 | 5 | 9 | 16
24-02-2017 | 5 | 7 | 11 | 23
25-02-2017 | 6 | 13 | 1 | 20
Total | 13 | 25 | 21 | 59
but I cannot specify the column names like How do I calc a total row using a PIVOT table, without UNION, ROLLUP or CUBE?. Because from time time there will be extra users added to this table but I want to be able to get all the columns with totals at the end. On my site I am using PHP to display the table on the page with the code:
$sqlres = "SELECT * FROM testreport order by Date DESC ";
$resultsql = mysqli_query($sqlres) or die(mysql_error());
echo "<table><tr>";
for($i = 0; $i < mysqli_num_fields($resultsql); $i++) {
$field_info = mysqli_fetch_field($resultsql, $i);
echo "<th>{$field_info->name}</th>";
}
// Print the data
while($rows = mysqli_fetch_row($resultsql)) {
echo "<tr>";
foreach($rows as $_column) {
echo "<td>{$_column}</td>";
}
echo "</tr>";
}
echo "</table>";
Which displays the headers and rows as I wanted them to be and any new user I add to the 'testreport' table column it automatically adds an extra column on the page, how do I go about achieving the rollup of these columns to get total.
Any help on this would be appreciated.
Related
SOLUTION: Make sure you don't 'use up' any $responses->fetch_assoc()s before the while loop.
I performed mysqli_fetch_array($responses);.
In php I have this sql query (simplified for your convenience, but the problem remains)
$sql = "SELECT id, content FROM responses ORDER BY RAND()";
$responses = $conn->query($sql);
where the responses table looks like this:
+----+----------+--------+------+
| id | content | userId | part |
+----+----------+--------+------+
| 4 | peewee | 31 | 1 |
| 5 | tallinn | 31 | 1 |
| 6 | dewey | 31 | 1 |
| 7 | stanford | 31 | 1 |
+----+----------+--------+------+
That doesn't format properly so all you need to know is that the id and content rows are different for each entry while the rest is the same for each.
The problem is, when I do a while loop on $responses like so:
while ($row = $responses->fetch_assoc()) {
$responseId = $row["id"];
$content = $row["content"];
echo " id: ".$responseId;
echo " content: ".$content;
}
I always get 1 record fewer than there are. In this case, since there are 4 rows, I would only see 3 echoed. However, it is not always the same 3, nor are they in the same order. If I remove the ORDER BY RAND() clause, then it is always the first record which is left out.
Thanks in advance
Cheers
I have a database contains ID and name of my Staff (DATABASE 1):
--------------
| ID | name |
--------------
| 1 | Mr.AA |
| 2 | Mr.AB |
|... | ... |
| 78 | Mr.CZ |
--------------
Then my colleague has the staff absence database per day for 2 years (DATABASE 2):
Tablename: Table_for_Mr.AA
--------------------------
| ID | date | work |
--------------------------
| 1 | 2016-01-01 | Yes |
| 2 | 2016-01-02 | Yes |
| 3 | 2016-01-03 | No |
|... | ... | ... |
|730 | 2017-12-31 | Yes |
--------------------------
Due to our agreement, we hold each database ourselves (2 parties), so each database is stored in different server.
Lately I need to get the data from DATABASE 2 to be shown in my website and I can ask my colleague to make PHP file that return the array for each name (www.colleaguewebsite/staff/absence.php?name=Mr.AA).
I already made the new 'workstat' database (DATABASE 3) in my server with this detail:
---------------------------------
| ID | date | Name | work |
---------------------------------
| 1 | 2016-01-01 |
| 2 | 2016-01-02 |
| 3 | 2016-01-03 |
|... | ... |
---------------------------------
this is the best I can do:
$sourceURL = 'www.colleaguewebsite/staff/absence.php'
$sql1= $conn->query("select * FROM staff ");
while($row_1 = $sql1->fetch_array()){
$name= $row_1 ['name'];
//getting the absence detail from each staff
$json_1 = file_get_contents($sourceURL.'?name='.$name);
$data_1 = json_decode($json_1,true);
foreach($data_1 as $value_1){
$date = $value_1['date'];
$work = $value_1['work'];
//if the correspondence date is exist then update, otherwise add
$sql_2 = $conn->query("select * FROM workstat WHERE date='$date' AND name='$name");
if ($sql_2->num_rows > 0){
$update=$conn->query("UPDATE workstat set name='$name', work='$work' WHERE date='$date' ");
}else{
$addnew=$ob->query("INSERT INTO availability (date, name, work) VALUES ('$date', '$name', '$work'
}
}
}
However, I have some things that bothers:
The required time to execute this script is very long, mostly exceeding the 90 seconds time.
Dirty database. I will have 730 row of data (per day) for each name, so my database 3 will have 730 * 78 person = 56.940 rows with duplicate date (2017-01-01 ... 2017-12-31 for Mr.AA, 2017-01-01 ...2017-12-31 for Mr.AB, etc...).
How can I optimize my code in table design and loading time?
Another method than file_get_contents is okay, I hope it's still PHP.
This is the bookings table I'm using for my query
+----------------------+
| event_id | person_id |
+----------------------+
| 5 | 7 |
| 4 | 7 |
| 3 | 7 |
| 4 | 5 |
| 3 | 5 |
| 5 | 3 |
+----------------------+
This table shows that person_id 7 has 3 bookings, 5 has 2 bookings and 3 has 6 bookings.
Currently, I'm using this query to get the total number of bookings per person.
$query='
SELECT
bookings.person_id,
COUNT(bookings.person_id) AS total,
bookings.event_id,
users.display_name
FROM bookings
INNER JOIN users ON bookings.person_id=users.id
WHERE users.id=bookings.person_id
GROUP BY bookings.person_id';
$result = mysql_query($query);
if($result) {
while($row = mysql_fetch_array($result))
{
/* total bookings per user */
$value = $row['total'];
$sum += $value;
/* events booked per user */
$events....
/* Displaying results */
echo "<tr width='500'>";
echo "<td>".$row['person_id']."</td>";
echo "<td>".$row['display_name']."</td>";
echo "<td>".$row['total']."</td>";
echo "<td>".$events."</td>";
echo "</tr>";
}
This works okay and gives me:
+-----------------------------------+
| ID | NAME | Total Bookings |
+-----------------------------------+
| 7 | Bob | 3 |
| 5 | Jane | 2 |
| 3 | Joe | 1 |
+-----------------------------------+
I'm seeking help to get this to display the events booked by each person (like the 4th columns below):
+------------------------------------------------+
| ID | NAME | Total Bookings | Event IDs |
+------------------------------------------------+
| 7 | Bob | 3 | 5,4,3 |
| 5 | Jane | 2 | 4,3 |
| 3 | Joe | 1 | 5 |
+------------------------------------------------+
Could you please help me getting there.
Thanks.
GROUP_CONCAT https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
$query='
SELECT
bookings.person_id,
COUNT(bookings.person_id) AS total,
GROUP_CONCAT(bookings.event_id) as event_ids,
users.display_name
FROM bookings
INNER JOIN users ON bookings.person_id=users.id
WHERE users.id=bookings.person_id
GROUP BY bookings.person_id';
A bit different query but same result:
SELECT
bookings.person_id,
COUNT(
bookings.person_id
) AS total,
users.display_name,
GROUP_CONCAT(
bookings.event_id
ORDER BY
bookings.event_id
) AS events_list
FROM
bookings,
users
WHERE
bookings.person_id=users.id
GROUP BY
bookings.person_id
ORDER BY
bookings.person_id
I don't know if for a large data, the execution time is less, more or equal.
Hello I have actually asked a similar question a while ago but only just realized I did not get an answer that solves my problem.
I have 2 tables in a MySQL DB, that are connected by the same main id, the following code is just a simplified example of the original code:
table1:
+-----------------------+
| main_id | name | type |
+-----------------------+
table2:
+----------------------------------------+
| main_id | secondary_id | color | price |
+----------------------------------------+
the result I want to achieve is to get every row of table 1, and under each one list all the linked by main id rows from table2, for example:
table1 rows:
+-------------------+
| 1 | name1 | type1 |
| 2 | name2 | type2 |
| 3 | name3 | type3 |
+-------------------+
table2 rows:
+----+------+----------+------+
| 1 | 23 | red | 500 |
| 1 | 24 | blue | 600 |
| 1 | 25 | green | 700 |
| 2 | 26 | pink | 400 |
| 2 | 27 | purple | 200 |
| 3 | 28 | white | 100 |
+----+------+----------+------+
result in display:
<h1 class="1">name1, type1</h1>
<div class="23">red,500</div>
<div class="23">red,500</div>
<div class="24">green,700</div>
<h1 class="2">name2, type2</h1>
<div class="25">pink,400</div>
<div class="26">purple,200</div>
And so on...I was using a while loop inside a while loop which wasn't giving me the required result, then I was advised to use MySQL JOIN, but when I do I get all values of the matching ids and cant display the headings only once and then list the related results under it, can someone please help me?
this is a simplified query i was using:
while($rows = $headings->fetch_array(MYSQLI_BOTH))
{
$id = $rows['id'];
$conts_q = $mysqli->query("SELECT * FROM `table2` WHERE id='$id'");
$conts_numr = $conts_q->num_rows;
if($conts_numr==0)
{
//Display empty
}
else
{
while($row2 = $conts_q->fetch_array(MYSQLI_BOTH))
{
//Get details and display
}
}
}
In your description, you use main_id, but in the code you use just id. Not sure which you're really using here - you will have to edit the code below to match your actual column names. If the database column name is actually main_id, then for instance the line with $id = $rows['id'] would have to be $rows['main_id']. Or it won't work.
while($rows = $headings->fetch_array(MYSQLI_BOTH))
{
echo '<h1 class="'.$rows['id'].'">'.$rows['name1'].', '.$rows['type'].'</h1>';
$id = $rows['id'];
$conts_q = $mysqli->query("SELECT * FROM `table2` WHERE id='$id'");
$conts_numr = $conts_q->num_rows;
if($conts_numr==0)
{
//Display empty
}
else
{
while($row2 = $conts_q->fetch_array(MYSQLI_BOTH))
{
if ($row2['id'] == $id) {
echo '<div class="'.$row2['secondary_id'].'">'.$row2['color'].', '.$row2['price'].'</div>';
}
}//while
}//else
}//while
My data (simplified) looks like....
------------------------
| key | id | minspld |
------------------------
| 1 | 400 | 90 |
| 2 | 400 | 40 |
| 3 | 401 | 38 |
| 4 | 401 | 90 |
| 5 | 402 | 90 |
| 6 | 402 | 89 |
| 7 | 403 | 77 |
| 8 | 403 | 15 |
| 9 | 404 | 90 |
-----------------------
I am trying to do....
For each id, add all their minspld entries together
Display them in a table like above, but each id only showing once, and the minspld column showing the total per each id.
Here's what I'm using at the moment and I'm displaying all entries separately (eg, each person shows twice)
<table><thead><tr><th>ID</th><th>Mins Played</th></tr></thead>
<tbody>
<?php
$queryget = mysql_query("SELECT * FROM mlsstats ORDER BY id ASC") or die(mysql_error());
while ($row = mysql_fetch_assoc($queryget))
{
$id = $row['id'];
$minspld = $row['minspld'];
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>".$minspld."</td>";
echo "</tr>";
}
?>
</tbody></table>
How would I write this to make each id show only once in the HTML, but with the added totals of all their minspld entries? (eg, id 400 would have 130. id 401 would have 128. etc.)
If this isn't clear, please let me know.. and thanks for any help.
Please try changing your query to:
SELECT id, SUM(minspld) AS minspld FROM mlsstats GROUP BY id ORDER BY id ASC
You dont have to use a loop for this. You can simply do this with query
Just run the query and get two columns. id and its total
SELECT
m.id,
SUM(minspld) AS TCount
FROM mytable AS m
GROUP BY m.id