This question already has answers here:
MySQL pivot table query with dynamic columns
(3 answers)
Closed 2 years ago.
I have 3 tables.
1. user
2. user_role
3. user_role_mapping
Sample record,
user
+--------+----------+---------------+
| userid | username | email |
+--------+----------+---------------+
| 1 | user1 |user1#test.com |
| 2 | user2 |user2#test.com |
+--------+----------+---------------+
user_role
+--------+----------+
| roleid | rolename |
+--------+----------+
| 1 | user |
| 2 | manager |
| 3 | director |
| 4 | admin |
+--------+----------+
User_role_mapping
+--------+------+
| roleid |userid|
+--------+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
+--------+------+
Query
select u.userid, u.username, u.email,
count(case when ur.rolename = 'user' THEN 1 END) user,
count(case when ur.rolename = 'manager' THEN 1 END) manager,
count(case when ur.rolename = 'director' THEN 1 END) director,
count(case when ur.rolename = 'admin' THEN 1 END) admin
from user_role ur
left join userrole_mapping urm
on ur.roleid = urm.roleid
left join user u
on urm.userid = u.userid
group by u.userid, u.username, u.email
Result:-
+--------+----------+---------------+------|---------|----------|-------|
| userid | username | email | user | manager | director | admin |
+--------+----------+---------------+------------------------------------
| 1 | user1 |user1#test.com | 1 | 1 | 1 | 1 |
+--------+----------+---------------+------------------------------------
Here, I am hardcoding the role in the query and the new role can get added in the future and I do not want to do the code change. Is there any better approach? I am fine with comma separated result for role in the result.
For comma separation you can use group_concat:
select u.user_id, u.user_name, u.email,
Group_concat(ur.role_name) roles
from user u
left join user_role_mapping urm
on urm.user_id = u.user_id
left join user_role ur
on ur.role_id = urm.role_id
where u.user_id = ?
group by u.user_id;
Another, maybe better approach w/o hardcoding would be to query the roles separately:
select u.user_id, u.user_name, u.email, ur.role_name
from user u
left join user_role_mapping urm
on urm.user_id = u.user_id
left join user_role ur
on ur.role_id = urm.role_id
where u.user_id = ?;
Related
I have a table containing the following data:
+-----------------+--------------+----------------------+------------+--------------------+-----------+------+---------+---------+-----+------------------------+---------------------+
| notification_id | from_user_id | from_user_auth_level | to_user_id | to_user_auth_level | status_id | type | subject | message | url | timestamp_inserted_utc | timestamp_read_utc |
+-----------------+--------------+----------------------+------------+--------------------+-----------+------+---------+---------+-----+------------------------+---------------------+
| 1 | NULL | NULL | 1 | 1 | 0 | 2 | test | test | url | 2010-10-10 00:00:00 | 2011-10-10 00:00:00 |
| 2 | 2 | 5 | 1 | 1 | 0 | 2 | test | test | url | 2010-10-10 00:00:00 | 2011-10-10 00:00:00 |
| 3 | 3 | 5 | 1 | 1 | 0 | 2 | test | test | url | 2010-10-10 00:00:00 | 2011-10-10 00:00:00 |
| 4 | 2295 | 4 | 1 | 1 | 0 | 2 | test | test | url | 2010-10-10 00:00:00 | 2011-10-10 00:00:00 |
| 5 | 10 | 1 | 1 | 1 | 0 | 2 | test | test | url | 2010-10-10 00:00:00 | 2011-10-10 00:00:00 |
+-----------------+--------------+----------------------+------------+--------------------+-----------+------+---------+---------+-----+------------------------+---------------------+
And then I have some other tables like 'users', 'companies', 'organizations', ... etc.
I need to be able to get the username, gender and image of every notification (based on the from_user_id and from_user_auth_level).
But the problem resides in the fact, that this info resides in different places, depending on what the user_auth_level is.
For example: if my user is a "regular" user, his auth_level will be 1. And the image will reside in my "users" table, and the gender is applicable.
But if the user has auth_level == 5, it means he is an organization. In this case, gender is not applicable, and the image resides in the "organization" table, this needs to be linked via users to user_roles and then to the organization.
And this goes on for every user type, they all require different joins.
I have created a working query, but this uses UNION's everywhere, and I have read that it is not the best to use for performance reasons, so i'm hoping someone can guide me to improving this query with performance in mind:
SELECT n.*, NULL as username, NULL as gender, NULL as picture
FROM notification as n
WHERE n.from_user_auth_level IS NULL
AND n.to_user_id = $userid
UNION
SELECT n.*, u.username, u.gender as gender, u.profile_picture as picture
FROM notification as n
LEFT JOIN users AS u ON n.from_user_id = u.user_id
WHERE n.from_user_auth_level = 1
AND n.to_user_id = $userid
UNION
SELECT n.*, u.username, NULL as gender, c.logo as picture
FROM notification as n
LEFT JOIN users AS u ON n.from_user_id = u.user_id
LEFT JOIN user_companies AS uc on u.user_id = uc.user_id
LEFT JOIN company as c on uc.company_id = c.company_id
WHERE n.from_user_auth_level = 4
AND n.to_user_id = $userid
UNION
SELECT n.*, u.username, NULL as gender, o.logo as picture
FROM notification as n
LEFT JOIN users AS u ON n.from_user_id = u.user_id
LEFT JOIN user_roles as ur on u.user_id = ur.user_id
LEFT JOIN organization as o on ur.org_id = o.org_id
WHERE n.from_user_auth_level = 5
AND n.to_user_id = $userid
UNION
SELECT n.*, u.username, NULL as gender, o.logo as picture
FROM notification as n
LEFT JOIN users AS u ON n.from_user_id = u.user_id
LEFT JOIN user_roles as ur on u.user_id = ur.user_id
LEFT JOIN organization as o on ur.org_id = o.org_id
WHERE n.from_user_auth_level = 7
AND n.to_user_id = $userid
UNION
SELECT n.*, u.username, NULL as gender, NULL as picture
FROM notification as n
LEFT JOIN users AS u ON n.from_user_id = u.user_id
WHERE n.from_user_auth_level = 9
AND n.to_user_id = $userid"
After I get this result, I use PHP to order the results based on the timestamp_inserted_utc, since it's not possible to get the correct results with the UNION.
I'd use the notification table as base and used conditional outer joins as:
select
n.*,t1.gender, t2.orgNo
from
notifications n
left outer join table1 t1 on (n.auth=1 and more join)
left outer join table2 t2 on (n.auth=2 and more..)
You will have more columns but their name would make sense and you may merge at application level.
Suggested indexes (most are "covering"):
n: (from_user_auth_level, to_user_id, from_user_id)
u: (user_id, username, profile_picture, gender)
o: (org_id, logo)
ur: (org_id, user_id)
c: (company_id, logo)
uc: (company_id, user_id)
(They may even speed up Teson's Answer.)
I created a toy dataset where I am trying to count the number of posts for each user. I seem to be getting the correct count values but the count column in the users table is not updated with the values.
I'm new to mysql and very confused! Can somebody tell me what I'm doing wrong?
users:
+---------+------+-------+
| user_id | user | pword |
+---------+------+-------+
| 1 | Amy | abcd |
| 2 | Jess | efgh |
| 3 | Lori | ijkl |
+---------+------+-------+
posts:
+---------+-------------+------+
| post_id | post | user |
+---------+-------------+------+
| 1 | hi | Lori |
| 2 | hello | Jess |
| 3 | hello again | Jess |
| 4 | and again | Jess |
+---------+-------------+------+
mysql> ALTER TABLE users ADD COLUMN post_count INT;
mysql> SELECT u.user_id, COUNT(p.user) AS post_count FROM users u LEFT JOIN posts p ON u.user LIKE p.user GROUP BY u.user_id;
+---------+------------+
| user_id | post_count |
+---------+------------+
| 1 | 0 |
| 2 | 3 |
| 3 | 1 |
+---------+------------+
mysql> SELECT * FROM users;
+---------+------+-------+------------+
| user_id | user | pword | post_count |
+---------+------+-------+------------+
| 1 | Amy | abcd | NULL |
| 2 | Jess | efgh | NULL |
| 3 | Lori | ijkl | NULL |
+---------+------+-------+------------+
Thanks!!
Please try the following...
UPDATE users
JOIN ( SELECT u.user_id AS user_id,
COUNT( p.user ) AS post_count
FROM users u
LEFT JOIN posts p ON u.user LIKE p.user
GROUP BY u.user_id ) postCountFinder
ON users.user_id = postCountFinder.user_id
SET users.post_count = postCountFinder.post_count;
This question takes your list of users and post counts obtained from the following...
SELECT u.user_id,
COUNT( p.user ) AS post_count
FROM users u
LEFT JOIN posts p ON u.user LIKE p.user
GROUP BY u.user_id;
... and performs an INNER JOIN with Users on shared value of user_id, creating a dataset with every row from users having the corresponding count tacked on the end.
We then use the SET command to set the empty post_count from users to its corresponding joined count.
If you have any questions or comments, thenplease feel free to post a Comment accordingly.
You need update statement to update the value in the newly added column.Try this:
Update usr
set usr.post_count=tbl.post_count
from users usr
inner join
(select u.user_id,COUNT(p.user)
AS post_count FROM users u
LEFT JOIN posts p ON u.user LIKE p.user GROUP BY u.user_id ) tbl
on tbl.user_id=usr.user_id
I have this tables:
1) customfields
+------+---------+-------+
|userid| fieldid | value |
+------+---------+-------+
| 1 | 1 | 5c54 |
| 2 | 1 | cerc |
| 2 | 3 | 3545 |
2) users
+------+---------+
|userid| name |
+------+---------+
| 1 | ale |
| 2 | ben |
| 3 | jak |
I want to achieve:
piva: fieldid = 1 in the table customfields
code: fieldid = 3 in the table customfields
+--------+---------+-------+
|name | piva | code |
+--------+---------+-------+
| ale | 5c54 | |
| ben | cerc | 3545 |
I don't know how to achieve this
You can use conditional aggregation:
SELECT c.userid, u.name,
MAX(CASE WHEN fieldid = 1 THEN value END) AS piva,
MAX(CASE WHEN fieldid = 3 THEN value END) AS code
FROM customfields AS c
INNER JOIN users AS u ON c.userid = u.userid
GROUP BY c.userid, u.name
This has nothing to do with a full outer join (which MySQL does not support). One method is just a join with conditional aggregation:
select u.name,
max(case when cf.fieldid = 1 then value end) as piva,
max(case when cf.fieldid = 3 then value end) as code
from customfields cf join
users u
using (userid)
group by u.name;
You could try this SQL-Query:
Select u.name,
(Select value from customfields where fieldid = 1 and userid= u.userid) as piva,
(Select value from customfields where fieldid =3 and userid = u.userid) as code
from users u
I want to create a query for project listings that would give the number of registered applications, excluding the ones for which the user does not exist.
In this case, considering user 10 does not exist, I should have the query results as folows:
RESULTS
+----+------------+--------------+
| id | project | applications |
+----+------------+--------------+
| 1 | MyProject1 | 3 |
| 2 | MyProject2 | 0 |
| 3 | MyProject3 | 0 |
+----+------------+--------------+
TABLES
Projects
+----+------------+
| id | name |
+----+------------+
| 1 | MyProject1 |
| 2 | MyProject2 |
| 3 | MyProject3 |
+----+------------+
applications
+----+------+------------+
| id | user | project_id |
+----+------+------------+
| 1 | 3 | 1 |
| 2 | 4 | 1 |
| 3 | 5 | 1 |
| 4 | 10 | 1 |
+----+------+------------+
users
+----+---------+
| id | Name |
+----+---------+
| 1 | Smith |
| 2 | John |
| 3 | Paul |
| 4 | Chris |
| 5 | Gabriel |
+----+---------+
The below query is not excluding the non-existing users:
SELECT `projects` . * , (
SELECT COUNT( * )
FROM `applications`
WHERE `applications`.`project_id` = `projects`.`id`
AND EXISTS (
SELECT `applications`.`id`
FROM `applications` , `users`,`project`
WHERE `application`.`user` = `users`.`id` AND `applications`.`project_id` = `project`.`id`
)
) AS `applications`
FROM `projects` ORDER BY `id` DESC LIMIT 30
I think you want left join and group by:
select p.id, p.name, count(u.id)
from projects p left join
applications a
on p.id = a.project_id left join
users u
on a.user_id = u.id
group by p.id, p.name;
However, you might want to think about fixing the data. It seems like there should be foreign key relationships between applications and projects and applications and users. The ability to have an invalid user means that there is no valid foreign key relationship to users.
Your query looks overly complicated. This should do:
select
id,
name as project,
(
select count(*)
from applications a
where a.project_id = p.id
and a.user in (select id from users)
) as applications
from projects p;
Based on previous solution
select p.id, p.name, count(u.id)
from projects p left join
applications a
on p.id = a.project_id left join
users u
on a.user = u.id
where u.id is not null
group by p.id, p.name;
When you do a left join, if the search value doesn't exists, it returns null. Then filtering by excluding null users, will give you the result.
Please find a sqlfiddle to illustrate it : http://www.sqlfiddle.com/#!9/cbfec6/3
But easiest solution would be
select p.id, p.name, count(u.id)
from projects p,applications a, users u
where a.user = u.id
and p.id = a.project_id
group by p.id, p.name;
I have these tables:
USER TABLE
uid | name | role
| |
1 | bob | package 1
2 | jill | package 2
3 | pam | package 1
NODE TABLE
nid | uid | type
| |
1 | 1 | car
2 | 1 | car
3 | 1 | car
4 | 2 | page
5 | 1 | car
6 | 3 | car
If I do:
select u.uid, u.name, count(nid) as totalNodes from USER as u left join NODE on n.uid = u.uid where n.type = 'car' group by u.uid
I end up with:
uid | name | totalNodes
| |
1 | bob | 4
3 | pam | 1
In other words, Jill is excluded. Why? And how can I avoid this? I.e. I want Jill to also appear in the list, but with totalNodes as 0 (or even NULL).
You need to perform your aggregate before attempting to join the tables as what you are currently doing is left joining, then restricting the data (at which point Jill is excluded) then grouping. If you do the count and restriction in a subquery you can then left join these results to the user table for the output you want:
SELECT u.uid, u.name, IFNULL(c.nodecount,0) AS `count`
FROM USER u LEFT JOIN (
SELECT uid, `type` , COUNT(nid) AS nodecount
FROM node
WHERE TYPE = 'car'
GROUP BY uid, type
) AS c ON u.uid = c.uid
use RIGHT JOIN instead left,
try :
select u.uid, u.name, count(nid) as totalNodes from USER as u
right join NODE on n.uid = u.uid where n.type IS NULL or n.type = 'car' group by n.uid
see this excellent post a visual explanation of joins :
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
mysql syntax of join :
http://dev.mysql.com/doc/refman/5.0/en/join.html