MYSQL find matching ids from another column - php

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

Related

Count in Joins in SQL

i have two tables (post.id is primary key which become seconday key in like.post_id )
table "post"
id user_id image
1 10 abc.jpg
2 20 xyz.jpg
3 10 ajb.jpg
Table "Like"
id user_id post_id likes
1 10 1 1
2 20 2 1
3 10 1 1
4 10 1 1
3 10 3 1
now i want whenever i pass user_id then i wan to get all post of users with number of likes of posts
i tried with following code but not worked,
SELECT selfie_info.id,selfie_info.user_id,selfie_info.image, (SELECT COUNT(m.likes)FROM post_likes m WHERE m.user_id='10') as total_likes FROM selfie_info where user_id='10'
how can i do this ? i want result like following (if i pass user_id=10 )
user_id post_id likes
10 1 3
10 3 1
SELECT p.user_id, p.id AS post_id, COUNT(l.id) AS total_likes
FROM post p
LEFT JOIN likes l ON l.post_id =p.id
WHERE p.user_id=10 GROUP BY p.id;
You can use aggregation:
select pl.post_id, count(*) as numlikes,
si.*
from selfie_info si join
post_likes pl
on si.user_id = pl.user_id
where si.user_id = 10
group by si.user_id, si.post_id
You can use si.* in the select, even though this is a group by query because you are aggregating by the primary key/unique id in the table.
You have all necessary data in table "like"
You can use one select from "like" table.
select user_id, post_id, sum(likes) as likes from like where user_id = 10;
You need to count or sum likes?
Depends on you needs You can sum(likes) or count(*) likes

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);

SQL: Get null as a return value for rows that don't match query

I have a table, Entity and a table Friends. I want to get the names of people who have visited the same location, but I only want to return them if they are not friends with the person (suggested friend query). In order to do this I have written the following query:
SELECT Entity_Id,
Category AS FC
FROM entity
LEFT
JOIN friends
ON entity.Entity_Id = friends.Entity_Id1
OR entity.Entity_Id = friends.Entity_Id2
WHERE Entity_Id IN ( :list_of_ids )
AND Entity_Id != :user_id
AND Category != 4
GROUP
BY Entity_Id
:list_of_ids is a comma separated list of user id's and in my test query there are 82 users, however only 15 users are returned from the query, where the users returned are users who have a relationship with :user_id.
Any user who does not have a relationship in the table is not returned in the query. I thought by providing a LEFT OUTER JOIN it would return NULL for fields that were not found in the friends table.
----- EXPECTED -----
--------------------
Entity_Id FC
--------------------
1 3
2 2
3 2
4 null
52 null
64 null
------ ACTUAL -------
---------------------
Entity_Id FC
---------------------
1 3
2 2
3 2
The structure of my friends table is as follows, also note that it supports reciprocal relationships if that is any help...
------ FRIENDS ------
---------------------
fid PK
entity_id1 INT
entity_id2 INT
category INT -- 1 = Facebook, 2 = G+, 3 = App, 4 = Blocked
How can I return the users who have a missing relationship?
You want a left outer join. But, with a left join, the conditions on the second table need to go into the on clause:
SELECT e.Entity_Id, f.Category AS FC
FROM entity e LEFT JOIN
friends f
ON e.Entity_Id IN (f.Entity_Id1, f.Entity_Id2) AND
f.category <> 4
WHERE e.Entity_Id IN ( :list_of_ids ) AND
e.Entity_Id <> :user_id
GROUP BY e.Entity_Id;
When you have the condition on category in the WHERE clause, you turn the LEFT JOIN into an INNER JOIN. When there is no match, category has a value of NULL, which fails the comparison.
Which table does the Category belong to? If its a friends column, move Category != 4 from WHERE to ON to get true outer join. (Otherwise it executes as a regular innner join...):
SELECT Entity_Id, Category AS FC
FROM entity
LEFT JOIN friends
ON (entity.Entity_Id = friends.Entity_Id1
OR entity.Entity_Id = friends.Entity_Id2)
AND Category != 4
WHERE entity.Entity_Id IN ( :list_of_ids )
AND entity.Entity_Id != :user_id
GROUP
BY Entity_Id

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

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]

Mysql left-join, count by unique value in a field not working

I have a table like below.
I want to be able to get a count of group_id then group by group_id then left join groups table where groups_user.group_id = groups.id but I'm only getting one result back.
I want group by unique group_id then count of each duplicate group_id. So far my query is like below:
SELECT 'groups.group' ,COUNT('groups_users.group_id') as groups_count
FROM `groups_users`
LEFT JOIN groups
ON 'groups_users.group_id' = 'groups.id'
GROUP BY 'groups_users.group_id'
Table:
id group_id user_id
26 3 1
22 2 1
19 1 1
20 1 2
21 1 4
Where am I getting wrong here?
I think you've got the JOIN backwards. Try:
SELECT g.group, COUNT(gu.group_id) AS groups_count
FROM groups g
LEFT JOIN groups_users gu
ON g.id = gu.group_id
GROUP BY g.group;

Categories