Querying with Many To Many in SQL - mysql

so I have two tables: "Group" and "Users" which have a many to many relationship. A group can have many users, and a user can have many groups. I need to be able to query all groups the user is in, and I also need to be able to have all of the other users that are in the group. Right now I am only able to query all the user's groups but in the group.users property it gives me back only one user.

You could try doing a left outer join from group to users and then doing another from users to group and union all to get all permutations or union to get distinct permutations.
Pseudo Code.
Select USERID, GroupID
FROM User
LEFT OUTER JOIN Group
ON Group.UserID = User.UserID
UNION --- Or UNION ALL If you wanted all relationships without removal of duplicates.
SELECT UserID, GroupID
FROM Group
LEFT OUTER JOIN User
ON User.GroupID = Group.GroupID

Related

Pull multiple rows from one table but only one row from a related table

I currently am trying to join two tables but prevent duplication of information from one of the tables.
The user's table has 4 columns, uid, name, email and status.
The stats table has 4 columns, uid, date, follows, views
What I would like to be able to do is pull every record from the stats table and only the name, email and status values from the user table. The issue I have with the below SQL is that it duplicates the data from the user table, is there a way around this?
SELECT u.name
, u.email
, u.status
, s.date
, s.follows
, s.views
FROM users u
JOIN stats s
ON u.id = s.uid
WHERE name = :name
If you don't want every matching stats row to be accompanied by its matching users row, then you have to run two queries:
SELECT u.id, u.name, u.email, u.status FROM users u WHERE name = :name
Note the result of u.id because you'll use it as a parameter for the second query:
SELECT s.date, s.follows, s.views FROM stats s WHERE s.uid = :uid
You have to understand that the relational model works because every query result is itself a relation. The matching data is returned in every row, and this is what allows JOIN to be part of an algebra, where the result can be used as the operand of another JOIN, or a GROUP BY, or some other relational operation.
You should read SQL and Relational Theory: How to Write Accurate SQL Code by C. J. Date.

mysql inner join interrogate 3 tables

I have a table in a MySQL DB, called ‘users’. The fields for users are : id, email, username, first_name, last_name. Another table in the same MySQL DB, called ‘premium_users’ , contains 2 fields : user_id, premium_service_id. A third table called ‘premium_services’ contains 2 fields : premium_service_id , premium_service_name.
I want to create an SQL query , to interrogate my db, so i can have a full list of what premium services has every premium user. How can i interrogate properly with inner join? I’ve try this :
select * from users inner join premium_users on users.id = premium_users.user_id inner join premium_services on premium_users.premium_service_id = premium_services.premium_service_id;
Since you say which service has every user, you'll need to use aggregation to determine this. Here's one way:
select user_id
from premium_users
group by user_id
having count(*) = (select count(*) from premium_services)
SQL Fiddle Demo
Depending on your data, you may need count(distinct premium_service_id) instead, but you should have constraints that don't allow duplicates in those table.
Rereading your question, I might have got this backwards. Looks like you want a list of premium services instead of users. Same concept applies:
select ps.premium_service_id
from premium_services ps
join premium_users pu on ps.premium_service_id = pu.premium_service_id
group by ps.premium_service_id
having count(distinct pu.user_id) = (select count(distinct user_Id) from premium_users)
More Fiddle

MySQL query from 2 tables confusion

I have a query that I would like to run but is not returning the expected results.
So my tables are like this
users (has two columns)
user_id,name
users_archive (has the same two columns)
user_id,name
I want to basically run a query that lists user_id from the respective table where the username matches what I'm searching for
For my example I have a user called MikeBOSS in users_archive with an user_id of 123 (there is no MikeBOSS in users table)
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users_archive.name='MikeBOSS'
WHERE users.name='MikeBOSS';
but that returns no results
SELECT users.user_id, users_archive.user_id
FROM users, users_archive
WHERE (users.name='MikeBOSS' OR users_archive.name='MikeBOSS');
That returns a bunch of results from the users table that are incorrect.
Could someone maybe point me in the correct direction?
You do not want a JOIN, you want a UNION. Look
SELECT users.user_id, 'users'
FROM users
WHERE users.name='MikeBOSS'
UNION
SELECT users_archive.user_id, 'archive'
FROM users_archive
WHERE users_archive.name='MikeBOSS';
A join condition normally links two tables. Yours does not:
ON users_archive.name='MikeBOSS'
A join condition that does link the two tables might look something like:
ON users.name = users_archive.name
If you wonder about the number of rows this returns, check each table individually. Is there even a row with name = 'MikeBoss' in the users_archive table?
Change
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users_archive.name='MikeBOSS'
WHERE users.name='MikeBOSS';
To
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users.name = users_archive.name WHERE users.name='MikeBOSS';
May be this could work if you have no relation between 2 tables. Using left join gives you record from one table even if its not present in other.
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users.userid = users_archive.userid and users.name = users_archive.name
WHERE users.name='MikeBOSS';

MySQL - 3 tables, is this complex join even possible?

I have three tables: users, groups and relation.
Table users with fields: usrID, usrName, usrPass, usrPts
Table groups with fields: grpID, grpName, grpMinPts
Table relation with fields: uID, gID
User can be placed in group in two ways:
if collect group minimal number of points (users.usrPts > group.grpMinPts ORDER BY group.grpMinPts DSC LIMIT 1)
if his relation to the group is manually added in relation tables (user ID provided as uID, as well as group ID provided as gID in table named relation)
Can I create one single query, to determine for every user (or one specific), which group he belongs, but, manual relation (using relation table) should have higher priority than usrPts compared to grpMinPts? Also, I do not want to have one user shown twice (to show his real group by points, but related group also)...
Thanks in advance! :) I tried:
SELECT * FROM users LEFT JOIN (relation LEFT JOIN groups ON (relation.gID = groups.grpID) ON users.usrID = relation.uID
Using this I managed to extract specified relations (from relation table), but, I have no idea how to include user points, respecting above mentioned priority (specified first). I know how to do this in a few separated queries in php, that is simple, but I am curious, can it be done using one single query?
EDIT TO ADD:
Thanks to really educational technique using coalesce #GordonLinoff provided, I managed to make this query to work as I expected. So, here it goes:
SELECT o.usrID, o.usrName, o.usrPass, o.usrPts, t.grpID, t.grpName
FROM (
SELECT u.*, COALESCE(relationgroupid,groupid) AS thegroupid
FROM (
SELECT u.*, (
SELECT grpID
FROM groups g
WHERE u.usrPts > g.grpMinPts
ORDER BY g.grpMinPts DESC
LIMIT 1
) AS groupid, (
SELECT grpUID
FROM relation r
WHERE r.userUID = u.usrID
) AS relationgroupid
FROM users u
)u
)o
JOIN groups t ON t.grpID = o.thegroupid
Also, if you are wondering, like I did, is this approach faster or slower than doing three queries and processing in php, the answer is that this is slightly faster way. Average time of this query execution and showing results on a webpage is 14 ms. Three simple queries, processing in php and showing results on a webpage took 21 ms. Average is based on 10 cases, average execution time was, really, a constant time.
Here is an approach that uses correlated subqueries to get each of the values. It then chooses the appropriate one using the precedence rule that if the relations exist use that one, otherwise use the one from the groups table:
select u.*,
coalesce(relationgroupid, groupid) as thegroupid
from (select u.*,
(select grpid from groups g where u.usrPts > g.grpMinPts order by g.grpMinPts desc limit 1
) as groupid,
(select gid from relations r where r.userId = u.userId
) as relationgroupid
from users u
) u
Try something like this
select user.name, group.name
from group
join relation on relation.gid = group.gid
join user on user.uid = relation.uid
union
select user.name, g1.name
from group g1
join group g2 on g2.minpts > g1.minpts
join user on user.pts between g1.minpts and g2.minpts

Mysql group concat on double join

I have a user table from which I want all values, so I have this query:
SELECT tbl_user.* FROM tbl_user
Now I want one additional column in this result which shows all roles this user has, (or nothing if there are no roles for the user). The role information comes from two additional tables.
The first table contains these two values: userid, roleid
The second table contains roleid and role_name.
So the group concat needs to get all role names based on the roleid's in table1.
I have tried several different ways to do this, but I don't succeed. Either I get only one result with several times the same rolename, or no result at all.
Thanks for your help
Michael
Update: added LEFT JOIN for users with no role.
SELECT
tbl_user.*,
GROUP_CONCAT(role_name) AS roles
FROM
tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.userid
JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
GROUP BY tbl_user.userid
Note that MySQL will permit a GROUP BY on fewer columns than appear in the SELECT list in total, but in other RDBMS you would need to explicitly list out the columns in tbl_user and include them in the GROUP BY, or do an additional self join against tbl_user to get the remaining columns from that table.
Something like:
SELECT
urole.userid,
uall.username,
uall.name,
uall.othercols,
urole.roles
FROM
tbl_user uall JOIN (
SELECT
tbl_user.userid,
GROUP_CONCAT(role_name) AS roles
FROM
tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.roleid
JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
GROUP BY tbl_user.userid
) urole ON uall.userid = urole.userid