Jump to content

one query instead of two


squiblo

Recommended Posts

How can I make the following two queries into just 1 query so I dont have to divide by 2 at the end.

 

this is what the db table looks like....

 

|| user_id  ||  page_zebra_id  ||  friend  ||  foe ||

        2                    53                    1            0

        53                    2                    1            0

 

 

$friend_check = mysql_query("SELECT * FROM page_zebra WHERE user_id='$user_id' AND friend=1");
$first_friends = mysql_numrows($friend_check);

$friend_check = mysql_query("SELECT * FROM page_zebra WHERE page_zebra_id='$user_id' AND friend=1");
$second_friends = mysql_numrows($friend_check);

$total_friends = ($first_friends + $second_friends) / 2;

Link to comment
https://forums.phpfreaks.com/topic/190841-one-query-instead-of-two/
Share on other sites

i have put

$friend_check = mysql_query("SELECT (COUNT(*) / 2) AS totalFriends FROM page_zebra WHERE (page_zebra_id='$user_id' OR user_id='$user_id') AND friend=1");
$total_friends = mysql_numrows($friend_check);

 

but get this error

Parse error: syntax error, unexpected ')'

In which line? Syntax of these two is correct.

 

And besides, this query will return only one row, but with value you need already calculated.

 

$friend_check = mysql_query("SELECT (COUNT(*) / 2) AS totalFriends FROM page_zebra WHERE (page_zebra_id='$user_id' OR user_id='$user_id') AND friend=1");
$row = mysql_fetch_assoc($friend_check);
$total_friends = $row['totalFriends'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.