mysql database for messages Query for pulling number of unread messages(with query)











up vote
0
down vote

favorite












i have a database for messages and i am currently able to pull the amount of messages an individual user has sent and the user names with the DISTINCT count(*) query, But i am un able to now pull the messages that have a notread column. my question is from the query below can anybody see a quik fix to display the number of unread messages . my database has a READ coloumn with either yes or no input at message creation..



The below query pulls the user names and the amount of messages from each user and displays the username and messages amount number by the side..



I wish to however replace the amount of messages with amount of messages unread.



here is the code



$db = new PDO("mysql:host=localhost;dbname=messages", 'root', ''); // 1. set database with this instead of conect - or change conect to this

$query="SELECT DISTINCT messageaccountname,count(*) FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname ";

$stat=$db->prepare($query);

$stat->execute();

while($row = $stat->fetch()){

$messagenumber = $row['count(*)'];
$messageaccountname=$row['messageaccountname'];


if ($messageaccountname != $useraccountname){


echo"<div id='namecon' class='ubnames' onclick='selectmessage("{$messageaccountname}")'>{$messageaccountname} ({$messagenumber}) </div>";
}
}


im thinking i need to change the messageaccountname,count(*) WHERE listID=$listID GROUP BY messageaccountname



but i am unsure as to how i should then display the number of results,



if anyone can see what i need to do please let me know, as i have been looking all day for the answer and have not found it yet.



Thank you all.



Lez










share|improve this question






















  • Rarely (almost nil) we are needed to use both Distinct and Group By together.
    – Madhur Bhaiya
    Nov 7 at 19:44















up vote
0
down vote

favorite












i have a database for messages and i am currently able to pull the amount of messages an individual user has sent and the user names with the DISTINCT count(*) query, But i am un able to now pull the messages that have a notread column. my question is from the query below can anybody see a quik fix to display the number of unread messages . my database has a READ coloumn with either yes or no input at message creation..



The below query pulls the user names and the amount of messages from each user and displays the username and messages amount number by the side..



I wish to however replace the amount of messages with amount of messages unread.



here is the code



$db = new PDO("mysql:host=localhost;dbname=messages", 'root', ''); // 1. set database with this instead of conect - or change conect to this

$query="SELECT DISTINCT messageaccountname,count(*) FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname ";

$stat=$db->prepare($query);

$stat->execute();

while($row = $stat->fetch()){

$messagenumber = $row['count(*)'];
$messageaccountname=$row['messageaccountname'];


if ($messageaccountname != $useraccountname){


echo"<div id='namecon' class='ubnames' onclick='selectmessage("{$messageaccountname}")'>{$messageaccountname} ({$messagenumber}) </div>";
}
}


im thinking i need to change the messageaccountname,count(*) WHERE listID=$listID GROUP BY messageaccountname



but i am unsure as to how i should then display the number of results,



if anyone can see what i need to do please let me know, as i have been looking all day for the answer and have not found it yet.



Thank you all.



Lez










share|improve this question






















  • Rarely (almost nil) we are needed to use both Distinct and Group By together.
    – Madhur Bhaiya
    Nov 7 at 19:44













up vote
0
down vote

favorite









up vote
0
down vote

favorite











i have a database for messages and i am currently able to pull the amount of messages an individual user has sent and the user names with the DISTINCT count(*) query, But i am un able to now pull the messages that have a notread column. my question is from the query below can anybody see a quik fix to display the number of unread messages . my database has a READ coloumn with either yes or no input at message creation..



The below query pulls the user names and the amount of messages from each user and displays the username and messages amount number by the side..



I wish to however replace the amount of messages with amount of messages unread.



here is the code



$db = new PDO("mysql:host=localhost;dbname=messages", 'root', ''); // 1. set database with this instead of conect - or change conect to this

$query="SELECT DISTINCT messageaccountname,count(*) FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname ";

$stat=$db->prepare($query);

$stat->execute();

while($row = $stat->fetch()){

$messagenumber = $row['count(*)'];
$messageaccountname=$row['messageaccountname'];


if ($messageaccountname != $useraccountname){


echo"<div id='namecon' class='ubnames' onclick='selectmessage("{$messageaccountname}")'>{$messageaccountname} ({$messagenumber}) </div>";
}
}


im thinking i need to change the messageaccountname,count(*) WHERE listID=$listID GROUP BY messageaccountname



but i am unsure as to how i should then display the number of results,



if anyone can see what i need to do please let me know, as i have been looking all day for the answer and have not found it yet.



Thank you all.



Lez










share|improve this question













i have a database for messages and i am currently able to pull the amount of messages an individual user has sent and the user names with the DISTINCT count(*) query, But i am un able to now pull the messages that have a notread column. my question is from the query below can anybody see a quik fix to display the number of unread messages . my database has a READ coloumn with either yes or no input at message creation..



The below query pulls the user names and the amount of messages from each user and displays the username and messages amount number by the side..



I wish to however replace the amount of messages with amount of messages unread.



here is the code



$db = new PDO("mysql:host=localhost;dbname=messages", 'root', ''); // 1. set database with this instead of conect - or change conect to this

$query="SELECT DISTINCT messageaccountname,count(*) FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname ";

$stat=$db->prepare($query);

$stat->execute();

while($row = $stat->fetch()){

$messagenumber = $row['count(*)'];
$messageaccountname=$row['messageaccountname'];


if ($messageaccountname != $useraccountname){


echo"<div id='namecon' class='ubnames' onclick='selectmessage("{$messageaccountname}")'>{$messageaccountname} ({$messagenumber}) </div>";
}
}


im thinking i need to change the messageaccountname,count(*) WHERE listID=$listID GROUP BY messageaccountname



but i am unsure as to how i should then display the number of results,



if anyone can see what i need to do please let me know, as i have been looking all day for the answer and have not found it yet.



Thank you all.



Lez







php html mysql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 13:46









lezz

205




205












  • Rarely (almost nil) we are needed to use both Distinct and Group By together.
    – Madhur Bhaiya
    Nov 7 at 19:44


















  • Rarely (almost nil) we are needed to use both Distinct and Group By together.
    – Madhur Bhaiya
    Nov 7 at 19:44
















Rarely (almost nil) we are needed to use both Distinct and Group By together.
– Madhur Bhaiya
Nov 7 at 19:44




Rarely (almost nil) we are needed to use both Distinct and Group By together.
– Madhur Bhaiya
Nov 7 at 19:44












1 Answer
1






active

oldest

votes

















up vote
0
down vote













i accept your READ column type is varchar or enum with values YES and NO. so if you change your query like below you can get the unreaded messages count



SELECT DISTINCT messageaccountname,count(*), sum(if(read = "NO", 1, 0)) unread_count FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname


hope will help you






share|improve this answer





















  • thank you, i will try this,(unfortnatly i get blank results)
    – lezz
    Nov 7 at 14:18












  • I only wish to change $messagenumber = $row['count(*)']; to display a number of unread messages
    – lezz
    Nov 7 at 14:23










  • Also note that DISTINCT has no useful meaning here. A GROUP BY query will return exactly one row per group.
    – Mike Robinson
    Nov 7 at 14:35










  • ok thank you. il ammend my query. thanks
    – lezz
    Nov 7 at 15:10











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190739%2fmysql-database-for-messages-query-for-pulling-number-of-unread-messageswith-que%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








up vote
0
down vote













i accept your READ column type is varchar or enum with values YES and NO. so if you change your query like below you can get the unreaded messages count



SELECT DISTINCT messageaccountname,count(*), sum(if(read = "NO", 1, 0)) unread_count FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname


hope will help you






share|improve this answer





















  • thank you, i will try this,(unfortnatly i get blank results)
    – lezz
    Nov 7 at 14:18












  • I only wish to change $messagenumber = $row['count(*)']; to display a number of unread messages
    – lezz
    Nov 7 at 14:23










  • Also note that DISTINCT has no useful meaning here. A GROUP BY query will return exactly one row per group.
    – Mike Robinson
    Nov 7 at 14:35










  • ok thank you. il ammend my query. thanks
    – lezz
    Nov 7 at 15:10















up vote
0
down vote













i accept your READ column type is varchar or enum with values YES and NO. so if you change your query like below you can get the unreaded messages count



SELECT DISTINCT messageaccountname,count(*), sum(if(read = "NO", 1, 0)) unread_count FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname


hope will help you






share|improve this answer





















  • thank you, i will try this,(unfortnatly i get blank results)
    – lezz
    Nov 7 at 14:18












  • I only wish to change $messagenumber = $row['count(*)']; to display a number of unread messages
    – lezz
    Nov 7 at 14:23










  • Also note that DISTINCT has no useful meaning here. A GROUP BY query will return exactly one row per group.
    – Mike Robinson
    Nov 7 at 14:35










  • ok thank you. il ammend my query. thanks
    – lezz
    Nov 7 at 15:10













up vote
0
down vote










up vote
0
down vote









i accept your READ column type is varchar or enum with values YES and NO. so if you change your query like below you can get the unreaded messages count



SELECT DISTINCT messageaccountname,count(*), sum(if(read = "NO", 1, 0)) unread_count FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname


hope will help you






share|improve this answer












i accept your READ column type is varchar or enum with values YES and NO. so if you change your query like below you can get the unreaded messages count



SELECT DISTINCT messageaccountname,count(*), sum(if(read = "NO", 1, 0)) unread_count FROM `messagedatabase` WHERE `listID`='$listID' GROUP BY messageaccountname


hope will help you







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 14:16









ilazmi

61




61












  • thank you, i will try this,(unfortnatly i get blank results)
    – lezz
    Nov 7 at 14:18












  • I only wish to change $messagenumber = $row['count(*)']; to display a number of unread messages
    – lezz
    Nov 7 at 14:23










  • Also note that DISTINCT has no useful meaning here. A GROUP BY query will return exactly one row per group.
    – Mike Robinson
    Nov 7 at 14:35










  • ok thank you. il ammend my query. thanks
    – lezz
    Nov 7 at 15:10


















  • thank you, i will try this,(unfortnatly i get blank results)
    – lezz
    Nov 7 at 14:18












  • I only wish to change $messagenumber = $row['count(*)']; to display a number of unread messages
    – lezz
    Nov 7 at 14:23










  • Also note that DISTINCT has no useful meaning here. A GROUP BY query will return exactly one row per group.
    – Mike Robinson
    Nov 7 at 14:35










  • ok thank you. il ammend my query. thanks
    – lezz
    Nov 7 at 15:10
















thank you, i will try this,(unfortnatly i get blank results)
– lezz
Nov 7 at 14:18






thank you, i will try this,(unfortnatly i get blank results)
– lezz
Nov 7 at 14:18














I only wish to change $messagenumber = $row['count(*)']; to display a number of unread messages
– lezz
Nov 7 at 14:23




I only wish to change $messagenumber = $row['count(*)']; to display a number of unread messages
– lezz
Nov 7 at 14:23












Also note that DISTINCT has no useful meaning here. A GROUP BY query will return exactly one row per group.
– Mike Robinson
Nov 7 at 14:35




Also note that DISTINCT has no useful meaning here. A GROUP BY query will return exactly one row per group.
– Mike Robinson
Nov 7 at 14:35












ok thank you. il ammend my query. thanks
– lezz
Nov 7 at 15:10




ok thank you. il ammend my query. thanks
– lezz
Nov 7 at 15:10


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190739%2fmysql-database-for-messages-query-for-pulling-number-of-unread-messageswith-que%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini