I have the following join below and I was wanting to know is there a better way to write it as I am getting Unknown table 'id' in MULTI DELETE and I cannot seem to pin point where.
Join:
$query = $dbConnection->prepare('
DELETE c.id, r.id, s.id,f.id,ip.id,ct.id
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = :campaign_id');
$query->execute(array(':campaign_id' => $campaign_id));
Your DELETE statement is incorrect. You should remove the fields.
DELETE
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = :campaign_id'
Related
can I get the role_ID of a user in a course with a SQL Statemant?
I also want to know what is the role of the user in the course?
Thank you for your help!
SELECT r.id, r.shortname
FROM mdl_role_assignments AS ra
LEFT JOIN mdl_user_enrolments AS ue ON ra.userid = ue.userid
LEFT JOIN mdl_role AS r ON ra.roleid = r.id
LEFT JOIN mdl_context AS c ON c.id = ra.contextid
LEFT JOIN mdl_enrol AS e ON e.courseid = c.instanceid AND ue.enrolid = e.id
WHERE ue.userid = ? AND e.courseid = ?;
I am sorry, I know there is a lot of answer on the forum concerning that question but I didn't succeed to adapt an answer to my problem.
Here is my function :
public function getPupil($db, $pupil_id){
$pupil = $db->query('
SELECT
a.gender as genderPupil,
pupil_name,
pupil_lastname,
a.id as pupilId,
b.id as idOne,
c.id as idTwo,
b.lastname as lastnameOne,
b.name as nameOne,
c.name as nameTwo,
c.lastname as lastnameTwo,
birthdate,
school,
level,
allergies,
special_diet,
tap,
d.name as tapNameFirstTerm,
e.name as tapNameSecondTerm,
f.name as tapNameThirdTerm
FROM pupils a
INNER JOIN responsibles b ON a.id_responsibles = b.id
INNER JOIN responsibles c ON a.id_responsibles_two = c.id
INNER JOIN tap_activities d ON a.tap_first_term = d.id
INNER JOIN tap_activities e ON a.tap_second_term = e.id
INNER JOIN tap_activities f ON a.tap_third_term = f.id
WHERE a.id = ?
', [$pupil_id])->fetch();
return $pupil;
}
I would like to add conditions to do not query the INNER JOIN when a.tap_first_term or a.tap_second_term or a.tap_third_term are equal to 0.
Thank you very much for your answers.
I found my solution.
For the people that could be interested, I just changed the three INNER JOIN for the tap_activities by LEFT JOIN. And it works as I want.
This question already has answers here:
Delete with Join in MySQL
(14 answers)
Closed 7 years ago.
I have the following statement below that is getting an error of You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.' at line 2
What syntax am I missing?
Code:
DELETE
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = '1582'
DELETE c
FROM campaigns c
JOIN campaignsFroms f ON f.id = c.id
JOIN campaignsRaw r ON r.id = c.id
JOIN campaignsSubjects s ON s.id = c.id
JOIN campaignIPTracking ip ON ip.id = c.id
JOIN campaignTracking ct ON ct.id = c.id
WHERE c.id = '1582'
So I have two MySQL queries that if I had the knowledge to combine I would but I don't so that's why I turned to "SO", and in that case I haven't tried anything because its out of my scope. I want to combine all in one query and if that's not possible please let me know.
Query one "This selects all of your friends posts including yours":
"SELECT b.*, c.photo, d.name, e.status
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_friendship e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id
ORDER BY b.id DESC LIMIT 20"
Query two "This selects all of the people your following posts":
"SELECT b.*, c.photo, d.name, e.status
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_follower e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id
ORDER BY b.id DESC LIMIT 20"
I have combined these but with php alone. I'd like to combine both in one single MySQL query. Thanks in advance
SELECT *
FROM
(SELECT b.*,
c.photo,
d.name,
e.status
FROM post b
INNER JOIN profile c
INNER JOIN USER d
INNER JOIN user_friendship e ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id LIMIT 20
UNION SELECT b.*,
c.photo,
d.name,
e.status
FROM post b
INNER JOIN profile c
INNER JOIN USER d
INNER JOIN user_follower e ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id LIMIT 20 ) MainQuery
ORDER BY id DESC
SELECT b.*, c.photo, d.name, e.status, "P"
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_friendship e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.friend_id = b.from_user
WHERE e.status = :status
AND e.user_id = :id
ORDER BY b.id DESC LIMIT 20
UNION
SELECT b.*, c.photo, d.name, e.status, "F"
FROM post b
INNER JOIN profile c
INNER JOIN user d
INNER JOIN user_follower e
ON b.from_user = c.user_id
AND b.from_user = d.id
AND e.to_id = b.from_user
WHERE e.status = :status
AND e.who_id = :id
ORDER BY b.id DESC LIMIT 20
With the extra column you can see where the row is coming from (P = post, F = following)
Keep in mind that union will remove duplicate rows. If you want to see all rows use UNION ALL
I have a select statement like this:
SELECT c.id AS courseid, u.id AS userid
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
I have another piece of data I want that does not have a direct relationship with the current tables. I have been calling this function in the output loop,
function getLastCourseAccessByUser($courseid, $userid){
global $DB;
return
$DB->get_record_sql('
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
FROM mdl_log
WHERE course = '.$courseid.' AND userid = '.$userid.'
ORDER BY time DESC
limit 1
');
}
but would rather get all the data once, and not call back the db.
I am trying this:
SELECT c.id AS courseid, u.id as userid
(
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
FROM mdl_log
WHERE course = c.id AND userid = u.id
) AS lastaccessdate
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
Or, could I call the function directly from the sql like this:
SELECT c.id AS courseid, u.id as userid,
'.getLastCourseAccessByUser(c.id,u.id).' AS lastaccessdate
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
Thank you.
SELECT c.id AS courseid, u.id AS userid, FROM_UNIXTIME(t.time, "%m/%d/%Y") as ftime
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
join mdl_log t on t.course = c.id and t.userid = u.id
You can put AND and OR clauses in a join in mysql so you can join on both your conditionals. Though you may want to make it an outer join so that it won't fail the entire query if the conditionals aren't met
Otherwise your sub select option should also work (this one)
SELECT c.id AS courseid, u.id as userid
(
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
FROM mdl_log
WHERE course = c.id AND userid = u.id
) AS lastaccessdate
FROM
mdl_user_enrolments ue
join mdl_enrol e on e.id = ue.enrolid
join mdl_user u on u.id = ue.userid
join mdl_course c on c.id = e.courseid
The more data you process on the MySQL Server side, the less traffic is generated between the MySQL Server and your PHP application, thus speeding up the application. I would say to get all the data from the server at once and not go back and forth too many times.
You can also create the getLastCourseAccessByUser function as a stored function in MySQL if you need to re-use it in other applications:
DELIMITER \\
DROP FUNCTION IF EXISTS getLastCourseAccessByUser\\
CREATE FUNCTION getLastCourseAccessByUser (in_course_id INT, in_user_id VARCHAR(50)
RETURNS STRING
BEGIN
SELECT FROM_UNIXTIME(time, "%m/%d/%Y") AS ftime
FROM mdl_log
WHERE course = in_course_id AND userid = in_user_id
ORDER BY time DESC
LIMIT 1
END\\
DELIMITER ;
Then you could use it in your SELECT statement:
SELECT c.id AS courseid,
u.id as userid,
getLastCourseAccessByUser(c.id,u.id) AS lastaccessdate
FROM mdl_user_enrolments ue
JOIN mdl_enrol e on e.id = ue.enrolid
JOIN mdl_user u on u.id = ue.userid
JOIN mdl_course c on c.id = e.courseid