Could someone please change the following query to work on postgres?
SELECT
user2.id AS ID,
ul.timeaccess,
user2.firstname AS Firstname,
user2.lastname AS Lastname,
user2.email AS Email,
user2.username AS IDNumber,
user2.institution AS Institution,
IF (user2.lastaccess = 0,'never',
DATE_FORMAT(FROM_UNIXTIME(user2.lastaccess),'%Y-%m-%d')) AS dLastAccess
,(SELECT DATE_FORMAT(FROM_UNIXTIME(timeaccess),'%Y-%m-%d') FROM mdl_user_lastaccess WHERE userid=user2.id AND courseid=c.id) AS CourseLastAccess
,(SELECT r.name
FROM mdl_user_enrolments AS uenrol
JOIN mdl_enrol AS e ON e.id = uenrol.enrolid
JOIN mdl_role AS r ON e.id = r.id
WHERE uenrol.userid=user2.id AND e.courseid = c.id) AS RoleName
FROM mdl_user_enrolments AS ue
JOIN mdl_enrol AS e ON e.id = ue.enrolid
JOIN mdl_course AS c ON c.id = e.courseid
JOIN mdl_user AS user2 ON user2 .id = ue.userid
LEFT JOIN mdl_user_lastaccess AS ul ON ul.userid = user2.id
WHERE c.id=14 AND ue.userid NOT IN (SELECT qa.userid FROM mdl_quiz_attempts AS qa
JOIN mdl_quiz AS q ON qa.quiz = q.id
JOIN mdl_course AS c ON q.course = c.id
WHERE c.id = 14 AND q.name LIKE '%quiz name goes here%')
It is for Moodle
i understand that it has to go to_char etc but don't know how to change it
Thanks in advance
SELECT
user2.id AS "ID",
ul.timeaccess,
user2.firstname AS "Firstname",
user2.lastname AS "Lastname",
user2.email AS "Email",
user2.username AS "IDNumber",
user2.institution AS "Institution",
(CASE WHEN user2.lastaccess = 0 THEN'never' END ||
to_char(to_timestamp(user2.lastaccess)::date,'YYYY-MM-DD')) AS "dLastAccess"
,(SELECT to_char(to_timestamp(timeaccess)::date,'YYYY-MM-DD') FROM mdl_user_lastaccess WHERE userid=user2.id AND courseid=c.id) AS "CourseLastAccess"
,(SELECT r.name
FROM mdl_user_enrolments AS uenrol
JOIN mdl_enrol AS e ON e.id = uenrol.enrolid
JOIN mdl_role AS r ON e.id = r.id
WHERE uenrol.userid=user2.id AND e.courseid = c.id) AS "RoleName"
FROM mdl_user_enrolments AS ue
JOIN mdl_enrol AS e ON e.id = ue.enrolid
JOIN mdl_course AS c ON c.id = e.courseid
JOIN mdl_user AS user2 ON user2 .id = ue.userid
LEFT JOIN mdl_user_lastaccess AS ul ON ul.userid = user2.id
WHERE c.id=14 AND ue.userid NOT IN (SELECT qa.userid FROM mdl_quiz_attempts AS qa
JOIN mdl_quiz AS q ON qa.quiz = q.id
JOIN mdl_course AS c ON q.course = c.id
WHERE c.id = 14 AND q.name LIKE '%quiz name goes here%')
Hey I tried.
FROM_UNIXTIME -is equivalent to to_timestamp on postgres.
IF ELSE are CASE WHEN in postgres.
to_char - if you want to format the date use this.
Have a look at: Data Type Formatting Functions on Postgres docs.
Use to_char(date, 'YYYY-MM-DD')
For example:
select to_char('20170501'::date, 'YYYY-MM-DD')
Returns:
2017-05-01
Check it here: http://rextester.com/JDZA16474
Related
Please help me to combine this two SQL statement so that it will in the $sql and will be interconnected. The final output is in the $sql and I can use member_status as field. Thanks
SELECT UID.data FROM prefix_user_info_field UIF,
prefix_user_info_data UID WHERE UID.fieldid = UIF.id AND
UIF.shortname = 'member_status' AND UID.userid = U.id) as 'member_status'";
$sql = "SELECT ci.id, u.id as userid, $userfields, co.id as courseid,
co.fullname as coursefullname, c.id as certificateid,
c.name as certificatename, c.verifyany
FROM {customcert} c
JOIN {customcert_issues} ci
ON c.id = ci.customcertid
JOIN {course} co
ON c.course = co.id
JOIN {user} u
ON ci.userid = u.id
WHERE ci.code = :code";
$sql = "SELECT ci.id, u.id as userid, $userfields, co.id as courseid,
co.fullname as coursefullname, c.id as certificateid,
c.name as certificatename, c.verifyany,
d.data AS memberstatus
FROM {customcert} c
JOIN {customcert_issues} ci ON c.id = ci.customcertid
JOIN {course} co ON c.course = co.id
JOIN {user} u ON ci.userid = u.id
LEFT JOIN (
SELECT d.userid, d.data
FROM {user_info_data} d
JOIN {user_info_field} f ON f.id = d.fieldid AND f.shortname = 'member_status'
) d ON d.userid = u.id
WHERE ci.code = :code";
How can this be modified with an update statement? Want to update the field "email" in the "mdl_user" table with a value of "users#domain.com" with all records that are returned by this statement
SELECT DISTINCT u.id AS userid, c.id AS courseid
FROM mdl_user u
JOIN mdl_user_enrolments ue ON ue.userid = u.id
JOIN mdl_enrol e ON e.id = ue.enrolid
JOIN mdl_role_assignments ra ON ra.userid = u.id
JOIN mdl_context ct ON ct.id = ra.contextid
AND ct.contextlevel =50
JOIN mdl_course c ON c.id = ct.instanceid
AND e.courseid = c.id
JOIN mdl_role r ON r.id = ra.roleid
AND r.shortname = 'student'
WHERE e.status =0
AND u.suspended =0
AND u.deleted =0
AND ue.status =0
AND courseid =1538
This will update all the selected users:
UPDATE mdl_user
JOIN (
SELECT DISTINCT u.id AS userid, c.id AS courseid
FROM mdl_user u
JOIN mdl_user_enrolments ue ON ue.userid = u.id
JOIN mdl_enrol e ON e.id = ue.enrolid
JOIN mdl_role_assignments ra ON ra.userid = u.id
JOIN mdl_context ct ON ct.id = ra.contextid
AND ct.contextlevel =50
JOIN mdl_course c ON c.id = ct.instanceid
AND e.courseid = c.id
JOIN mdl_role r ON r.id = ra.roleid
AND r.shortname = 'student'
WHERE e.status =0
AND u.suspended =0
AND u.deleted =0
AND ue.status =0
AND courseid =1538
) AS a
ON a.userid = mdl_user.id
SET email = 'users#domain.com'
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 is a query in moodle to get alumn status in courses.
A 'X' indicates user is enrolled, empty not enrolled.
We have to add a subquery for every course we have...
Could anyone help to do a better query?
SELECT
user1.firstname AS Firstname,
user1.lastname AS Lastname,
user1.email AS Email,
if (exists( SELECT
ue.id
FROM mdl_user_enrolments AS ue
JOIN mdl_enrol AS en ON en.id = ue.enrolid
JOIN mdl_course AS course ON course.id = en.courseid
WHERE user1.id = ue.userid AND course.id = '1'), 'X', '') as "Enrolled Course One"
FROM mdl_user AS user1
LEFT JOIN mdl_user_enrolments AS ue ON ue.userid = user1.id
LEFT JOIN mdl_enrol AS en ON en.id = ue.enrolid
LEFT JOIN mdl_course AS course ON course.id = en.courseid
[...]
WHERE user1.deleted='0'
You could try something like this but it will take a long time to process if you have a lot of users and courses
SELECT CONCAT(u.id, '_', c.id) AS uniqueid,
u.id AS userid,
u.firstname,
u.lastname,
u.email,
MAX(CASE WHEN ue.id IS NULL THEN '' ELSE 'X' END) AS enrolled,
c.id AS courseid,
c.fullname AS coursename
FROM mdl_user u
CROSS JOIN mdl_course c
LEFT JOIN mdl_enrol e ON e.courseid = c.id
LEFT JOIN mdl_user_enrolments ue ON u.id = ue.userid AND ue.enrolid = e.id
WHERE u.deleted = 0
GROUP BY u.id, c.id
we're running this query in moodle database:
SELECT l.*,
MAX(l.time) as time,cat.id as catid, cat.name as catname,c.id as courseid,
c.fullname as coursename,
m.name as module,
r.revision as revision,
r.name as resourcename,
sco.name as scormname,
glo.name as glossaryname,
qui.name as quizname
FROM {log} l
INNER JOIN {user} u ON l.userid = u.id
INNER JOIN {course} c ON l.course = c.id
INNER JOIN {course_categories} cat ON c.category = cat.id
INNER JOIN {course_modules} cm ON l.cmid = cm.id
INNER JOIN {modules} m ON cm.module = m.id
LEFT JOIN {resource} r ON cm.instance = r.id and l.module = "resource"
LEFT JOIN {scorm} sco ON cm.instance = sco.id and l.module = "scorm"
LEFT JOIN {glossary} glo ON cm.instance = glo.id and l.module = "glossary"
LEFT JOIN {quiz} qui ON cm.instance = qui.id and l.module = "quiz"
WHERE LOWER(l.action) LIKE LOWER(:modaction) COLLATE utf8_bin AND l.userid = :userid
GROUP BY l.cmid
ORDER BY MAX(l.time) DESC
and i would like something like this:
SELECT l.*,
MAX(l.time) as time,cat.id as catid, cat.name as catname,c.id as courseid,
c.fullname as coursename,
m.name as module,
r.revision as revision,
r.name as resourcename
FROM {log} l
INNER JOIN {user} u ON l.userid = u.id
INNER JOIN {course} c ON l.course = c.id
INNER JOIN {course_categories} cat ON c.category = cat.id
INNER JOIN {course_modules} cm ON l.cmid = cm.id
INNER JOIN {modules} m ON cm.module = m.id
INNER JOIN (
SELECT * {resource} re WHERE cm.instance = r.id OR
SELECT * {scorm} sco WHERE cm.instance = sco.id OR
SELECT * {glossary} WHERE ON cm.instance = glo.id OR
SELECT * {quiz} qui WHERE cm.instance = qui.id OR
) as r
WHERE LOWER(l.action) LIKE LOWER(:modaction) COLLATE utf8_bin AND l.userid = :userid
GROUP BY l.cmid
ORDER BY MAX(l.time) DESC
Results return NULL values in "resource" names (resourcename, scormname, glossaryname or quizname) and only one valid "resource" name. I render results depending on if result is not null.
But i would like to avoid "filter" by not null result in resourcename, scormname, glossaryname and quizname using PHP.
Thanks!
Something like this maybe - use a union
SELECT l.cmid,
MAX(l.time) as logtime,
cat.id as catid,
cat.name as catname,
c.id as courseid,
c.fullname as coursename,
mods.module,
mods.revision,
mods.name as modname
FROM mdl_log l
JOIN mdl_user u ON l.userid = u.id
JOIN mdl_course c ON l.course = c.id
JOIN mdl_course_categories cat ON c.category = cat.id
JOIN (
SELECT cm.id AS cmid, m.name AS module, r.name, r.revision
FROM mdl_course_modules cm
JOIN mdl_modules m ON cm.module = m.id AND m.name = 'resource'
JOIN mdl_resource r ON r.id = cm.instance
UNION
SELECT cm.id AS cmid, m.name as modulentype, s.name, NULL AS revision
FROM mdl_course_modules cm
JOIN mdl_modules m ON cm.module = m.id AND m.name = 'scorm'
JOIN mdl_scorm s ON s.id = cm.instance
UNION
SELECT cm.id AS cmid, m.name as modulentype, g.name, NULL AS revision
FROM mdl_course_modules cm
JOIN mdl_modules m ON cm.module = m.id AND m.name = 'glossary'
JOIN mdl_glossary g ON g.id = cm.instance
UNION
SELECT cm.id AS cmid, m.name as modulentype, q.name, NULL AS revision
FROM mdl_course_modules cm
JOIN mdl_modules m ON cm.module = m.id AND m.name = 'quiz'
JOIN mdl_quiz q ON q.id = cm.instance
) mods ON mods.cmid = l.cmid AND mods.module = l.module
WHERE l.module IN ('resource', 'scorm', 'glossary', 'quiz')
GROUP BY l.cmid
ORDER BY 1 DESC