I have a SELECT query with multiple JOIN who works perfectly, but now I want to DELETE this rows, and I can't find how.
This code returns me an error SQL (1064):
DELETE FROM rapport
INNER JOIN course ON rapport.course_id = course.id
INNER JOIN reunion ON course.reunion_id = reunion.id
INNER JOIN paris ON rapport.paris_id = paris.id
WHERE reunion.id = 231431
AND paris.bookmaker_id = 3
I need some help, thanks
Your syntax is wrong.
Use this with aliases also:
DELETE r
FROM rapport r
INNER JOIN course c ON r.course_id = c.id
INNER JOIN reunion u ON c.reunion_id = u.id
INNER JOIN paris p ON r.paris_id = p.id
WHERE u.id = 231431 AND p.bookmaker_id = 3
If you want to delete from all tables then you must specify their aliases after DELETE:
DELETE r, c, u, p
FROM rapport r
INNER JOIN course c ON r.course_id = c.id
INNER JOIN reunion u ON c.reunion_id = u.id
INNER JOIN paris p ON r.paris_id = p.id
WHERE u.id = 231431 AND p.bookmaker_id = 3
Basically, you need to specify what table to delete from. Assuming this is rapport:
DELETE r
FROM rapport r JOIN
course c
ON r.course_id = c.id JOIN
reunion ru
ON c.reunion_id = ru.id JOIN
paris p
ON r.paris_id = p.id
WHERE ru.id = 231431 AND p.bookmaker_id = 3;
MySQL also supports deletion from multiple tables. However, cascading constraints are usually a better approach.
Related
I get an error stating that there is incorrect syntax near the key word INNER. I wish to delete from all tables included in the statement as they are all constraints related to the table Session.
DELETE FROM Session INNER JOIN
Booking ON Session.SessionID = Booking.SessionID INNER JOIN
BookingChild ON Booking.BookingID = BookingChild.BookingID INNER JOIN
Reservations ON Session.SessionID = Reservations.SessionID INNER JOIN
ReservationChild ON Reservations.ReservationID =
ReservationChild.ReservationID INNER JOIN
SessionResources ON Session.SessionID = SessionResources.SessionID
INNER
JOIN
SnackSession ON Session.SessionID = SnackSession.SessionID INNER JOIN
SessionSpeaker ON Session.SessionID = SessionSpeaker.SessionID
WHERE Session.SessionID = 8
Hello you must create foreign key with CASCADE action. After this you can delete row only first table, relation data will delete automatically!
Good luck!
Not an answer; too long for a comment:
FWIW, I find this easier to read:
DELETE
FROM Session s
JOIN Booking b
ON s.SessionID = b.SessionID
JOIN BookingChild bc
ON b.BookingID = bc.BookingID
JOIN Reservations r
ON s.SessionID = r.SessionID
JOIN ReservationChild rc
ON r.ReservationID = rc.ReservationID
JOIN SessionResources sr
ON s.SessionID = sr.SessionID
JOIN SnackSession x
ON s.SessionID = x.SessionID
JOIN SessionSpeaker y
ON s.SessionID = y.SessionID
WHERE s.SessionID = 8
List the tables after the DELETE:
DELETE s, b, bc, r, rc, sr, ss, ssp
FROM Session s JOIN
Booking b
ON s.SessionID = b.SessionID JOIN
BookingChild bc
ON b.BookingID = bc.BookingID JOIN
Reservations r
ON s.SessionID = r.SessionID JOIN
ReservationChild rc
ON r.ReservationID = rc.ReservationID JOIN
SessionResources sr
ON s.SessionID = sr.SessionID JOIN
SnackSession ss
ON s.SessionID = ss.SessionID JOIN
SessionSpeaker ssp
ON s.SessionID = ssp.SessionID
WHERE s.SessionID = 8;
This is described in the documentation under Multiple-Table Syntax.
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 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'
I have a query that I know it should atleast return one row. How can i modify my follow query to make sure that album returns data. thanks for any help.
Query Here. I know that Album has a row and I need to return it.
select distinct p.*,
a.ID as parentalbumid,a.CreatorID as albumcreatorid,a.AlbumName,a.AlbumDescription,a.AlbumDefaultImageURL,a.Private,a.DateCreated,a.AdultContent,a.PasswordProtected,a.AllowTags,a.TypeID,a.AlbumAutoID,
mainuser.Username as mainuserusername,mainuser.ID as mainuserid,mainuser.PictureUrl as mainuserpictureurl,
c.ID as commentID,c.PhotoID as commentphotoid,c.OutputMessage,c.CommentDate,
t.ID as tagID,t.PhotoID as tagphotoid,t.UserID,t.TextTag,t.LeftLocation,t.TopLocation,
u.ID as userid,u.Username,u.FirstName,u.LastName,u.PictureUrl
from photos p
inner join albums a on a.ID = p.AlbumID
inner join users mainuser on mainuser.ID = p.UserID
left join comments c on c.PhotoID = p.ID
left join tags t on t.PhotoID = p.ID
left join users u on u.ID = c.CommentBy
where a.AlbumAutoID = 3
order by p.DateUploaded desc;
Use only LEFT JOINs (and put the table albums as the first table of your FROM):
select distinct p.*,
a.ID as parentalbumid,a.CreatorID as albumcreatorid,a.AlbumName,a.AlbumDescription,a.AlbumDefaultImageURL,a.Private,a.DateCreated,a.AdultContent,a.PasswordProtected,a.AllowTags,a.TypeID,a.AlbumAutoID,
mainuser.Username as mainuserusername,mainuser.ID as mainuserid,mainuser.PictureUrl as mainuserpictureurl,
c.ID as commentID,c.PhotoID as commentphotoid,c.OutputMessage,c.CommentDate,
t.ID as tagID,t.PhotoID as tagphotoid,t.UserID,t.TextTag,t.LeftLocation,t.TopLocation,
u.ID as userid,u.Username,u.FirstName,u.LastName,u.PictureUrl
from albums a
left join photos p on a.ID = p.AlbumID
left join users mainuser on mainuser.ID = p.UserID
left join comments c on c.PhotoID = p.ID
left join tags t on t.PhotoID = p.ID
left join users u on u.ID = c.CommentBy
where a.AlbumAutoID = 3
order by p.DateUploaded desc;
I'm having problems adding a conditional field to my query.
The query returns the jobs that are published on my website (many joines, nothing very speical). The thing is I need to add another field in order to know if the current user already applied for this job (in order to remove the APPLY button)...
I have a jobApplication table, which has the user_id and job_id tables. I thought about using another join but that can't work. I tried to create it with an IF and SELECT but didn't quite managed to make it work :/...
this is my query:
SELECT *, j.job_id as jid, c.name as city_name
FROM jobs j
JOIN areas a ON a.area_id = j.job_area
JOIN positions p ON p.position_id = j.job_position
JOIN fields f ON f.id = j.job_field
JOIN cities c ON j.job_city = c.id
JOIN jobTypes jt ON j.job_type = jt.job_id
JOIN companies comp ON j.job_company = comp.company_id
LEFT JOIN jobApplications ja ON <missing>
WHERE j.job_field = '$field' AND j.job_position = '$position'
AND j.job_area = '$area'
ORDER BY j.creationDate DESC"
any attempt I made to add a condition select broke the query, any chance someone can give me a better direction?
Thanks!!
It's hard to say for sure without knowing more about your schema, but you are on the right track with the left join. You will need to left join onto the applicants table using the applicant Id for the current user and to the job ID from the jobs table.
SELECT
*,
j.job_id as jid,
c.name as city_name
FROM jobs j
JOIN areas a ON a.area_id = j.job_area
JOIN positions p ON p.position_id = j.job_position
JOIN fields f ON f.id = j.job_field
JOIN cities c ON j.job_city = c.id
JOIN jobTypes jt ON j.job_type = jt.job_id
JOIN companies comp ON j.job_company = comp.company_id
LEFT JOIN jobApplications ja ON ja.applicantId = '$applicantId' and ja.jobId = j.jobId
WHERE
j.job_field = '$field'
AND j.job_position = '$position'
AND j.job_area = '$area'
ORDER BY j.creationDate DESC"