Jump to content

Recommended Posts

Hello guys after several hours of trying to combine two queries ive had to admit defeat, does anyone know of a way to combine these two queries so i can lessen the load on the pagea and server its self, iany help is greatly appretiated.

 


$result = mysql_query("SELECT unread FROM messages WHERE unread = 'unread' and receiver = '$session->username'");
$num_rows = mysql_num_rows($result);

$view_msg = mysql_query("SELECT received FROM messages WHERE received = '0' and receiver = '$session->username'");
$msg = mysql_num_rows($view_msg);

 

Thanks rick

Link to comment
https://forums.phpfreaks.com/topic/159955-solved-combining-two-queries/
Share on other sites

I don't think you can interpolate complex PHP statements like that.

<?php
$result = mysql_query("SELECT unread FROM messages WHERE unread = 'unread' and receiver = '".$session->username."'");
$num_rows = mysql_num_rows($result);

$view_msg = mysql_query("SELECT received FROM messages WHERE received = '0' and receiver = '".$session->username."'");
$msg = mysql_num_rows($view_msg);

 

But combining them -

<?php
$sql = 'SELECT received, unread FROM messages WHERE received = "0" AND unread = "unread" AND receiver = "'.$session->username.'";';

so if i use

 

$sql = 'SELECT received, unread FROM messages WHERE received = "0" AND unread = "unread" AND receiver = "'.$session->username.'";';

 

how would i get the info i require from that as currently im using

 

$result = mysql_query("SELECT unread FROM messages WHERE unread = 'unread' and receiver = '$session->username'");
$num_rows = mysql_num_rows($result);

$view_msg = mysql_query("SELECT received FROM messages WHERE received = '0' and receiver = '$session->username'");
$msg = mysql_num_rows($view_msg);

$consoleOutput = "<font face='Verdana' color='#FFFFFF' size='2'>Welcome back <b>$session->username</b>,</font>
<a href='index.php?page=messages&option=inbox'>Private Messages</a> (Unread " .$msg. ", Total " .$num_rows. ")";

 

Sorry to drag this out as i said im learning so trying to cover all angles

While Maq works his magic, i thought i add an idea,

Add two fields to the users table called msg_unread and msg_recieved,

when the user sends a message just add to that field as well, and when the user views his messages run those queries and update the users fields

 

or maybe try (untested)

<?php
$sql = 'SELECT COUNT(received) as numreceived, COUNT(unread) as numunread FROM messages WHERE (received = "0" OR unread = "unread") AND receiver = "'.$session->username.'" GROUP BY receiver  ';
?>

 

Uh... you really should really clarify what you need.

 

Try this -

<?php
$sql = 'SELECT (SELECT COUNT(received) FROM messages WHERE received = "0" AND receiver = "' . $session->username . '") AS received), (SELECT COUNT(unread) FROM messages WHERE unread = "unread" AND receiver = "' . $session->username . '") AS unread;';

Ok let me try and explain my problem,

 

with what you gave

 

$sql = 'SELECT (SELECT COUNT(received) FROM messages WHERE received = "0" AND receiver = "' . $session->username . '") AS received), (SELECT COUNT(unread) FROM messages WHERE unread = "unread" AND receiver = "' . $session->username . '") AS unread;';

 

Im curious how i would go about getting my results from that as right now im using,

 

$num_rows = mysql_num_rows($result);

 

To get the ammount of pm's the user has in the inbox total and im using,

 

$msg = mysql_num_rows($view_msg);

 

to show the user how many unread pms they have, so using what you showed me how do i get those two results ? sorry if im being confusing but im trying my best to explain something im unsure how to do :(

 

 

Uh you don't.

 

<?php
$sql = 'SELECT (SELECT COUNT(received) FROM messages WHERE received = "0" AND receiver = "' . $session->username . '") AS received), (SELECT COUNT(unread) FROM messages WHERE unread = "unread" AND receiver = "' . $session->username . '") AS unread;';

$results = mysql_query($sql);
echo $results['received'];

echo $results['unread'];

:-\

Try this Example

<?php
$sql = 'SELECT COUNT(received) as numreceived, COUNT(unread) as numunread FROM messages WHERE (received = "0" OR unread = "unread") AND receiver = "'.$session->username.'" GROUP BY receiver  ';

$result = mysql_query($sql);
$row = mysql_fetch_assoc($result)

$consoleOutput = "<font face='Verdana' color='#FFFFFF' size='2'>Welcome back <b>$session->username</b>,</font>
<a href='index.php?page=messages&option=inbox'>Private Messages</a> (Unread {$row['numunread']}, Total {$row['numreceived']})"; 
?>

 

stelthius You may want to try a performace test on both queries.. to see which one suite best,

 

Ahh true, i was going to do a self join, but it occured to me a simpler and quicker route would be instead of running 2 or 3 selects or joins just add them up!

 

<?php
$sql = 'SELECT SUM( received ) AS numreceived, SUM( if(unread="unread",1,0 )) AS numunread FROM messages WHERE receiver = {$session->username}';

$result = mysql_query($sql);
$row = mysql_fetch_assoc($result)

$consoleOutput = "<font face='Verdana' color='#FFFFFF' size='2'>Welcome back <b>$session->username</b>,</font>
<a href='index.php?page=messages&option=inbox'>Private Messages</a> (Unread {$row['numunread']}, Total {$row['numreceived']})"; 
?>

 

EDIT oops left a limit on it LOL

This is true BUT the value is being converted into a 1 or 0 1=found 0= not found (kinda) if unread="unread" it get set to 1, which is the same as the record so if 500 records get set to 1 then thats 1x500 = 500 :)

i assumes recieved was always one but you can do the same

if(received > 0,1,0 )

This is true BUT the value is being converted into a 1 or 0 1=found 0= not found (kinda) if unread="unread" it get set to 1, which is the same as the record so if 500 records get set to 1 then thats 1x500 = 500 :)

i assumes recieved was always one but you can do the same

if(received > 0,1,0 )

 

Yes I see, excuse me.  It hould work, that's some nifty hackery Mad.  ;)

Nice one MadTechie. Two things though, you should check if received is 0 (as said by the OP) and you should put quotes around the username just in case a space is allowed.

 

Oh BALLS

 

update:

$sql = "SELECT SUM( received ) AS numreceived, SUM( if(unread="unread",1,0 )) AS numunread FROM messages WHERE receiver = '{$session->username}' LIMIT 1";

 

 

@Maq :D

Yeah kinda pleased with that one

 

 

EDIT: i added Limit 1 as it should be okay.. but its 2:40am so i could be wrong!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.