I have an mysql query for getting which user assigned to which course and if the course having certificate then only the results will be prints. for this i am using inner join with many table. Here is the code :
SELECT DISTINCT c.fullname,usr.id, usr.username, usr.email, c.enrolenddate
FROM m_tl_course AS c
INNER JOIN m_tl_context AS cx ON c.id = cx.instanceid AND cx.contextlevel = '50'
INNER JOIN m_tl_role_assignments AS ra ON cx.id = ra.contextid
INNER JOIN m_tl_role AS r ON ra.roleid = r.id
INNER JOIN m_tl_user AS usr ON ra.userid = usr.id
INNER JOIN m_tl_certificate AS ce ON ce.course = c.id
WHERE r.name = "Student" and ra.timeend = '0'
I have an another table to having data's like the user's who's download their certificate. The table name is m_tl_certification.
In this table having Columns like, user_id ( this the user id), Course_id (this is the course id), cert_date ( this is the certificate download date).
What i want is i want to get the user's who is not download their certicate.
How to get this. please can anyone help me ?
What will be the column value if not downloaded and if downloaded. If the Download date is NULL then in where add Download_Date IS NULL
SELECT DISTINCT c.fullname,usr.id, usr.username, usr.email,c.enrolenddate
FROM m_tl_course AS c
INNER JOIN m_tl_context AS cx ON c.id = cx.instanceid AND cx.contextlevel = '50'
INNER JOIN m_tl_role_assignments AS ra ON cx.id = ra.contextid
INNER JOIN m_tl_role AS r ON ra.roleid = r.id
INNER JOIN m_tl_user AS usr ON ra.userid = usr.id
INNER JOIN m_tl_certificate AS ce ON ce.course = c.id INNER JOIN m_tl_certificate AS ce ON ce.course = c.id
INNER JOIN m_tl_certification As cee ON cee.user_id = usr.id
WHERE r.name = "Student" and ra.timeend = '0' and cee.downloadDate IS NULL
Modify your query with the following
SELECT DISTINCT c.fullname,usr.id, usr.username, usr.email, c.enrolenddate
FROM m_tl_course AS c
INNER JOIN m_tl_context AS cx ON c.id = cx.instanceid AND cx.contextlevel = '50'
INNER JOIN m_tl_role_assignments AS ra ON cx.id = ra.contextid
INNER JOIN m_tl_role AS r ON ra.roleid = r.id
INNER JOIN m_tl_user AS usr ON ra.userid = usr.id
LEFT JOIN m_tl_certificate AS ce ON ce.course = c.id
WHERE r.name = "Student" and ra.timeend = '0'
Related
I'm writing a query with multiple joins where I want every user to show entries against all category Types. When I execute the query below only 1 record is returned because the employee u.employee_id = "0079-P" has only worked on 1 project but I want to get data for all the category_types with users workhours displayed as null for the categories he didn't work on.
Select u.employee_id As Employee_ID, u.user_name As UserName, COALESCE(primaryDept.ctd_name, primaryProj.ctd_name) As PrimaryDeptOrProj, region.region_name As Region, categoryType.ctd_id, categoryType.ctd_name, SUM(tsdd.workhours)
From users u
LEFT JOIN category_type_details primaryDept ON u.user_primary_department = primaryDept.ctd_id
LEFT JOIN category_type_details primaryProj ON u.user_primary_project = primaryProj.ctd_id
LEFT JOIN regions region ON u.region_id = region.region_id
LEFT JOIN timesheets ts ON u.user_id = ts.timesheet_user
INNER JOIN timesheet_mr tsmr ON ts.timesheet_caller = tsmr.tsmr_id
INNER JOIN timesheet_details tsd ON ts.timesheet_id = tsd.tsd_timesheet_id
INNER JOIN timesheet_day_details tsdd ON tsd.tsd_id = tsdd.tsd_id
RIGHT OUTER JOIN category_type_details categoryType ON tsd.tsd_category_type_id = categoryType.ctd_id
WHERE tsmr.tsmr_id = 14 and u.employee_id = "0079-P"
GROUP BY u.user_id, tsd.tsd_category_type_id;
I tried this query with variations and it returns 1 record in any case.
You could change your query to this:
SELECT u.employee_id AS Employee_ID, u.user_name AS UserName,
COALESCE(pd.ctd_name, pp.ctd_name) AS PrimaryDeptOrProj,
r.region_name AS Region, ct.ctd_id, ct.ctd_name, SUM(tsdd.workhours)
FROM users u
LEFT JOIN category_type_details pd ON u.user_primary_department = pd.ctd_id
LEFT JOIN category_type_details pp ON u.user_primary_project = pp.ctd_id
LEFT JOIN regions r ON u.region_id = r.region_id
LEFT JOIN timesheets ts ON u.user_id = ts.timesheet_user
INNER JOIN timesheet_mr tsmr ON ts.timesheet_caller = tsmr.tsmr_id AND tsmr.tsmr_id = 14
INNER JOIN timesheet_details tsd ON ts.timesheet_id = tsd.tsd_timesheet_id
INNER JOIN timesheet_day_details tsdd ON tsd.tsd_id = tsdd.tsd_id
RIGHT OUTER JOIN category_type_details ct ON tsd.tsd_category_type_id = ct.ctd_id AND u.employee_id = "0079-P"
GROUP BY ct.ctd_id, u.user_id, u.employee_id, u.user_name,
COALESCE(pd.ctd_name, pp.ctd_name), r.region_name, ct.ctd_name
ORDER BY ct.ctd_id, u.user_id, u.employee_id, u.user_name,
COALESCE(pd.ctd_name, pp.ctd_name), r.region_name, ct.ctd_name;
You only got 1 row because the condition in WHERE clause filter all the NULL user_id rows for other category.
For MySQL, you could omit other columns in GROUP BY clause:
SELECT u.employee_id AS Employee_ID, u.user_name AS UserName,
COALESCE(pd.ctd_name, pp.ctd_name) AS PrimaryDeptOrProj,
r.region_name AS Region, ct.ctd_id, ct.ctd_name, SUM(tsdd.workhours)
FROM users u
LEFT JOIN category_type_details pd ON u.user_primary_department = pd.ctd_id
LEFT JOIN category_type_details pp ON u.user_primary_project = pp.ctd_id
LEFT JOIN regions r ON u.region_id = r.region_id
LEFT JOIN timesheets ts ON u.user_id = ts.timesheet_user
INNER JOIN timesheet_mr tsmr ON ts.timesheet_caller = tsmr.tsmr_id AND tsmr.tsmr_id = 14
INNER JOIN timesheet_details tsd ON ts.timesheet_id = tsd.tsd_timesheet_id
INNER JOIN timesheet_day_details tsdd ON tsd.tsd_id = tsdd.tsd_id
RIGHT OUTER JOIN category_type_details ct ON tsd.tsd_category_type_id = ct.ctd_id AND u.employee_id = "0079-P"
GROUP BY ct.ctd_id, u.user_id
ORDER BY ct.ctd_id, u.user_id;
My current query:
select users.id as user_id, opportunities.id as op_id, opportunities.title, certificates.id as cert_id from opportunities
join opportunity_certificates on opportunities.id=opportunity_certificates.opportunity_id
join certificates on opportunity_certificates.certificate_id=certificates.id
join user_certificates on certificates.id=user_certificates.certificate_id
join users on user_certificates.user_id=users.id
where opportunity_certificates.is_required = 1 and
opportunities.id = 1
This produces the table on the picture below.
cert_id column can have values from 1 to 7, depends on the opportunities.id. In the table below, I want the query to return only the rows which have the same user_id but different cert_id, 1 and 2.
If the table had 3 different cert_id, I would want it to return only the rows which have same user_id but different cert_id, 1,2 and 3.
when the cert_id has only one value, query should return all the records with that one value in cert_id. Basically, it should show all users who have all required certificates.
The query has to be in the current format. I experimented with
group by users.id
having count(*) >
but I don't know how to make that comparison dynamic, relative to the count of distinctive values in the cert_id column.
Compare counts with a having condition.
select u.id as user_id --, o.id as op_id, o.title
from opportunities o
join opportunity_certificates oc on o.id=oc.opportunity_id
join certificates c on oc.certificate_id=c.id
join user_certificates uc on c.id=uc.certificate_id
join users u on uc.user_id=u.id
where oc.is_required = 1 and o.id = 1
group by u.id --,o.id,o.title
having count(distinct c.id)=(select count(distinct id) from certificates)
Useful?
with data as (
select users.id as user_id, o.title, c.id as cert_id
from opportunities o
inner join opportunity_certificates oc on oc.opportunity_id = o.id
inner join certificates c on c.id = oc.certificate_id
inner join user_certificates uc on uc.certificate_id = c.id
inner join users u on u.id = uc.user_id
where oc.is_required = 1 and o.id = 1
)
select user_id, min(title) as title, max(cert_id) as num_certs
from data
group by user_id
having count(cert_id) = (select max(cert_id) from data);
I'm assuming that cert_id values start and 1 and run sequentially. You could also use count(distinct ...) in the having clause but it guess it's debatable which ones expresses you intent more clearly.
If your version of MySQL doesn't support CTEs then you should be able to just drop that whole subquery into the having clause as well.
select u.id as user_id, min(o.title) as title, max(c.cert_id) as num_certs
from opportunities o
inner join opportunity_certificates oc on oc.opportunity_id = o.id
inner join certificates c on c.id = oc.certificate_id
inner join user_certificates uc on uc.certificate_id = c.id
inner join users u on u.id = uc.user_id
where oc.is_required = 1 and o.id = 1
group by u.id
having count(c.cert_id) = (
select max(c.cert_id)
from opportunities o
inner join opportunity_certificates oc on oc.opportunity_id = o.id
inner join certificates c on c.id = oc.certificate_id
inner join user_certificates uc on uc.certificate_id = c.id
inner join users u on u.id = uc.user_id
where oc.is_required = 1 and o.id = 1
);
Here's another one that might work if you have window functions available. (It might work with Laravel better?):
select *
from (
select users.id as user_id, o.title,
count(distinct c.id) over (partition by u.id) as user_certs,
max(c.id) over () as total_certs
from opportunities o
inner join opportunity_certificates oc on oc.opportunity_id = o.id
inner join certificates c on c.id = oc.certificate_id
inner join user_certificates uc on uc.certificate_id = c.id
inner join users u on u.id = uc.user_id
where oc.is_required = 1 and o.id = 1
) t
where user_certs = total_certs;
Hello. I have just learned how to select all related data to, say, the row
tennismatch.ID = 1:
SELECT * FROM tennismatch m
JOIN tennismatch_tennisset ms
ON m.`ID` = ms.`TennisMatch_ID`
JOIN tennisset s
ON ms.`mapOfSets_ID` = s.`ID`
JOIN tennisset_game sg
ON s.`ID` = sg.`TennisSet_ID`
JOIN game g
ON sg.`gamesMap_ID` = g.`ID`
JOIN game_point gp
ON g.`ID` =gp.`Game_ID`
JOIN point p
ON gp.`points_ID` = p.`ID`
WHERE m.`ID` = 1
but I cannot figure how to delete it ALL.
Big thanks in advance.
Use a DELETE JOIN statement
DELETE m, s, g, p, ms, sg, gp
FROM tennismatch m
JOIN tennismatch_tennisset ms ON m.ID = ms.TennisMatch_ID
JOIN tennisset s ON ms.mapOfSets_ID = s.ID
JOIN tennisset_game sg ON s.ID = sg.TennisSet_ID
JOIN game g ON sg.gamesMap_ID = g.ID
JOIN game_point gp ON g.ID = gp.Game_ID
JOIN point p ON gp.points_ID = p.ID
WHERE m.ID = 1
I have a query that shows a list of revisions and employees for every revision..
Now I'm trying to show if the given employee already has a row in the answers table..
This is the overview of the database
This is my working query that shows the list of revisions and employees
SELECT l.id, l.naam, r.id as revision_id, r.beschrijving, e.id as employee_id, e.voornaam, e.achternaam,
FROM lists l
INNER JOIN revisions r ON l.id = r.list_id
INNER JOIN employeelists el ON el.list_id= l.id
INNER JOIN employees e ON e.id = el.employee_id
INNER JOIN customers c ON c.id = e.customer_id
WHERE customer_id = :id AND r.actief = 1
Now I've tried several things to see if the employee already has a record in the answers table.. But It's failing the whole time..
Try 1 : Adding the Answers table with a left outer join
SELECT l.id, l.naam, r.id as revision_id, r.beschrijving, e.id as employee_id, e.voornaam, e.achternaam,
**CASE WHEN a.coach_id != 0 THEN 1 ELSE 0 END as FILLED IN**
FROM lists l"""
INNER JOIN revisions r ON l.id = r.list_id
**LEFT OUTER JOIN answers a ON a.revision_id = r.id**
INNER JOIN employeelists el ON el.list_id= l.id
INNER JOIN employees e ON e.id = el.employee_id
INNER JOIN customers c ON c.id = e.customer_id
WHERE customer_id = :id AND r.actief = 1
now the problem is that every employee is shown multiple times...
This is the SQLFiddle of the working database, The only thing i can't do is check if the given employee ( werknemer ) exists in the answers ( antwoorden ) table..
http://sqlfiddle.com/#!2/0c01c/4
Any idea on how i can solve this? I tried a subquery, but that didn't work out either.. Thanks!
Problem with Query Now
I thought i was correct but there's one more error. in the answers table, it shows results for werknemer_id ( employee_id ) = 78. For the revisie ( revision ) 1 and 2
While there is only results for revisie 1 (screenshot below)
Thanks!
How about this exist column will have 0 if not in antwoorden and 1 if exist
SELECT l.id, l.naam, r.revisie as revisie, r.id as revisie_id, r.beschrijving, w.id as werknemer, w.voornaam, w.achternaam
, a.werknemer_id,
(CASE WHEN a.werknemer_id IS NULL THEN 0 ELSE 1 END ) AS `exist`
FROM lijsten l
INNER JOIN revisies r ON l.id = r.lijst_id
INNER JOIN werknemerlijsten wl ON wl.lijst_id = l.id
INNER JOIN werknemers w ON w.id = wl.werknemer_id
INNER JOIN klanten k ON k.id = w.klant_id
LEFT JOIN antwoorden a ON w.id = a.werknemer_id
WHERE klant_id = 39 AND r.actief = 1
GROUP BY r.beschrijving, w.id
Fiddle
Final solution
This is the final outcome, only see 'ingevuld' is 1 if the desired coach (1 in thise case) is in the answers table.
SELECT l.id, l.naam, r.revisie AS revisie, r.id AS revisie_id, r.beschrijving, w.id AS werknemer, w.voornaam, w.achternaam, a.coach_id,
CASE WHEN a.coach_id = 1 THEN 1 ELSE 0 END AS ingevuld
FROM lijsten l
INNER JOIN revisies r ON l.id = r.lijst_id
INNER JOIN werknemerlijsten wl ON wl.lijst_id = l.id
INNER JOIN werknemers w ON w.id = wl.werknemer_id
INNER JOIN klanten k ON k.id = w.klant_id
LEFT JOIN antwoorden a ON w.id = a.werknemer_id AND r.id=a.revisie_id
WHERE klant_id = 39 AND r.actief = 1
group by r.id, w.id, a.coach_id
select
s.id, s.description, s.improvement, s.previous_year_id,
s.current_year_id, s.first_name, s.last_name, s.username,
s.finding, s.action, s.share, s.learned, s.timestamp,
d.title as department_title,
group_concat(g.title SEPARATOR \' | \') as strategic_goals,
y1.year as current_year_title, y2.year as previous_year_title,
u.summary_id, u.file_name as file_name
from
summary s, year y1, year y2, strategic_goal_entries sge,
goal g, department d, uploads u
where
s.id = sge.summary_id
and
s.current_year_id = y1.id
and
s.previous_year_id = y2.id
and
sge.goal_id = g.id
and
s.id = u.summary_id
and
s.department_id = d.id
and
s.department_id = '4'
group by
s.id
This only returns records from the summary table that has a relating record in the uploads table (s.id = uploads.summary_id) that contain a value within the uploads.summary_id field
I want to return all records, whether or not it has a file associated with it.
Any help is appreciated.
Suggest refactoring this SQL query to use ANSI joins. To achive your goal, you'd want a LEFT JOIN instead:
SELECT /*your columns*/
from summary s
INNER JOIN year y1 ON s.current_year_id = y1.id
INNER JOIN year y2 ON s.previous_year_id = y2.id
INNER JOIN strategic_goal_entries sge ON s.id = sge.summary_id
INNER JOIN goal g ON sge.goal_id = g.id
INNER JOIN department d ON s.department_id = d.id
LEFT JOIN uploads u ON s.id = u.summary_id
WHERE s.department_id = '4'
group by s.id