Siper
Siper

Reputation: 1195

How to check a record belongs to user in MySQL?

I've a SQL Query:

SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod 
FROM `support_tickets_replies` r 
LEFT JOIN `support_tickets` t ON (t.id = r.tid) 
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid) 
LEFT JOIN `users` responser ON (responser.id = r.uid) 
WHERE r.tid = [something goes here] 

I must check, does that ticket belongs to current user. User ID is in t.uid. When it's not that user, just returns column "error" with message "Forbidden". It's possible to do with only MySQL?

Upvotes: 0

Views: 275

Answers (1)

serakfalcon
serakfalcon

Reputation: 3531

SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod 
FROM `support_tickets_replies` r 
LEFT JOIN `support_tickets` t ON (t.id = r.tid) 
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid) 
LEFT JOIN `users` responser ON (responser.id = r.uid) 
WHERE r.tid = [something goes here]
AND t.uid = [User ID goes here]

This query will only turn up records that belong to the user. If the record doesn't belong to the user, it will return nothing.

Upvotes: 1

Related Questions