Combining join results with an at least function - mysql

I'm new to SQL and have managed to pick up the basic functions capably enough, however I'm now trying to find the people with at least two tokens from the results of an inner join:
SELECT
users.[First Name],
users.[Last Name],
IssuedTokens.UserID,
IssuedTokens.TokenID,
Tokens.TokenType
FROM IssuedTokens
INNER JOIN users ON users.ID = IssuedTokens.UserID
INNER JOIN Tokens ON Tokens.number = IssuedTokens.TokenID
GROUP BY IssuedTokens.UserID
HAVING COUNT(*) >= 2
ORDER BY IssuedTokens.UserID
This gives the error:
Column 'Users.First Name' is invalid in the select list because it is
not contained in either an aggregate function or the GROUP BY clause.
I'm comfortable using functions on pre-existing tables, but have not seen how to manipulate the results of a join. If anyone could help it would be much appreciated.

You can do a separate aggregation -- before the join -- to get the users with multiple tokens. Then, the rest of the query doesn't need an aggregation:
SELECT u.[First Name], u.[Last Name], u.UserID, it.TokenID, t.TokenType
FROM IssuedTokens it INNER JOIN
users u
ON u.ID = it.UserID INNER JOIN
Tokens t
ON t.number = it.TokenID INNER JOIN
(SELECT it.UserId
FROM IssuedTokens it
GROUP BY it.UserId
HAVING COUNT(*) >= 2
) itu
ON itu.UserId = it.UserId
ORDER BY it.UserID;

Related

MySQL: How to use column alias in left join?

I am trying to use column alias in the left join but I get sql error with uknown field.
select *, (select SenderId from messages where messageId = 5) as senderId from threads
Left join users where users.id = senderId
This query is quite simple but why is not it working or what is the best way to achieve this ?
I'll really appreciate any contribution.
Thanks
You can't use column aliases in ON or WHERE clauses, because column aliases are assigned to values in the result set, but ON and WHERE are used to create the result set. You need to use another JOIN.
SELECT t.*, m.senderId, u.*
FROM threads AS t
CROSS JOIN messages AS m
LEFT JOIN users AS u ON u.id = m.SenderId
WHERE m.messageId = 5
select t.*, mu.*
from threads as t
Left Join
(select SenderId,users. *
from messages, users
where messages.messageId
= users.id and messsageId = 5) as mu
You need to join threads and mu tables with some id

SQL join to return results only if all rows match

this is my first time on the site so please be gentle. I have tried to find my answer here, but nothing seems to fit the bill and after reading up on different types of joins, I'm still none the wiser.
I have the following tables:
employees (employee_id, name)
assessments (assessment_id, name, pass_score)
authorizations (authorization_id, name)
Employees take assessments which is logged in:
assessments_taken (assessment_taken_id, employee_id, assessment_id, score)
An authorization is composed of multiple individual assessments given by:
authorization_requirements (auth_req_id, authorization_id, assessment_id)
where authorization_id & assessment_id are unique constraints to prevent duplicates of the pair.
The query I am trying to design is something along the lines of:
SELECT employees.name, authorization.name ...
I only want to return employees and authorizations where an employee has passed all assessments for that authorization given in authorization_requirements.
Can anyone help me find the query/sub-queries I am looking for?
The below image may help you somewhat in learning SQL joins - it even gives you some of the SQL itself!. As for the authorization_requirements it sounds like you'll need to be using some WHERE clause along with the join.
Hope this helps, and any clarification, please feel free to ask!
Have a look at this http://sqlfiddle.com/#!2/ac7887/2
I think this query will do it for you. It relies on there only being one entry in the Assessments Taken per employee per assessment. If employees can take the assessments multiple times and all results are recorded in this table, there's a second query that should work for you.
SELECT
Employees.name Employee,
authorizations.name Authorization
FROM
employees
CROSS JOIN authorizations
INNER JOIN authorization_requirements ON authorizations.authorization_id = authorization_requirements.authorization_id
INNER JOIN assessments ON authorization_requirements.assessment_id = assessments.assessment_id
LEFT JOIN assessments_taken ON employees.employee_id = assessments_taken.employee_id AND assessments.assessment_id = assessments_taken.assessment_id AND assessments.pass_score <= assessments_taken.score
GROUP BY
Employees.name,
authorizations.name
HAVING
COUNT(assessments.assessment_id) = COUNT(assessments_taken.assessment_taken_id)
If multiple entries in assessments_taken:
SELECT
Employees.name Employee,
authorizations.name Authorization
FROM
employees
CROSS JOIN authorizations
INNER JOIN authorization_requirements ON authorizations.authorization_id = authorization_requirements.authorization_id
INNER JOIN assessments ON authorization_requirements.assessment_id = assessments.assessment_id
LEFT JOIN (SELECT
assessments_taken.employee_id,
assessments_taken.assessment_id,
MAX(Score) best_score
FROM
assessments_taken
GROUP BY
assessments_taken.employee_id,
assessments_taken.assessment_id
) best_assessments_taken ON employees.employee_id = best_assessments_taken.employee_id AND assessments.assessment_id = best_assessments_taken.assessment_id AND assessments.pass_score <= best_assessments_taken.best_score
GROUP BY
Employees.name,
authorizations.name
HAVING
COUNT(assessments.assessment_id) = COUNT(best_assessments_taken.assessment_id)
These queries get every employee and authorization and then ensure that the employee has got an entry in assessments_taken which exceeds the required score for every assessment specified in authorization_requirements
If CROSS JOIN is not supported you might need to put those two tables in a sub query and then change the join conditions to reference the columns from the sub query instead:
SELECT
employee_authorizations.employee_name Employee,
employee_authorizations.authorization_name Authorization
FROM
(SELECT
employees.employee_id,
employees.name employee_name,
authorizations.authorization_id,
authorizations.name authorization_name
FROM
employees,
authorizations) employee_authorizations
INNER JOIN authorization_requirements ON employee_authorizations.authorization_id = authorization_requirements.authorization_id
INNER JOIN assessments ON authorization_requirements.assessment_id = assessments.assessment_id
LEFT JOIN (SELECT
assessments_taken.employee_id,
assessments_taken.assessment_id,
MAX(Score) best_score
FROM
assessments_taken
GROUP BY
assessments_taken.employee_id,
assessments_taken.assessment_id
) best_assessments_taken ON employee_authorizations.employee_id = best_assessments_taken.employee_id AND assessments.assessment_id = best_assessments_taken.assessment_id AND assessments.pass_score <= best_assessments_taken.best_score
GROUP BY
employee_authorizations.employee_name,
employee_authorizations.authorization_name
HAVING
COUNT(assessments.assessment_id) = COUNT(best_assessments_taken.assessment_id)

MySQL query optimization: Multiple SELECT IN to LEFT JOIN

I usually go with the join approach but in this case I am a bit confused. I am not even sure that it is possible at all. I wonder if the following query can be converted to a left join query instead of the multiple select in used:
select
users.id, users.first_name, users.last_name, users.description, users.email
from users
where id in (
select assigned.id_user from assigned where id_project in (
select assigned.id_project from assigned where id_user = 1
)
)
or id in (
select projects.id_user from projects where projects.id in (
select assigned.id_project from assigned where id_user = 1
)
)
This query returns the correct result set. However, I guess the repetition of the query that selects assigned.id_project is a waste.
You could start with the project assignments of user 1 a1. Then find all assignments of other people to those projects a2, and the user in the project table p. The users you are looking for are then in either a2 or p. I added distinct to remove users who can be reached in both ways.
select distinct u.*
from assigned a1
left join
assigned a2
on a1.id_project = a2.id_project
left join
project p
on a1.id_project = p.id
join user u
on u.id = a2.id_user
or u.id = p.id_user
where a1.id_user = 1
Since both subqueries have a condition where assigned.id_user = 1, I start with that query. Let's call that assignment(s) the 'leading assignment'.
Then join the rest, using left joins for the 'optional' tables.
Use an inner join on user that matches either users of assignments linked to the leading assignment or users of projects linked to the leading project.
I use distinct, because I assumen you'd want each user once, event if they have an assignment and a project (or multiple projects).
select distinct
u.id, u.first_name, u.last_name, u.description, u.email
from
assigned a
left join assigned ap on ap.id_project = a.id_project
left join projects p on p.id = a.id_project
inner join users u on u.id = ap.id_user or u.id = p.id_user
where
a.id_user = 1
Here's an alternative way to get rid of the repetition:
SELECT
users.id,
users.first_name,
users.last_name,
users.description,
users.email
FROM users
WHERE id IN (
SELECT up.id_user
FROM (
SELECT id_user, id_project FROM assigned
UNION ALL
SELECT id_user, id FROM projects
) up
INNER JOIN assigned a
ON a.id_project = up.id_project
WHERE a.id_user = 1
)
;
That is, the assigned table's pairs of id_user, id_project are UNIONed with those of projects. The resulting set is then joined with the user_id = 1 projects to obtain the list of all users who share the projects with the ID 1 user. And now it only remains to retrieve the details for those users, which in this case is done in the same way as in your query, i.e. using an IN clause.
I'm sorry to say that I don't have MySQL to thoroughly test the performance of this query and so cannot be quite sure if it is in any way better or worse than your original query or than the one suggested both by #GolezTrol and by #Andomar. Generally I tend to agree with #GolezTrol's comment that a query with simple (semi- or whatever-) joins and repetitive parts might turn out more efficient than an equivalent sophisticated query that doesn't have repetitions. In the end, however, it is testing that must reveal the final answer for you.

MYSQL JOIN syntax How to Join Three Tables

The following query does what I want. It returns all the resuls in the users table and then if there is a match in the details tble, returns the relevant data
users
id|username
details
id|userid|firstname|lastname
$sql = "SELECT u.*, d.*
FROM `users` u
LEFT JOIN `details` d on
u.id = d.userid
ORDER BY $strorder";
However, when I try to join an additonal table where I want to do the same thing--return all the results of the users table and if there is a match in the third table, return the relevant data (total followers of this user)--it only returns one record.
3rd table
follow
id|followerid|followedid
$sql = "SELECT u.*, d.*, COUNT(f.id)
FROM `users` u
LEFT JOIN `details` d on
u.id = d.userid
LEFT JOIN `follow` f on
u.id = f.followedid
ORDER BY $strorder";
Can anyone see what I am doing wrong? Thanks in advance for any suggestions.
Many thanks.
Try to avoid * to select fields, it will be clearer to group your datas (even if mysql is quite permissive with groupings).
When you have an aggregate function (like COUNT, SUM), the other "non aggregated" requested fields should be in a GROUP BY clause.
Mysql don't force you to GROUP BY all the fields, but... I think it's quite a good habit to be "as ANSI as possible" (usefull when you use another DBMS)
SELECT u.id, u.username, d.firstname, d.lastname, count(*) as numberfollowers
FROM user u
LEFT JOIN details d on u.id = d.userid
LEFT JOIN follow f on u.id = f.followedid
GROUP BY u.id, u.username, d.firstname, d.lastname --or just GROUP BY u.id with Mysql
ORDER BY count(*) desc
COUNT being an aggregate function, when selected with other columns, requires you to group your results by those other columns in the select list.
You should rewrite your query with columns that you want to select from users and details and group by those columns.

SQL Join returns only one record

I'm writing a query whereby I'm trying to count the total number of records in report and assignment table, whiles at the same time retrieving information from the main table group. Group has a primary key id which is saved in the other tables as gid. This is the query:
SELECT `group`.`id` AS `gid`
, `group`.`name` AS `g_name`
, COUNT(`report`.`id`) AS `reports`
FROM `group`
LEFT OUTER JOIN `report` ON `report`.`gid` = `group`.`id`
LEFT OUTER JOIN `assignment` ON `assignment`.`gid` = `group`.`id`
WHERE `group`.`active` = 0
ORDER BY
`group`.`name`;
My problem is whenever I execute this only one record is returned even if theirs multiple groups.
Thanks in advance.
Well, your query is far from correct :) First of all, you should not have aggregated functions (in this case count) without a group by clause. Now, even if you have that clause the query will summarize information and you want both: the detail and a summary in the same query. I'd recommend 2 separate queries to retrieve this information, but if you want information mixed in only one query (the detail and also the "total number of records in report and assignment table") try the following query:
SELECT
`group`.id AS gid,
`group`.name AS g_name,
(SELECT COUNT(*) from report) as ReportTotalCount,
(SELECT COUNT(*) from assignment) as AssignmentTotalCount,
FROM `group`
WHERE `group`.`active` = 0
LEFT OUTER JOIN report ON report.gid = `group`.id
LEFT OUTER JOIN assignment ON assignment.gid = `group`.id
ORDER BY `group`.name;
I whish I could understand exactly what you're looking for but this might give you an idea on how to get the result you expect.
Can't see anything obvious in your query that would limit it to returning one record.
You are going to have to break it up to see where the problem is against your existing data.
So how many groups where acitive = 0, ahow many with a corresponding assignment record, etc.
maybe it will help:
SELECT
groupid,
groupname,
reports,
assignments,
FROM
(SELECT group.id, group.name, COUNT(*) AS reports from group
INNER JOIN report ON (report.gid = group.id)
WHERE group.active = 0
GROUP BY group.id ) AS ReportForGroup
CROSS JOIN
(SELECT group.id AS groupid, group.name AS groupname, COUNT(*) AS assignments from group
INNER JOIN assignmentON (assignment.gid = group.id)
WHERE group.active = 0
GROUP BY group.id ) AS AssignmentForGroup
ON (ReportForGroup.groupid = AssignmentForGroup.groupid)
ORDER BY groupname;
I'm can't check it so if LEFT JOIN returns to COUNT(*) 0 or 1. if it returns 0 just change the INNERs to LEFTs and use INNER JOIN between the two queries