PHP/SQL - How can I display distinct values and the number of occurrences?
So I am currently using PHP to pull the bodies from emails, parse the data, and insert the data into a user-created DB. I want to display an overview of the data (that is inside the DB) using PHP. Here is an example of what I want to achieve:
This is my table:
Table Name: bldgSensors
sensor_code | temp_limit | current_temp
---------------------------------------
0102260100A | 55 | 45
0102260100B | 55 | 50
0102260100A | 55 | 48
Desired output using PHP:
Sensors Count
0102260100A 2
0102260100B 1
So far, I can find distinct values but cannot output the total count:
$result1 = mysqli_query($DBconn,"SELECT DISTINCT sensor_code FROM bldgSensors");
echo "<table border='1'>
<tr>
<th>Distinct sensor codes</th>
<th>Count</th>
</tr>";
while($row1 = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $row1['sensor_code'] ."</td>";
}
echo "</table>";
mysqli_close($DBconn);
Thanks so much in advance! This will help tremendously!
php sql count distinct
add a comment |
So I am currently using PHP to pull the bodies from emails, parse the data, and insert the data into a user-created DB. I want to display an overview of the data (that is inside the DB) using PHP. Here is an example of what I want to achieve:
This is my table:
Table Name: bldgSensors
sensor_code | temp_limit | current_temp
---------------------------------------
0102260100A | 55 | 45
0102260100B | 55 | 50
0102260100A | 55 | 48
Desired output using PHP:
Sensors Count
0102260100A 2
0102260100B 1
So far, I can find distinct values but cannot output the total count:
$result1 = mysqli_query($DBconn,"SELECT DISTINCT sensor_code FROM bldgSensors");
echo "<table border='1'>
<tr>
<th>Distinct sensor codes</th>
<th>Count</th>
</tr>";
while($row1 = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $row1['sensor_code'] ."</td>";
}
echo "</table>";
mysqli_close($DBconn);
Thanks so much in advance! This will help tremendously!
php sql count distinct
Possible duplicate of stackoverflow.com/questions/1346345/…
– misorude
Nov 13 '18 at 9:07
That duplicate could btw. easily be found by copy&pasting the question title you have chosen into Google verbatim … So please make a proper research effort before asking next time.
– misorude
Nov 13 '18 at 9:08
add a comment |
So I am currently using PHP to pull the bodies from emails, parse the data, and insert the data into a user-created DB. I want to display an overview of the data (that is inside the DB) using PHP. Here is an example of what I want to achieve:
This is my table:
Table Name: bldgSensors
sensor_code | temp_limit | current_temp
---------------------------------------
0102260100A | 55 | 45
0102260100B | 55 | 50
0102260100A | 55 | 48
Desired output using PHP:
Sensors Count
0102260100A 2
0102260100B 1
So far, I can find distinct values but cannot output the total count:
$result1 = mysqli_query($DBconn,"SELECT DISTINCT sensor_code FROM bldgSensors");
echo "<table border='1'>
<tr>
<th>Distinct sensor codes</th>
<th>Count</th>
</tr>";
while($row1 = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $row1['sensor_code'] ."</td>";
}
echo "</table>";
mysqli_close($DBconn);
Thanks so much in advance! This will help tremendously!
php sql count distinct
So I am currently using PHP to pull the bodies from emails, parse the data, and insert the data into a user-created DB. I want to display an overview of the data (that is inside the DB) using PHP. Here is an example of what I want to achieve:
This is my table:
Table Name: bldgSensors
sensor_code | temp_limit | current_temp
---------------------------------------
0102260100A | 55 | 45
0102260100B | 55 | 50
0102260100A | 55 | 48
Desired output using PHP:
Sensors Count
0102260100A 2
0102260100B 1
So far, I can find distinct values but cannot output the total count:
$result1 = mysqli_query($DBconn,"SELECT DISTINCT sensor_code FROM bldgSensors");
echo "<table border='1'>
<tr>
<th>Distinct sensor codes</th>
<th>Count</th>
</tr>";
while($row1 = mysqli_fetch_array($result1))
{
echo "<tr>";
echo "<td>" . $row1['sensor_code'] ."</td>";
}
echo "</table>";
mysqli_close($DBconn);
Thanks so much in advance! This will help tremendously!
php sql count distinct
php sql count distinct
edited Nov 13 '18 at 3:02
asked Nov 13 '18 at 2:57
Chandler Phillips
92
92
Possible duplicate of stackoverflow.com/questions/1346345/…
– misorude
Nov 13 '18 at 9:07
That duplicate could btw. easily be found by copy&pasting the question title you have chosen into Google verbatim … So please make a proper research effort before asking next time.
– misorude
Nov 13 '18 at 9:08
add a comment |
Possible duplicate of stackoverflow.com/questions/1346345/…
– misorude
Nov 13 '18 at 9:07
That duplicate could btw. easily be found by copy&pasting the question title you have chosen into Google verbatim … So please make a proper research effort before asking next time.
– misorude
Nov 13 '18 at 9:08
Possible duplicate of stackoverflow.com/questions/1346345/…
– misorude
Nov 13 '18 at 9:07
Possible duplicate of stackoverflow.com/questions/1346345/…
– misorude
Nov 13 '18 at 9:07
That duplicate could btw. easily be found by copy&pasting the question title you have chosen into Google verbatim … So please make a proper research effort before asking next time.
– misorude
Nov 13 '18 at 9:08
That duplicate could btw. easily be found by copy&pasting the question title you have chosen into Google verbatim … So please make a proper research effort before asking next time.
– misorude
Nov 13 '18 at 9:08
add a comment |
1 Answer
1
active
oldest
votes
You can do that by modifying your query. Use the COUNT and GROUP BY sql functions:
Try this:
$query = "SELECT
sensor_code,
COUNT(sensor_code) AS sensorCount
FROM bldgSensors
GROUP BY sensor_code
ORDER BY sensorCount DESC";
$result1 = mysqli_query($DBconn, $query);
echo
'<table border="1">
<tr>
<th>Distinct sensor codes</th>
<th>Count</th>
</tr>';
while($row1 = mysqli_fetch_array($result1)){
echo
'<tr>' .
'<td>' . $row1['sensor_code'] . '</td>' .
'<td>' . $row1['sensorCount'] . '</td>' .
'</tr>';
}
echo
'</table>';
mysqli_close($DBconn);
Wow this worked. Thank you so much!! If I could upvote this 1000 times I would.
– Chandler Phillips
Nov 13 '18 at 3:44
Hey no problem :) Hope the rest of your project goes well. ~Cheers!
– Joseph_J
Nov 13 '18 at 3:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273131%2fphp-sql-how-can-i-display-distinct-values-and-the-number-of-occurrences%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do that by modifying your query. Use the COUNT and GROUP BY sql functions:
Try this:
$query = "SELECT
sensor_code,
COUNT(sensor_code) AS sensorCount
FROM bldgSensors
GROUP BY sensor_code
ORDER BY sensorCount DESC";
$result1 = mysqli_query($DBconn, $query);
echo
'<table border="1">
<tr>
<th>Distinct sensor codes</th>
<th>Count</th>
</tr>';
while($row1 = mysqli_fetch_array($result1)){
echo
'<tr>' .
'<td>' . $row1['sensor_code'] . '</td>' .
'<td>' . $row1['sensorCount'] . '</td>' .
'</tr>';
}
echo
'</table>';
mysqli_close($DBconn);
Wow this worked. Thank you so much!! If I could upvote this 1000 times I would.
– Chandler Phillips
Nov 13 '18 at 3:44
Hey no problem :) Hope the rest of your project goes well. ~Cheers!
– Joseph_J
Nov 13 '18 at 3:46
add a comment |
You can do that by modifying your query. Use the COUNT and GROUP BY sql functions:
Try this:
$query = "SELECT
sensor_code,
COUNT(sensor_code) AS sensorCount
FROM bldgSensors
GROUP BY sensor_code
ORDER BY sensorCount DESC";
$result1 = mysqli_query($DBconn, $query);
echo
'<table border="1">
<tr>
<th>Distinct sensor codes</th>
<th>Count</th>
</tr>';
while($row1 = mysqli_fetch_array($result1)){
echo
'<tr>' .
'<td>' . $row1['sensor_code'] . '</td>' .
'<td>' . $row1['sensorCount'] . '</td>' .
'</tr>';
}
echo
'</table>';
mysqli_close($DBconn);
Wow this worked. Thank you so much!! If I could upvote this 1000 times I would.
– Chandler Phillips
Nov 13 '18 at 3:44
Hey no problem :) Hope the rest of your project goes well. ~Cheers!
– Joseph_J
Nov 13 '18 at 3:46
add a comment |
You can do that by modifying your query. Use the COUNT and GROUP BY sql functions:
Try this:
$query = "SELECT
sensor_code,
COUNT(sensor_code) AS sensorCount
FROM bldgSensors
GROUP BY sensor_code
ORDER BY sensorCount DESC";
$result1 = mysqli_query($DBconn, $query);
echo
'<table border="1">
<tr>
<th>Distinct sensor codes</th>
<th>Count</th>
</tr>';
while($row1 = mysqli_fetch_array($result1)){
echo
'<tr>' .
'<td>' . $row1['sensor_code'] . '</td>' .
'<td>' . $row1['sensorCount'] . '</td>' .
'</tr>';
}
echo
'</table>';
mysqli_close($DBconn);
You can do that by modifying your query. Use the COUNT and GROUP BY sql functions:
Try this:
$query = "SELECT
sensor_code,
COUNT(sensor_code) AS sensorCount
FROM bldgSensors
GROUP BY sensor_code
ORDER BY sensorCount DESC";
$result1 = mysqli_query($DBconn, $query);
echo
'<table border="1">
<tr>
<th>Distinct sensor codes</th>
<th>Count</th>
</tr>';
while($row1 = mysqli_fetch_array($result1)){
echo
'<tr>' .
'<td>' . $row1['sensor_code'] . '</td>' .
'<td>' . $row1['sensorCount'] . '</td>' .
'</tr>';
}
echo
'</table>';
mysqli_close($DBconn);
edited Nov 13 '18 at 3:44
answered Nov 13 '18 at 3:26
Joseph_J
3,0322620
3,0322620
Wow this worked. Thank you so much!! If I could upvote this 1000 times I would.
– Chandler Phillips
Nov 13 '18 at 3:44
Hey no problem :) Hope the rest of your project goes well. ~Cheers!
– Joseph_J
Nov 13 '18 at 3:46
add a comment |
Wow this worked. Thank you so much!! If I could upvote this 1000 times I would.
– Chandler Phillips
Nov 13 '18 at 3:44
Hey no problem :) Hope the rest of your project goes well. ~Cheers!
– Joseph_J
Nov 13 '18 at 3:46
Wow this worked. Thank you so much!! If I could upvote this 1000 times I would.
– Chandler Phillips
Nov 13 '18 at 3:44
Wow this worked. Thank you so much!! If I could upvote this 1000 times I would.
– Chandler Phillips
Nov 13 '18 at 3:44
Hey no problem :) Hope the rest of your project goes well. ~Cheers!
– Joseph_J
Nov 13 '18 at 3:46
Hey no problem :) Hope the rest of your project goes well. ~Cheers!
– Joseph_J
Nov 13 '18 at 3:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273131%2fphp-sql-how-can-i-display-distinct-values-and-the-number-of-occurrences%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of stackoverflow.com/questions/1346345/…
– misorude
Nov 13 '18 at 9:07
That duplicate could btw. easily be found by copy&pasting the question title you have chosen into Google verbatim … So please make a proper research effort before asking next time.
– misorude
Nov 13 '18 at 9:08