Jaynesh Posted July 9, 2011 Share Posted July 9, 2011 Hello. How can I add a column without effects the query? For example, when I add another column in SELECT, it produces each output as double. I want to add another column because I want to create an if statement with it in the while output. SELECT Users.id, Posts.post, Users.username, Posts.post_id, Posts.karma_up, Posts.karma_down FROM Posts, Users, Friends Quote Link to comment https://forums.phpfreaks.com/topic/241512-select/ Share on other sites More sharing options...
QuickOldCar Posted July 9, 2011 Share Posted July 9, 2011 I figure can use DISTINCT in your query http://dev.mysql.com/doc/refman/5.6/en/distinct-optimization.html If that doesn't do it for you use array_unique() on the results. http://php.net/manual/en/function.array-unique.php Quote Link to comment https://forums.phpfreaks.com/topic/241512-select/#findComment-1240614 Share on other sites More sharing options...
Jaynesh Posted July 9, 2011 Author Share Posted July 9, 2011 Hello It is still not working. I don't have duplicates in the database. For some reason it is outputting more then once. Quote Link to comment https://forums.phpfreaks.com/topic/241512-select/#findComment-1240622 Share on other sites More sharing options...
QuickOldCar Posted July 9, 2011 Share Posted July 9, 2011 "Got Code?" Quote Link to comment https://forums.phpfreaks.com/topic/241512-select/#findComment-1240624 Share on other sites More sharing options...
Jaynesh Posted July 9, 2011 Author Share Posted July 9, 2011 $friendfeed2 = "SELECT dbUsers.id, dbPosts.post, dbUsers.username, dbPosts.post_id, dbPosts.karma_up, dbPosts.karma_down FROM dbPosts, dbUsers, dbFriends, dbKarma WHERE (dbPosts.post_id NOT IN (SELECT post_id FROM dbDownvote WHERE user_id = $user) AND dbPosts.username_id = dbUsers.id AND dbFriends.user_id = $user AND dbFriends.friend_id = dbPosts.username_id AND dbFriends.status = 1) OR (dbPosts.post_id NOT IN (SELECT post_id FROM dbDownvote WHERE user_id = $user) AND dbPosts.username_id = dbUsers.id AND dbFriends.friend_id = $user AND dbFriends.user_id = dbPosts.username_id AND dbFriends.status = 1) UNION SELECT dbUsers.id, dbPosts.post, dbUsers.username, dbPosts.post_id, dbPosts.karma_up, dbPosts.karma_down FROM dbPosts, dbUsers, dbPosts_share, dbFriends WHERE (dbPosts.post_id NOT IN (SELECT post_id FROM dbDownvote WHERE user_id = $user) AND dbPosts.username_id = dbUsers.id AND dbPosts_share.user_id = dbFriends.friend_id AND dbFriends.user_id = $user AND dbPosts_share.post_id = dbPosts.post_id AND dbFriends.status = 1) OR (dbPosts.post_id NOT IN (SELECT post_id FROM dbDownvote WHERE user_id = $user) AND dbPosts.username_id = dbUsers.id AND dbPosts_share.user_id = dbFriends.user_id AND dbFriends.friend_id = $user AND dbPosts_share.post_id = dbPosts.post_id AND dbFriends.status = 1) UNION SELECT dbUsers.id, dbPosts.post, dbUsers.username, dbPosts.post_id, dbPosts.karma_up, dbPosts.karma_down FROM dbPosts, dbUsers WHERE dbPosts.username_id = dbUsers.id AND username_id = $user"; while( $i < $row = mysql_fetch_array($friendfeed_query2)) { if ($row['id'] == $user) { echo $div_mypostbox; } else { echo $div_postbox; } $karma_up = $row['karma_up']; $karma_down = $row ['karma_down']; $karma_total = $karma_up - $karma_down; echo "<a href=vote.php?up=" . $row['post_id'] . "><</a>"; echo $karma_total; echo "<a href=vote.php?down=" . $row['post_id'] . ">></a>"; echo $break; $post = $row['post']; $post_wrap = wordwrap($post, 50, "\n", true); echo "$post_wrap"; echo $div_end; echo $div_postfooter; echo "Submitted by " . $row['username'] . "<br>"; if ($row['id'] == $user) { echo "<a href=delete.php?id=" . $row['post_id'] . ">delete</a><br>"; } echo $div_end, $div_end, $div_end, $break; } I want to add a column in, to add an if command within the loop. I want to add something similar to what I've done with " if ($row['id'] == $user)" in the 2nd quote. Quote Link to comment https://forums.phpfreaks.com/topic/241512-select/#findComment-1240628 Share on other sites More sharing options...
Jaynesh Posted July 9, 2011 Author Share Posted July 9, 2011 In simpler terms it always gives me multiple results of each row when I have a table that is not in SELECT. for e.g "SELECT dbPosts.post, dbPosts.username_id FROM dbPosts, dbUsers WHERE dbPosts.username_id = 10"; The dbUsers table isnt in the SELECT section, so it gives me multiple results of each row. How do I stop this from happening. Quote Link to comment https://forums.phpfreaks.com/topic/241512-select/#findComment-1240642 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.