Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Can anyone help with joining these tables?
Old 06-09-2008, 03:25 PM Can anyone help with joining these tables?
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Hi,

I have a messages table and a friends table and I am using the following queriers to display the number of messages and buddy requests for the user that is currently logged in.

Since the userid is the same in the where clause in both queries should they be joined? I can join basic tables but I'm not sure how to join these tables correctly, can anyone show me how they would do it?

Any help will be appreciated

PHP Code:
<?
// Get number of messages
$messagesNummysql_num_rows(mysql_query("SELECT userto FROM messages WHERE userto = '$_SESSION[userid]' and status='unread'"));
 
// Get number of friend requests
$buddyrequestsmysql_num_rows(mysql_query("SELECT * FROM friends WHERE friend1='$_SESSION[userid]' and status='pending'"));
drew22299 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-13-2008, 03:43 PM Re: Can anyone help with joining these tables?
Banned

Posts: 421
Location: Boston, MA
Trades: 1
I don't know much of mySql, I will give you the code for MS SQL you can convert it to mySql

Select M.userto from Messages M Join Friends F ON M.userto=F.friend1
Where M.userto='$_SESSION[userid]' and M.status='unread'
AND F.
status='pending'
webcosmo is offline
Reply With Quote
View Public Profile Visit webcosmo's homepage!
 
Old 06-13-2008, 04:42 PM Re: Can anyone help with joining these tables?
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
If you're justing wanting to display counts, there's no reason you should be loading all that data with your queries. But I wouldn't make one query out of it, since you're just counting from two tables, a join would be rather inefficient I would think.

Try this:
Code:
"SELECT count(status) AS thecount FROM messages WHERE userto = '$_SESSION[userid]' and status='unread'"
Let the database do the heavylifting for you, then you just have to read one variable =)
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Old 06-16-2008, 05:05 PM Re: Can anyone help with joining these tables?
drew22299's Avatar
Skilled Talker

Posts: 93
Trades: 0
Thanks
drew22299 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Can anyone help with joining these tables?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23441 seconds with 12 queries