Jump to content

Who's friends with who or whom?


bytesize

Recommended Posts

Table friends

users   

Fred

Tom

Julie

Henry

Bill

Wally

Joe

Joe

friendwith

Joe

Joe

Joe

Joe

Joe

Joe

Julie

Wally

Fred, Tom, Julie, Henry, Bill, and Wally have friended Joe.

<?php
$username = 'Joe';
$query = "SELECT * FROM friends WHERE friendwith=' $username'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
    echo $row['users']."<br/>";
    echo $row['friendwith']."<br/>";
}
?>

 

Joe is friends with Julie and Wally.

<?php
$username = 'Joe';
$query = "SELECT * FROM friends WHERE users='$username'"; 
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
    echo $row['users']."<br/>";
    echo $row['friendwith']."<br/>";
}
?>

 

Here's where I need your expert advice!

I want to know who friended Joe except for those that Joe has friended.

In other words, I want to show Fred, Tom, Henry, and Bill.

Julie and Wally should not be included in the query because Joe has friended them as well.

 

Link to comment
Share on other sites

Table users

code

Fred

Julie

Bill

Wally

Joe

Table friends

f_userid   

Fred

Julie

Bill

Wally

Joe

Joe

friendwith

Joe

Joe

Joe

Joe

Julie

Wally

 

Joe is friends with Julie and Wally.

<?php	
$username = 'Joe';		
$myfriends = "SELECT * 
	FROM 
		users 
	JOIN 
		friends 
	ON 
		users.code = friends.friendwith
	WHERE 
		friends.f_userid ='$username'";
$myfriendsresult	= mysql_query($myfriends);
while($myfriendsrow = mysql_fetch_array($myfriendsresult))
{
echo $myfriendsrow['f_userid']."<br/>";
echo $myfriendsrow['friendwith']."<br/>";
}
?>

 

Fred, Julie, Bill, and Wally have friended Joe.

<?php	
$username = 'Joe';
$query = "SELECT * 
	FROM 
		users 
	JOIN 
		friends 
	ON 
		users.code = friends.f_userid 
	WHERE 
		friends.friendwith='$username'";
$result	= mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo $row['f_userid']."<br/>";
echo $row['friendwith']."<br/>";
}
?>

Julie and Wally should not be included in the query because Joe has friended them as well.

I want to know who friended Joe except for those that Joe has friended.

In other words, I want to show Fred and Bill.

Julie and Wally should not be included in the query because Joe has friended them as well.

============================================

I've included my attempt at creating a subcategory.

This subcategory shows Fred and Bill plus Joe the User because Joe is friends with Julie and Wally.

I need to show only Fred and Bill.

<?php	
$query = "SELECT DISTINCT * FROM users JOIN friends ON users.code = friends.f_userid 
	WHERE NOT EXISTS (SELECT * FROM friends WHERE users.code = friends.friendwith AND friends.f_userid='$username')";
$result	= mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo $row['f_userid']."<br/>";
echo $row['friendwith']."<br/>";
}
?>

Pointing me in the right direction will be greatly appreciated.

Link to comment
Share on other sites

users is a table and code is a column in users.

SELECT users, friendwith FROM friends WHERE users <> 'Joe' AND users NOT IN (SELECT friendwith FROM friends WHERE friendwith <> 'Joe')

 

I tried this but it doesn't work.

SELECT code, friendwith FROM friends, users WHERE code <> 'Joe' AND code NOT IN (SELECT friendwith FROM friends WHERE friendwith <> 'Joe')

Link to comment
Share on other sites

Yes, I only need table friends, but table users is needed to display the images of those that are not friends with f_userid. I can add table users after solving friends table.

 

Still not working.

SELECT f_userid, friendwith FROM friends WHERE f_userid <> 'Joe' AND f_userid NOT IN (SELECT friendwith FROM friends WHERE friendwith <> 'Joe')

Link to comment
Share on other sites

SELECT f_userid, friendwith FROM friends WHERE f_userid <> 'Joe' AND f_userid NOT IN (SELECT friendwith FROM friends WHERE friendwith <> 'Joe')

 

This must work otherwise you're doing something wrong. What you're getting?

Link to comment
Share on other sites

I copied your query and nothing happens because f_userid and friendwith both contain Joe. Doesn't <> represent not equal to?

SELECT f_userid, friendwith FROM friends WHERE f_userid <> 'Joe' AND f_userid NOT IN (SELECT friendwith FROM friends WHERE friendwith <> 'Joe')

When I echo the query, I get the query with Joe.

Link to comment
Share on other sites

Table friends

f_userid   

Fred

Tom

Henry

Bill

Julie

Wally

Joe

Joe

 

friendwith

Joe

Joe

Joe

Joe

Joe

Joe

Julie

Wally

This what your table should look like. Joe is in both columns because Joe friended Julie and Wally but not the others.

I get the same query you get when it echos out on my page.

This is why I'm having trouble with the query.

Link to comment
Share on other sites

This is my php.

<?php
$username = 'Joe';
$query = "SELECT f_userid, friendwith FROM friends WHERE f_userid <> '$username' AND f_userid NOT IN (SELECT friendwith FROM friends WHERE friendwith <> '$username')";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
   echo $row['f_userid']."<br/>";
   echo $row['friendwith']."<br/>";
}
?>

 

This is what I get when I echo the $query.

SELECT f_userid, friendwith FROM friends WHERE f_userid <> 'Joe' AND f_userid NOT IN (SELECT friendwith FROM friends WHERE friendwith <> 'Joe')

 

Nothing echos out for $row['f_userid'] or $row['friendwith'].

 

If I change the query to:

SELECT f_userid, friendwith FROM friends WHERE f_userid = 'Joe' AND f_userid NOT IN (SELECT friendwith FROM friends WHERE friendwith <> 'Joe')

 

$row['f_userid'] and $row['friendwith'] echo out content form table friends using the query with =.

Link to comment
Share on other sites

try

<?php	
$username = 'Joe';		
$myfriends = "SELECT * 
	FROM 
		users 
	JOIN 
		friends 
	ON 
		users.code = friends.f_userid
	WHERE 
		friends.friendwith ='$username'
                  AND   friends.f_userid NOT IN 
                (SELECT friendwith FROM friends WHERE f_userid='$username')";
$myfriendsresult	= mysql_query($myfriends);
while($myfriendsrow = mysql_fetch_array($myfriendsresult))
{
echo $myfriendsrow['f_userid']."<br/>";
echo $myfriendsrow['friendwith']."<br/>";
}
?> 

not teted

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.