如何在MySQL中检查记录是否属于用户?

如何在MySQL中检查记录是否属于用户?,mysql,sql,Mysql,Sql,我有一个SQL查询: 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_aut

我有一个SQL查询:

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] 
我必须检查,这张票是否属于当前用户。用户ID位于
t.uid
中。当不是该用户时,只返回“error”列并显示消息“禁止”。可以只使用MySQL吗

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]
此查询将只显示属于该用户的记录。
如果记录不属于该用户,它将不返回任何内容。

类似“禁止”的消息通常与权限有关。我知道,但我想检查,记录是否存在且不属于该用户(返回错误)、不存在(不返回任何内容)或存在且属于该用户(返回所有内容)。我考虑过,但在这个解决方案中,我不知道票证是否不属于该用户,或者根本不存在。我不确定您试图实现什么,但不管它是什么,都可能应该在查询之外的代码中完成。为什么票证存在但不属于用户有什么关系?我希望避免读取不属于用户的票证,并以最佳方式执行。或者,也许没什么关系,我可以在查询之外执行,而不会损失性能?好的,就性能而言,通常最好是尽可能少地返回记录,因此,最好按用户id进行过滤,这样它就不会拾取任何不属于该用户的票证,并且您可以让数据库引擎找出执行此操作的最佳方法。总的来说,您的查询没有那么复杂,所以在这种情况下,我不会担心性能