Here is my table structure:
// users
+----+--------+
| id | name |
+----+--------+
| 1 | Jack |
| 2 | Peter |
| 3 | John |
| 4 | Barman |
| 5 | Ali |
+----+--------+
// friends
+---------+-----------+
| user_id | friend_id |
+---------+-----------+
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 3 | 1 |
| 3 | 2 |
| 3 | 4 |
| 5 | 2 |
+---------+-----------+
-- both user_id and friend_id columns refer to the id column of users table
And here is my query:
// $id = 1;
select distinct f1.user_id, f1.friend_id
from friend f1
where user_id = $id
or
user_id in (select f2.friend_id
from friend f2
where user_id = $id);
/* output
| 1 | 3 |
| 1 | 4 |
| 1 | 5 |
| 3 | 2 |
| 3 | 4 |
| 5 | 2 |
As you see, my query selects
Jack (because of $id = 1)
All Jack's friends
All friends of Jack's friends
Ok all fine. In reality, I'm trying to make a graph of the result. actually I did it. Now I want to restrict the results to only common friends. I mean I want to remove single nodes. In other word, I want to select friends who have at least two edges.
Is doing that possible by changing the query or should I do that in the PHP layer?
A visual example and its expected output:
The bellow query will return all the common friends that are not direct friends:
select distinct f.user_id, f2.friend_id common_friend
from friends f
inner join friends f2
on f.friend_id = f2.user_id
left join friends f3
on f.user_id = f3.user_id
and f2.friend_id = f3.friend_id
where f3.user_id is null
and f.user_id <> f2.friend_id
#and f.user_id = 1 Jack
The first 'inner' join returns the circle of friends and the second 'left' join joins the first-order friends from the circle -> the where f3.user_id is null removes the first-order friends.
The last f.user_id <> f2.friend_id is to remove a user being a common friend to himself.
Try this
SELECT DISTINCT f1.user_id, f1.friend_id
FROM friend f1
WHERE (
user_id = $id
OR user_id IN (
SELECT f2.friend_id
FROM friend f2
WHERE user_id = $id))
AND (friend_id IN (
SELECT f3.friend_id
FROM friend f3
WHERE user_id = $id))
Related
I have these tables...
GROUP_MEMBERS
+---------------------------------+
| id | group_id | member_id |
+---------------------------------+
| 1 | 1 | 1 |
| 2 | 1 | 4 |
+---------------------------------+
MEMBERS
+-------------------------------------+
| id | first | last | role_id |
+-------------------------------------+
| 1 | Jack | Jones | 1 |
| 2 | Jane | Doe | 2 |
| 3 | Bob | Bee | 2 |
| 4 | Jen | Nee | 1 |
+-------------------------------------+
GROUPS
+-----------------+
| id | name |
+-----------------+
| 1 | group1 |
| 2 | group2 |
+-----------------+
As it is, I am using the following query...
SELECT
(members.id) AS memid,
members.first,
members.last,
members.role_id
FROM
members
LEFT JOIN group_members ON
members.id = group_members.member_id
WHERE
group_members.member_id IS NULL
GROUP BY
members.id;
This outputs the members (Jane and Bob) who are not in the 'GROUP_MEMBERS' table as it should, but what I am trying get working is if I am on and another group ($_GET['group_id']), how can I show all members that do not have rows that match group_id and member_id on the 'GROUP_MEMBERS' table...
i.e if group_id = '2' show all members
I have tried adding in WHERE clause... AND group_members.group_id IS NULL.. but it shows nothing then.
Does anyone have a query which would get the output I'm looking for?
Thanks
[EDITED]
Just to clarify...
If my url had 'group_id=1'
I should see:
Bob
Jane
If my url has 'group_id=2'
I should see:
Jack
Jane
Bob
Jen
So it only shows 'members' that do not exist( with the 'group_id' in the url) in the 'GROUP_MEMBERS' table
If I have understood the question correctly, you are looking for something like I have made on this fiddle:
DB Fiddle
The query I use is:
$sql = 'SELECT * FROM groups
RIGHT JOIN group_members ON groups.id = group_id
RIGHT JOIN members ON member_id = members.id
WHERE group_id <> ? OR group_id is NULL;'
$group_id = $_GET['group_id'];
$query = $mysqli->prepare($sql);
$query->bind_param('i', $group_id);
In short, this query will select from the groups table, ensuring that we will select every group in your database.
Then we will join the other two tables completely (using the RIGHT JOIN).
Finally, we are going to select every member that isn't the specified the one provided by the URL, or any member that is not in a group.
You can use a sub-query
how can I show all members that do not have rows that match 'group_id'
$group_id= $_GET['group_id'];
$q = "SELECT * FROM MEMBERS WHERE MEMBERS.id NOT IN(
SELECT member_id FROM GROUP_MEMBERS WHERE group_id='$group_id'
);";
Explanation
SELECT member_id FROM GROUP_MEMBERS WHERE group_id='$grID'
this will get all the members in this group by a given ID
then you select all members that are not among them.
SELECT * FROM MEMBERS WHERE MEMBERS.id NOT IN()
this one will give members data except the ids inside the brackets
the sub query will get the ids of members in a given group
no need for joining the three tables since you are using id of the group existing in GROUP_MEMBERS and linking the GROUP and MEMBERS
one side note
if you have a group name and what all users not in this group you then will need to use the GROUPS table
SELECT * FROM MEMBERS WHERE MEMBERS.id NOT IN(
SELECT member_id FROM GROUP_MEMBERS WHERE group_id = (
SELECT id from GROUPS WHERE name = '$Group_Name'
)
);
you may use WHERE group_id IN (...) it will work the same
This is a demonstration, I created same database with same data and tested the queries
+----+-------+-------+---------+
| id | first | last | role_id |
+----+-------+-------+---------+
| 1 | Jack | Jones | 1 |
| 2 | Jane | Doe | 2 |
| 3 | Bob | Bee | 2 |
| 4 | Jen | Nee | 2 |
+----+-------+-------+---------+
+----+--------+
| id | name |
+----+--------+
| 1 | group1 |
| 2 | group2 |
+----+--------+
+----+----------+-----------+
| id | group_id | member_id |
+----+----------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 4 |
+----+----------+-----------+
I run the sub-query as above and the results as expected,
MariaDB []> select * from members where id not in
(select member_id from group_members where group_id = 1);
+----+-------+------+---------+
| id | first | last | role_id |
+----+-------+------+---------+
| 2 | Jane | Doe | 2 |
| 3 | Bob | Bee | 2 |
+----+-------+------+---------+
similar for when you have group name
MariaDB []> select * from members where id not in
(select member_id from group_members where group_id =
(select id from groups where name='group1'));
+----+-------+------+---------+
| id | first | last | role_id |
+----+-------+------+---------+
| 2 | Jane | Doe | 2 |
| 3 | Bob | Bee | 2 |
+----+-------+------+---------+
My database looks something like this, in order of user, userteam (connection table) and team:
Usertable UserTeam Teamtable
+--------+------+ +--------+--------+ +--------+-------+
| userid | name | | userid | teamid | | teamid | name |
+--------+------+ +--------+--------+ +--------+-------+
| 1 | eric | | 1 | 1 | | 1 | awsm |
| 2 | john | | 1 | 2 | | 2 | doe |
| 3 | carl | | 2 | 1 | | 3 | empty |
+--------+------+ | 3 | 1 | +--------+-------+
+--------+--------+
How do I select all users that IS NOT in a team, and echo them out only once? I've tried doing it with the usertable, but then it will echo for instance, if I try and select all members not in team 3:
SELECT userid FROM userteam WHERE teamid!=3;
SELECT * FROM user WHERE userid='$previousSql';
1 eric
1 eric
2 john
3 carl
What I'd like instead is:
1 eric
2 john
3 carl
I'm making an add members function, and I'd very much not like everyone to show up more than once.. Any directions, help or guiding would be much appreciated.
add
GROUP BY name
to the query
This should do it:
SELECT u.userid, u.name FROM Usertable u
INNER JOIN UserTeam ut ON ut.userid = u.userid
INNER JOIN Teamtable tt ON tt.teamid = ut.teamid
WHERE tt.teamid != 3
GROUP BY u.userid, u.name;
SELECT userid, name FROM usertable
JOIN userteam ON userteam.userid = usertable.userid
JOIN teamtable on teamtable.teamid = userteam.teamid
WHERE teamtable.teamid = 3;
The SQL above should do the proper joins between all the tables and then will only list the userid/names of people who have a teamid of 3 in the teamtable
EDIT* Removed ! from WHERE clause.
I have 2 tables in database:
How can I get total user for each group. i.e: group 1: total are 2 users;
group2: total are 2 users;
group3: total is 1 user
You need normalization and never store comma-separated data.
Consider the following
mysql> select * from user_table ;
+---------+---------------+
| user_id | user_group_id |
+---------+---------------+
| 1 | 1,2 |
| 2 | 2 |
| 3 | 1,3 |
+---------+---------------+
3 rows in set (0.00 sec)
mysql> select * from group_table ;
+----------+------------+
| group_id | group_name |
+----------+------------+
| 1 | a |
| 2 | b |
| 3 | c |
+----------+------------+
3 rows in set (0.00 sec)
The above data is not normalized and to get the desired result out of these you need to use some in-efficient query as
select
g.group_id,
count(*) as total
from group_table g
left join user_table u on find_in_set(g.group_id,u.user_group_id) > 0
group by g.group_id ;
+----------+-------+
| group_id | total |
+----------+-------+
| 1 | 2 |
| 2 | 2 |
| 3 | 1 |
+----------+-------+
Now lets do normalization and store user-group data in a different table as
mysql> select * from user_to_group ;
+---------+----------+
| user_id | group_id |
+---------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 3 | 1 |
| 3 | 3 |
+---------+----------+
You can easily write different queries from these tables now and here are some examples
select group_id,count(*) as tot from user_to_group group by group_id ;
+----------+-----+
| group_id | tot |
+----------+-----+
| 1 | 2 |
| 2 | 2 |
| 3 | 1 |
+----------+-----+
Joining the tables would even more easy
select
g.group_id,
g.group_name,
count(*) as tot
from user_to_group ug
join group_table g on g.group_id = ug.group_id
join user_table u on u.user_id = ug.user_id
group by g.group_id
+----------+------------+-----+
| group_id | group_name | tot |
+----------+------------+-----+
| 1 | a | 2 |
| 2 | b | 2 |
| 3 | c | 1 |
+----------+------------+-----+
SELECT group_name, COUNT(*) FROM user_table u, group_table g WHERE u.user_group_id LIKE %g.group_id% GROUP BY g.group_name;
this should work and give you a list of all groups and how many users are in them.
I will recommend you to create a third table which holds the information about which users are in which groups.
CREATE TABLE users_in_groups
(
user_id INT
, group_id INT
);
Then you can join like this:
SELECT
gt.group_id
, count(ut.user_id)
FROM
user_table AS ut
, INNER JOIN users_in_groups AS uig ON uig.user_id = ut.user_id
, INNER JOIN group_table AS gt ON gt.group_id = uig.group_id
GROUP BY
gt.group_id
;
To use the table you have now will you have to do something like this (in mysql):
SELECT
gt.group_id
, count(ut.user_id)
FROM
user_table AS ut
, INNER JOIN group_table AS gt ON LOCATE(gt.group_id, ut.user_group_id) > 0
GROUP BY
gt.group_id
Remember, when using group by, always locate what makes your group unique!
This is not an answer to your specific question but rather an alternative data structure proposal that might be better.
Introduce a new table members that looks like
# members
user_id | group_id
1 | 1
1 | 2
2 | 2
3 | 1
3 | 3
Then you could SELECT group_id, count(*) FROM members GROUP BY group_id
+----------+----------+
| group_id | count(*) |
+----------+----------+
| 1 | 2 |
| 2 | 2 |
| 3 | 1 |
+----------+----------+
This structure might also make it easier for you to manage your memberships. user_id + group_id should be unique. And if supported let them be foreign keys.
Im making an application where its possible to vote on pictures.
Im doing some joins and would like to check each picture i get, if the user logged in has voted on it.
My votes table setup is like this:
+-------------------------+
| id | user_id | photo_id |
+-------------------------+
| 1 | 2 | 6 |
+-------------------------+
| 2 | 4 | 5 |
+-------------------------+
| 3 | 3 | 5 |
+-------------------------+
| 4 | 1 | 6 |
+-------------------------+
Im joining 3 tables:
users, photos and votes
SELECT
users.*,
photos.*,
users.id as user_userid,
photos.id as photo_photoid,
COUNT(votes.id) as totalvotes
FROM
photos
LEFT JOIN
votes
ON
votes.photo_id = photos.id
LEFT JOIN
users
ON
users.id = photos.author_id
GROUP BY
photos.id
ORDER BY
totalvotes
DESC
I would like to make a query inside this sql that does something like this:
+---------------------------------------------------------------------------------+
| photo_photoid | user_userid | totalVotes | currentUserHasVotetThisAmountOfTime |
+---------------------------------------------------------------------------------+
| 6 | 1 | 2 | 1 |
+---------------------------------------------------------------------------------+
| 5 | 1 | 2 | 0 |
+---------------------------------------------------------------------------------+
So i guess im looking for a count of the records, where votes.user_id = $MyLoggedInUser AND votes.photo_id = photo.id
Any suggestions?
Here is your request :
SELECT
v.photo_id,
COUNT(v.id) AS total_votes,
(SELECT COUNT(id) FROM vote WHERE photo_id = v.photo_id AND user_id = 1) AS currentUserHasVotetThisAmountOfTime
FROM
vote AS v
GROUP BY
v.photo_id
ORDER BY
total_votes
DESC
Just replace user_id = 1 by your own ID (in the sub-request line 4).
I get ride of the user_id column, since this is your something you provide there is imo no point to return this in the query as well.
If you want to test it by yourself : http://sqlfiddle.com/#!2/ba2a1/16/0
I have a following system where users can follow each other and search for users to follow. Here are examples of the users and following tables:
Users Table:
+-------+-----------+
| id | userName |
+-------+-----------+
| 1 | John |
| 2 | Harriet |
| 3 | Chris |
| 4 | Lisa |
| 5 | Joe |
+-------+-----------+
Following Table:
+-------+-------+-------+
| id | id_1 | id_2 |
+-------+-------+-------+
| 1 | 5 | 3 |
| 2 | 1 | 2 |
| 3 | 1 | 5 |
| 4 | 5 | 4 |
+-------+-------+-------+
I want to know how to structure a mySQL statement to join the following table with the users table where the username is like '%$search%' to return all results of this occurrence (i.e. searching 'jo' returns John and Joe) and when there are multiple results for each person (i.e. Joe will occur twice in Following) return only results where id_1 is the current logged in users id to show that the logged in user is already following this person.
$search = $_POST['search'];
$userid = $_POST['userid'];
$qry = "
SELECT u.id
, u.userName
, f.id_1
, f.id_2
FROM users u
LEFT
JOIN following f
ON IF('$userid' = f.id_1, u.id = f.id_2, u.id = f.id_1)
WHERE u.userName LIKE '%$search%';
";
This query returns every occurrence of the username in the following table but it should only return once.
Any help would be great.
Thanks.
Not absolutely sure but give it a try
SELECT u.id
, u.userName
, f.id_1
, f.id_2
FROM users u
LEFT JOIN following f
ON u.id in (f.id_1,f.id_2)
AND f.id_1 = '$userid'
WHERE u.userName LIKE '%$search%'
ORDER BY u.id
LIMIT 1;