WHERE query inside an AND statement? PHP/PDO/MySQL - php

I have 2 tables on my website, a users table and a user_friendships table each structured as so...
Users
id | user_id | credits_bank | credits_offered
User Friendships
id | user_id | user_followed_id
When my user logs in he is presented with a list of other users on the website - the list of other users are those who are stored in the users table and have a greater value in the credits_bank table than that of the credits_offered table.
When a friendship is created, the session users id is stored in the user_friendships table, and the id of the other member he followed is also stored in the user_friendships table under the column user_followed_id.
The problem is I now need a query to return all users who have move credits_bank than credits_offered and users that aren't already in the user_frienships table in the same record as the session user.
I'm currently using...
SELECT DISTINCT u.*
FROM users u
LEFT JOIN user_friendships uf ON u.user_id = uf.user_followed_id
WHERE u.user_id <> ?
AND u.credits_offered <= credits_bank
AND uf.user_followed_id IS NULL
Update
I want to see a list of users whose credits_bank is a greater value than credits_offered and I only want to show them if they dont already exist in a record in my user_friendships table in the same row as my session user.
Users
id | user_id | credits_bank | credits_offered
___________________________________________________
1 123 10 2
2 231 6 3
3 312 6 5
4 213 2 1
User Friendships
id | user_id | user_followed_id
___________________________________________________
1 123 231
2 123 312
Result
If session user_id = 123 then...
user_id 231 and 312 WOULDN'T show as they are in the user friendships table alongside session user id
user_id 213 WOULD show as they have more credits_bank than credits_offered and arent in friendships table
IF the session user_id was 312 then he would see all results as he isnt friends with anybody in the user_friendships table...

As far as I can tell, you're close. If the user id of the current user is called SESS_USER_ID, something like this should work for you;
SELECT DISTINCT u.*
FROM users u
LEFT JOIN user_friendships uf
ON uf.user_followed_id = u.user_id
AND uf.user_id = SESS_USER_ID
WHERE u.credits_offered <= credits_bank
AND uf.user_followed_id IS NULL
AND u.user_id <> SESS_USER_ID
(note that SESS_USER_ID is used twice in the query to make it simple)
An SQLfiddle to test with.

Try this:
SELECT u.id, u.user_id, u.credits_bank, u.credits_offered
FROM users u
WHERE u.credits_bank>u.credits_offered
AND u.user_id = [ENTER LOGGED IN USERS ID HERE]
AND u.user_id NOT IN (
SELECT f.user_ol
FROM user_friendships f
)
Let me know if you have any issues
EDIT
Latest SQL:
SELECT u.id, u.user_id, u.credits_bank, u.credits_offered
FROM users u
INNER JOIN user_friendships f
ON f.user_followed_id = u.user_id
AND u.credits_bank > u.credits_offered
AND f.user_id != [CURRENT_USER_ID]
AND u.user_id != [CURRENT_USER_ID]

Related

Join with condition with different column

I have following two tables :
user_profile
id | name | email | phone
1 | Rahul |r#gmail.com |1234567890
2 | Rohan |r1#gmail.com |1234567890
3 | Mohan |m#gmail.com |1234567890
user_request_table
id | from_user_id|to_user_id|status
1 | 1 | 2 | 2
2 | 3 | 1 | 2
Here status
0 = Request is cancel,
1 = request is pending,
2 = request accepted
I have following section to display :
Find a friend
Here I need to display suggetion for friend. I need to display all record from user_profile table but user should not be a friend(status should not be 2) or request should not be pending(status should not be 1).
So if I logged in as Rohan(id : 2) it should suggest me Mohan as friend suggestion.
Would this work:
SELECT
u.*
FROM
user_profile u
LEFT JOIN user_request_table r ON u.id=r.to_user_id AND r.from_user_id = <your_logged_in_user_id>
WHERE
r.status NOT IN (2,1) --filter pending and accepted here ;
So you want those records in user_profile table that do not have a record in user_request table where user is either from_user_id or to_user_id. The pattern is to do a left join to user_request and filter out those where user_request.id is null:
select
p.*
from
user_profile as p
left join user_request as r on
(
(p.id = r.from_user_id and r.to_user_id = {id})
or
(p.id = r.to_user_id and r.from_user_id = {id})
)
and r.status in (1,2)
where
r.id is null
and p.id <> {id}
;
with {id} a parameter for the user you want to suggest friends for.
First get all possible pairs from the user_profile table. Next filter through all the pairs and eliminate the unqualified using data from the user_request_table. It will get all the eligible (from_user , to_user)pairs.
select u1_id as fromuser,u2_id as touser
from
(select distinct u1.id as u1_id,u2.id as u2_id from user_profile u1 cross join user_profile u2 where u1.id!=u2.id) all_pair
where not exists
(select from_user_id ,to_user_id from user_request_table where status>0 and (
((u1_id=from_user_id) and(u2_id=to_user_id)) or ((u1_id=to_user_id) and(u2_id=from_user_id))
)
)
;
The result set looks like this:
fromuser, touser
3 2
2 3
This solution is for MSSQL SERVER, you can modify as per your preferred server
Pass your logged in id to declared variable
DECLARE #LoginId AS INT
SET #LoginId=2 /*here I passed logged in id as 2*/
SELECT * FROM user_profile up
WHERE 1=1 and id<>#LoginId
AND id NOT IN (SELECT from_user_id FROM user_request_table WHERE to_user_id=#LoginId and STATUS in(1,2))
AND id NOT IN (SELECT to_user_id FROM user_request_table WHERE from_user_id=#LoginId and STATUS in(1,2))

Or condition in INNER JOIN

I have a table structured as follows (points):
id1 | id2 | p1 | p2
1 3 5 7
3 1 2 4
1 2 1 7
And another table strucuted as follows (users):
id | name
1 User1
2 User2
3 User3
So now, I need a query that specifing an ID (for example 3), the query check that the ID appears in the column id1 and id2, and if it appears in one of the two columns, it gives me back the user name with id1 and id2 from the rows selected. So, for example if I specific the ID 3, the query give me back:
name1 | name2 | p1 | p2
User1 User3 5 7
User3 User1 2 4
I tried various solutions but no way to do it, I think that I need an OR condition in the INNER JOIN but I don't know if it's possible and if it's the solution.. I didn't find nothing here.
I mean something like:
INNER JOIN users ON (users.id = points.id1) || (users.id = points.id2)
Any solution for that? Thanks
Join the user table twice:
SELECT u1.name, u2.name, p.p1, p.p2
FROM points p
JOIN users u1 ON u1.id = p.id1
JOIN users u2 ON u2.id = p.id2
WHERE u1.id = 3 OR u2.id = 3
Use case statement it will give you all matching value not need restricted for one or two values
CREATE TABLE points (id1 int(2), id2 int(2), p1 int(2), p2 int(2));
INSERT INTO points VALUES(1,3,5,7);
INSERT INTO points VALUES(3,1,2,4);
INSERT INTO points VALUES(1,2,1,7);
CREATE TABLE users (id int(2), name char(23));
INSERT INTO users VALUES(1,'user1');
INSERT INTO users VALUES(2,'user2');
INSERT INTO users VALUES(3,'user3');
SELECT (CASE WHEN u.id = p.id1 THEN u.name END) AS name1,
(CASE WHEN u1.id = p.id2 THEN u1.name END) AS name2,
p1, p2
FROM points p
INNER JOIN users u ON (u.id = p.id1)
INNER JOIN users u1 ON (u1.id = p.id2);

MYSQL find matching ids from another column

How can I select matching data from another column given this example:
id user_id match_id
1 3 4
2 3 5
3 4 3
4 4 6
Note: ID column is an auto_increment column
So basically the output should be that user_id 3 and user_id 4 are matched since user_id 3 has user_id 4 in the match_id column while user_id 4 has user_id 3 in the match_id column
Assuming the table you listed is user_matching, and the table you're trying to join on is users and it has both an id and username column, you can use the following (Use u. and u2. to differentiate between fields on the matched user/member, or the original user/member):
SELECT u.username, u2.username AS match_username
FROM user_matching m
LEFT JOIN users u ON u.id = u.user_id
LEFT JOIN users u2 on u2.id = u.match_id

Select comma separated records from table along with user image

Table : frei_session
id username accountId status status_mesg
14 Sumit Bijvani 50 0 I am available
16 Dilip Borad 49 1 I am available
15 Karan Bijvani 51 1 I am available
Table : users
accountId friends userImage
49 50,52 49.jpg
50 49,52,51,44 50.jpg
51 50 51.jpg
I have 2 tables, frei_session have records of online users and users table have data of users and friends of them separated ID by comma.
I want to retrieve data from frei_session table based on friends column of users table.
For Ex
If user 49 is online, I want Output like below
id username accountId status status_mesg userImage
14 Sumit Bijvani 50 0 I am available 50.jpg
Because User 50 and 52 is friend of User 49 but now only User 50 is online
I have tried following query. but it show me wrong userImage.
SELECT b.*, a.userImage
FROM users a
INNER JOIN frei_session b
ON FIND_IN_SET(b.accountID, a.friends) > 0
INNER JOIN
(
SELECT accountID, username, MAX(id) id
FROM frei_session
GROUP BY accountID, username
) c ON b.accountID = c.accountID AND
b.username = c.username AND
b.id = c.id
WHERE b.status = 0 AND
a.accountID = 49
SQL Fiddle Demo
The result from the frei_session is the friend of user (a) that's why it does not contain the image of your friend but the image of the user you are trying to search. So in order for you to get your friends image, you need to join it to another user (d) so you can get the correct image from the frei_session table.
SELECT b.*, d.userImage
FROM users a
INNER JOIN frei_session b
ON FIND_IN_SET(b.accountID, a.friends) > 0
INNER JOIN
(
SELECT accountID, username, MAX(id) id
FROM frei_session
GROUP BY accountID, username
) c ON b.accountID = c.accountID AND
b.username = c.username AND
b.id = c.id
INNER JOIN users d
ON b.AccountID = d.AccountID
WHERE b.status = 0 AND
a.accountID = 49
SQLFiddle Demo
By normalizing your tables you can do all this with basic JOIN operarions. Please take a look at database normalization as your schema violates all rules of proper database design.
friends contains more than a single value and therefore violates
the first normal form.
status_mesg depends on status and
therefore violates the second normal form.

MySQL query with 2 tables join

I have a users like this:
id username
1 user1
2 user2
3 user3
and a msgs like this:
t_id sent_by id msg
1 2 1 whatever
2 3 1 is
3 2 1 here
Where users.id is a primary key and msgs.id is a foreign key. In the msgs table, id is the destination of the message sent by sent_by.
I want to select and display the username of sent_by as long as the logged in user (via sessions) is the msgs.id.
To clarify things, here is the pseudocode of what I wanted to do:
Users has logged in. Store its userid to session.
Display the distinct usernames of who sent me (the logged in user) the messages. In the example above, the display will be: user2, user3 if I have a user id of 1.
I was thinking of using join but ended up doing 2 queries for the sent_by and ids. It seems not an efficient query.
What should I do?
This is a straightforward JOIN since you only wish to return usernames of the sent_by ids.
$sql = "SELECT DISTINCT
users.username
msgs.sent_by
FROM users JOIN msgs ON users.id = msgs.sent_by
WHERE id = {$_SESSION['my_userid']}";
SELECT DISTINCT u.username, m.sent_by
FROM msgs m
INNER JOIN users u ON u.id = m.sent_by
WHERE m.id = {$_SESSION['userid']}
ORDER BY m.t_id DESC

Categories