SELECT u.username,
r.position,
r.score,
r.winner,
t.team
FROM ".TBL_FOOT_TOUR_ROUNDS." r
LEFT JOIN ".TBL_USERS." u
ON u.id = r.winner
LEFT JOIN ".TBL_FOOT_TOUR_PLAYERS." pl
ON pl.userid = r.winner
LEFT JOIN ".TBL_FOOT_TEAMS." t
ON t.id = pl.team
WHERE pl.tourid = '$tour_id' && r.tourid = '$tour_id' && r.round = '$i'
ORDER BY r.position
I have one problem with this query. The WHERE pl.tourid = '$tour_id' is reliant on the LEFT JOIN ".TBL_FOOT_TOUR_PLAYERS." pl ON pl.userid = r.winner. As it's a left join, how can I make that WHERE only function if the LEFT JOIN does?
Can't think of a solution!
Thanks
I guess that you only want rows with no matching winner, or where the winner has the specified tourid. In this case, you would use:
WHERE (pl.tourid IS NULL OR pl.tourid = '$tour_id')
Alternatively, if you only want to link to the player if (s)he has the right tourid, then add it to the ON clause:
ON pl.userid = r.winner AND pl.tourid = '$tour_id'
The results will be different, either might be what you are looking for.
SELECT u.username,
r.position,
r.score,
r.winner,
t.team
FROM ".TBL_FOOT_TOUR_ROUNDS." r
LEFT JOIN ".TBL_USERS." u
ON u.id = r.winner
LEFT JOIN ".TBL_FOOT_TOUR_PLAYERS." pl
ON pl.userid = r.winner
AND pl.tourid = '$tour_id'
AND r.tourid = '$tour_id'
AND r.round = '$i'
LEFT JOIN ".TBL_FOOT_TEAMS." t
ON t.id = pl.team
ORDER BY r.position
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;
I get error When I use subquery in select and from clause
Error: https://www.awesomescreenshot.com/image/3069216/0505cad495528e9f9af2281ea281415c.
Here what I have done so far:
SELECT m.id, m.title_name, m.release_date, m.plot, m.poster,
(SELECT count(*) FROM users_ratings WHERE type_id = m.id AND type_name = 'movies' LIMIT 1) count_user_rating
FROM (SELECT u.* FROM users u
LEFT JOIN users_ratings ur ON ur.user_id = u.id
WHERE ur.type_id = 2
AND ur.type_name = 'movies') u
LEFT JOIN users_ratings ur ON ur.user_id = u.id
LEFT JOIN movies m ON m.id = ur.type_id
WHERE ur.type_name = 'movies'
UNION
SELECT s.id, s.title_name, s.first_air_date release_date, s.plot, s.poster,
(SELECT count(*) FROM users_ratings WHERE type_id = s.id AND type_name = 'series' LIMIT 1) count_user_rating
FROM (SELECT u.* FROM users u
LEFT JOIN users_ratings ur ON ur.user_id = u.id
WHERE ur.type_id = 2
AND ur.type_name = 'movies') u
LEFT JOIN users_ratings ur ON ur.user_id = u.id
LEFT JOIN series s ON s.id = ur.type_id
WHERE ur.type_name = 'series'
Not sure whats wrong in your query, seems fine to me. But here is another way to achieve it. Try this
SELECT m.id,
m.title_name,
m.release_date,
m.plot,
m.poster,
t.count_user_rating
FROM (SELECT u.*
FROM users u
LEFT JOIN users_ratings ur
ON ur.user_id = u.id
WHERE ur.type_id = 2
AND ur.type_name = 'movies') u
LEFT JOIN users_ratings ur
ON ur.user_id = u.id
LEFT JOIN movies m
ON m.id = ur.type_id
LEFT JOIN (SELECT type_id,
Count(*) AS count_user_rating
FROM users_ratings
WHERE NAME = 'movies'
GROUP BY type_id) t
ON t.type_id = m.id
WHERE ur.type_name = 'movies'
UNION
SELECT s.id,
s.title_name,
s.first_air_date release_date,
s.plot,
s.poster,
t.count_user_rating
FROM (SELECT u.*
FROM users u
LEFT JOIN users_ratings ur
ON ur.user_id = u.id
WHERE ur.type_id = 2
AND ur.type_name = 'movies') u
LEFT JOIN users_ratings ur
ON ur.user_id = u.id
LEFT JOIN series s
ON s.id = ur.type_id
LEFT JOIN (SELECT type_id,
Count(*) AS count_user_rating
FROM users_ratings
WHERE NAME = 'series'
GROUP BY type_id) t
ON t.type_id = s.id
WHERE ur.type_name = 'series'
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'
this query is works for activity
$query1 = "select t.entity_id, t.parent_id, t.length,gu.measurement_unit_id
from tree t
left join units u
on t.parent_id = u.region_id
and u.activity_id IN (some activity_id)
left join product_grid_units gu
on u.id = gu.unit_id
WHERE t.entity_id IN (some region_id)";
and this query works for region
$query2 = "select t.entity_id, t.parent_id, t.length,gu.measurement_unit_id
from tree t
left join units u
on t.parent_id = u.activity_id
and u.region_id IN (some region_ids)
left join product_grid_units gu
on u.id = gu.unit_id
WHERE t.entity_id IN ("some activity_ids")";
so i want to combine this two querys...can anyone help?...
Use UNION(implicit distinct) or UNION ALL like this:
select t.entity_id, t.parent_id, t.length,gu.measurement_unit_id
from tree t
left join units u on t.parent_id = u.region_id
and u.activity_id IN (some activity_id)
left join product_grid_units gu on u.id = gu.unit_id
WHERE t.entity_id IN (some region_id)
UNION ALL
select t.entity_id, t.parent_id, t.length,gu.measurement_unit_id
from tree t
left join units u on t.parent_id = u.activity_id
and u.region_id IN (some region_ids)
left join product_grid_units gu on u.id = gu.unit_id
WHERE t.entity_id IN ("some activity_ids")
I have this SQL Query.
SELECT
c.id,
c.name,
c.slug,
sc.id,
sc.name,
sc.slug,
COUNT(bsc.id) AS business_count
FROM
fi_category c
LEFT JOIN
fi_subcategory sc ON c.id = sc.category_id AND (sc.deleted_at IS NULL)
LEFT JOIN (
fi_business b
INNER JOIN
fi_business_subcategory bsc ON b.id = bsc.business_id AND (bsc.deleted_at IS NULL)
INNER JOIN
fi_suburb su ON su.id = b.suburb_id AND su.city_id = 1
) ON sc.id = bsc.subcategory_id
WHERE
(c.deleted_at IS NULL)
GROUP BY
c.id, sc.id
as this type of query is not supported by DQL. i want to use it with Doctrine_RawSql
i tried using it this way.
$q = new Doctrine_RawSql();
$q->select('{c.name}, {c.slug}, {sc.name}, {sc.slug}, {COUNT(bsc.id) AS business_count}');
$q->from('fi_category c LEFT JOIN fi_subcategory sc ON c.id = sc.category_id AND (sc.deleted_at IS NULL) LEFT JOIN (fi_business b INNER JOIN fi_business_subcategory bsc ON b.id = bsc.business_id AND (bsc.deleted_at IS NULL) INNER JOIN fi_suburb su ON su.id = b.suburb_id AND su.city_id = 1) ON sc.id = bsc.subcategory_id');
$q->groupBy('GROUP BY c.id, sc.id');
$q->addComponent('c', 'Model_Category c');
$q->addComponent('sc', 'c.Subcategory sc');
$q->addComponent('bsc', 'sc.BusinessSubcategory bsc');
$q->addComponent('b', 'bsc.Business b');
$q->addComponent('su', 'b.Suburb su');
$q->execute();
this query is not working, which is the correct way of building Doctrine_Rawsql for the above SQL query?
if incase if someone wants to check the tables here is the link to fiddle http://sqlfiddle.com/#!2/5adaa
UPDATE: this is continuation of this question, select query and count based on condition if someone could help me build Doctrine_Rawsql by using any of the query from the solution provided by #MvG