I have two tables, one of them is for friends of a person and another one is for channel member.
I want to check if one of my friends is one of the channel members. If so don't show his name, else show him.
$get_friend = "select * from friends where user_1_id='$user_id' AND friends_status=1";
$run_friend = mysqli_query($conn,$get_friend);
$select_members = "SELECT user_id from channel_members where channel_id='$channel_id'";
$run_members = mysqli_query($conn,$select_members);
Try this query :
select * from friends where user_1_id='$user_id' AND friends_status=1 AND user_2_id NOT IN(SELECT user_id from channel_members where channel_id='$channel_id')
This will select all your friends from friends table which are not a member of channel '$channel_id'
SELECT
friends.*
FROM
friends
LEFT JOIN channel_members
ON channel_members.channel_id = '$channel_id'
AND channel_members.user_id = friends.user_2_id
WHERE
user_1_id = '$user_id'
AND friends_status = 1
AND channel_members.channel_id IS NULL
You can use this query.
I added an excluding LEFT JOIN.
It will try to join the members of the channel who are your friends to the result, but by specifying channel_members.channel_id IS NULL in the where clause you are filtering those datasets out which really have a member in the channel - leaving you with only the friends who are not in the channel.
Related
I have two tables, one for registered users and one to store votes.
We are logging in with registrants.id and registrants.zipcode. Once they vote their votes are inserted into the votes table, along with their Registration ID.
Im trying to right a select statement that returns a record that will select all the records for Matched ID and Zipcode, but the ID is not in the Votes.voter column. i have tried all kinds of variations of all the joins i can think of. is it something simple i am missing.
SELECT * FROM registrants
LEFT JOIN votes on registrants.id = votes.voter
WHERE registrants.id = 1 AND registrants.zipcode = 46706 and votes.voter <> 1
Perhaps a not exists query:
select * from registrants
where registrants.zipcode = '46706'
and not exists (select 1 from votes where registrants.id = votes.voter)
I am trying to only show unique userIds (userIds are (0,1,2,3,4,5,6,7,8,9 etc...) for the query I am running. I tried using DISTINCT in my query, but it only shows me unique values of the rows that have 2 or more of the same userId.
Is there a way I can use php to only show the unique values. My weak points are arrays and it makes it more complicated because its using data from a MySQLi query.
Example right now I have with the query now (lets say its GROUP BY rentPaid DESC and the rent total is 800.00 for all users):
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
3--------400.00------April
1--------200.00------April
1--------100.00------April
Example desired output:
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
Can I do this with MYSQL because I tried DISTINCT and it wouldn't work, how about PHP?
Query:
SELECT
properties.*,
leases.*,
users.userId, users.primaryPhone,
CONCAT(users.userFirstName,' ',users.userLastName) AS user,
admins.adminName, payments.*
FROM
properties
LEFT JOIN leases ON properties.propertyId = leases.propertyId
LEFT JOIN assigned ON properties.propertyId = assigned.propertyId
LEFT JOIN admins ON assigned.adminId = admins.adminId
LEFT JOIN users ON properties.propertyId = users.propertyId
LEFT JOIN payments ON properties.propertyId = payments.propertyId
WHERE
payments.rentMonth = '$currentMonth' AND
payments.rentYear = '$currentYear'
Edit: Please excuse my formatting, this is my first post.
Edit: Added query....its long, but works lol. I only want unique userIds (no double or triple userIds etc...)
I suspect this is what you want:
SELECT userID, MAX(rentPaid) AS maxRentPaid, rentMonth
FROM yourTable
WHERE rentMonth = "April"
GROUP BY userID
ORDER BY maxRentPaid
I have 2 tables.
Table A: trades: which contains the columns: tradeID, tradeName, tradeShow, and tradeGuy.
Table B: offers: which contains the columns: tradeID, offerName, offerGuy.
I'm trying to select all columns from table A (trades) WHERE the value of "tradeShow" = 'Yes', And the value of "tradeGuy" != the user's Username. That much is easy, but I also don't want to select any records which have an offer created by the user. In other words, in table B (offers), offerGuy != Username WHERE trade ID from Table B = tradeID from Table A.
But, how do I merge these 2 conditions? I've tried this:
$sql = "SELECT *
FROM trades t1
JOIN offers t2
ON (t1.tradeID = t2.tradeID)
WHERE t1.tradeShow='Yes' AND t1.tradeGuy!='$username' AND t2.offeringGuy!='$username'";
But the problem with that is it only selects the records from trades which have an offer, because of the forth line: ON (t1.tradeID = t2.tradeID), as in it only selects trades which have a record in (offers) that mentions their tradeID.
I've also tried an awkward attempt to link the 2 tables with a meaningless link by adding a "linker" column to each table with the default value of "XXX", and did this:
$sql = "SELECT *
FROM trades t1
JOIN offers t2
ON (t1.linkerA = t2.linkerB)
WHERE t1.tradeShow='Yes' AND t1.tradeGuy!='$username' AND (t2.offeringGuy!='$username' WHERE t1.tradeID=t2.tradeID)";
But the problem with that is using 2 Where clauses...
So, how do I merge the 2 conditions?
What you're looking for is called an OUTER JOIN (in this case a LEFT OUTER JOIN) which will give you null results for missing matches, something like;
SELECT *
FROM trades t1
LEFT OUTER JOIN offers t2
ON t1.tradeID = t2.tradeID AND t2.offeringGuy = '$username'
WHERE t1.tradeShow='Yes' AND t1.tradeGuy!='$username' AND t2.offeringGuy IS NULL
We add a condition to the LEFT JOIN that we're only interested in matches against t2.offeringGuy = '$username', which will return NULL values in t2's fields if there is no match.
Then we just check that t2.offeringGuy IS NULL to find the non matches.
I would do this with not exists rather than an explicit join:
SELECT *
FROM trades t
WHERE t.tradeShow = 'Yes' AND t.tradeGuy <> '$username' and
not exists (select 1
from offers o
where t.tradeID = o.tradeID and o.tradeGuy = '$username'
);
I'm having a bit of trouble figuring out an SQL statement that I need to make.
I have 2 tables
Friends
===================================
friendfromid | friendtoid | request
===================================
Users
=================
userid | username
=================
I need to get the userid and username of each user that is my friend.
My id is either in the friendfromid or friendtoid depending on if I am requested the user or the user requested me.
So basically what needs to happen is the script needs to look at the friends table, get all the rows where my id is either friendfromid or friendtoid check that the request field is set to 1, take all the rows that fit that match then get the ids and usernames of each friendfromid or friendtoid which isn't mine.
For instance, if my id was 8 and the friends id was 9, let's say they requested me their id would be in the friendfromid field and my id would be in the friendtoid field, that means the script would take their id (9) and match it to that user in the users table.
This will give friends list with friend id and friend name:
SELECT u.userid , u.username
FROM Friends f
JOIN Users u
ON (( f.friendfromid ={the ID} AND friendtoid=u.userid)
OR ( f.friendtoid = {the ID} AND friendfromid=u.userid))
WHERE f.request = 1
Put {the ID}= user id whose friend list required.
Should be something like this:
SELECT *
FROM users
JOIN friends
ON friendfromid != userid
OR friendtoid != userid
WHERE request = 1
AND userid = {the ID}
Can't achieve in 1 query.
To get friendtoid with "my user ID" in friendfromid field:
SELECT b.friendtoid FROM Users a, Friends b WHERE request=1 AND friendfromid = a.userid
Vice versa for friendfromid.
Use a UNION query, joining to friends as below
SELECT username FROM user INNER JOIN friends ON (userid = friendfrom) WHERE friendto = {myid) AND request = 1
UNION
SELECT username FROM user INNER JOIN friends ON (userid = friendto) WHERE friendfrom = {myid} AND request = 1
Remember you need indexes on the friendto and friendfrom columns for performance
I have friends table where there are stored two friends' IDs. When I query, let's say my friends, is there an easy way to get my friends' names from users table? I use PHP and MySQL and the tables are not connected by foreign key but I think I can connect them (using Navicat).
My query:
session_start();
if(isset($_SESSION['valid_user']))
{
$currentuser= $_SESSION['valid_user'];
}
$friendships= mysql_query("SELECT * FROM friends WHERE Person1 = '$currentuser' OR Person2 = '$currentuser' ");
while($row = mysql_fetch_assoc($friendsips))
{
if ($row['Person1'] == $currentuser) echo $row['Person2']; // quering the users name from here is hard and long thing to do
if ($row['Person2'] == $currentuser) echo $row['Person1'];
}
friends(friendshipID, Person1, Person2)
users(ID, name, surname)
Assuming the table structure is :
Users:
userid , name
Friends:
friendship_id , userid_1 , userid_2
SELECT name
FROM Friends F JOIN Users U ON (F.userid_2 = U.userid)
WHERE userid_1 = X
Edit:
If you want to get both names you can use this.
SELECT
U1.name as name1 , U2.name as name2
FROM
Friends F
JOIN Users U1 ON (F.userid_1 = U1.userid)
JOIN Users U2 ON (F.userid_2 = U2.userid)
WHERE
userid_1 = X
You can adjust the where clause to query on either user ids.