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
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 ')'
Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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.