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
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 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.
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.
I need to search following result .
singer_id=2
---------
B9 - song's list
song_au1
song10
p7 - song's list
songx
song11
11T- song list
song 35
The result is actully have following requirements.
1. show all songs by singer_id=2 order by (custom arrangement may be some or other table).
and because i'm using PHP, I want to show result the above format..
top - singer name
then it's album type
and all songs of that album
2nd album type
and all songs of that album
.....
I'm too much confused..
can somebody please tell me the best query!
table i've
================================================
| u_id | u_title | u_song_type | singer_id |
|================================================
| 1 | song1 | p7 | 1 |
| 2 | songx | p7 | 2 |
| 3 | songpo | p7 | 9 |
| 4 | song_dum3| 11T | 2 |
| 5 | song_01 | p7 | 3 |
| 6 | song_au1 | B9 | 2 |
| 7 | song11 | p7 | 2 |
| 8 | song35 | 11T | 1 |
| 9 | song10 | B9 | 2 |
-------------------------------------------------
Thank
After a long effort of hours. I have resolved it.
At the end I got It was easy.
$artistId = '2';
echo 'Singer Id:' . $row["artistName"];
//following query selects all records and sort records in a why i need that.
$sql2 = "SELECT * FROM album
ORDER BY
FIELD(u_song_type, 'B9','p7','11T')";
$result2 = mysqli_query($con,$sql2);
//An array is defined, to filter the title only once if
//repeated occurrence
$songTypesArray = array();
while($row2 = mysqli_fetch_array($result2)) {
if (in_array($row2['u_song_type'], $songTypesArray)) {
//if song Type is found in the array, skip it.
//I left it empty. if found in array
}
else{
//if song type is not found in the arry we'll push
// song type into array
array_push($songTypesArray,$row2['u_song_type']);
// song type only appears once if it is same
// in all song title.
echo $row2['u_song_type'];
}
//at the end song title appears.
echo $row2["u_title"];
}
}
This took whole night of mind in resolution and finally it is there. I hope people would get enough help from here
Thanks
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