Mysql 如何对这个相关子查询建模?

Mysql 如何对这个相关子查询建模?,mysql,sql,Mysql,Sql,所以这个查询返回这个表 select * from players where username = 'weise03' reportID username ign 992499 weise03 Weisey 992637 weise03 Weisey 使用此查询中的reportID,我可以运行 SELECT * FROM reports WHERE reportID = 992499 SELECT * FROM reports WHERE reportID = 992637

所以这个查询返回这个表

select * from players where username = 'weise03'

reportID username ign
992499   weise03  Weisey
992637   weise03  Weisey
使用此查询中的reportID,我可以运行

SELECT * FROM reports WHERE reportID = 992499
SELECT * FROM reports WHERE reportID = 992637


reportID username ign
992499   alphaas  Jester <-- I want this

reportID username   ign
992637   PorcoDiooo Cotton Mather <-- and this
但是你发现你不能在子查询中使用别名,所以这是错误的


我意识到我可以为第一次查询返回的每个reportID运行一个查询,但我想知道是否有更好的方法对此进行建模。如果没有更好的方法,我如何才能在不添加额外字段的情况下使其尽可能快呢?因为将来的某些命令可能需要数百个查询。

听起来您想要加入

select r.* from reports r 
join players p on p.reportid = r.reportid
where p.username = 'weise03'

听起来你想加入

select r.* from reports r 
join players p on p.reportid = r.reportid
where p.username = 'weise03'

你想要的输出是什么?我用什么标记了我想要的两行?您的示例数据不完整。请分享两个表中的样本数据以及期望的输出..你期望的输出是什么?我用什么标记了我想要的两行?您的示例数据不完整。请分享两个表中的样本数据以及所需的输出..我现在意识到我应该澄清这一点。我想要运行的命令只接受用户名,因此reportID必须从查询中派生,我不能像那样加入reportID:/实际上第一个命令起作用了。我确信在胡闹之后我不能做那样的事,但你证明我错了吗?我现在意识到我应该澄清这一点。我想要运行的命令只接受用户名,因此reportID必须从查询中派生,我不能像那样加入reportID:/实际上第一个命令起作用了。我肯定在胡闹之后我不能做那样的事,但你证明我错了吗。