DB:MySql
我有两个表-Student和MoneyExchange。学生有列StudentId,FirstName和LastName。 MoneyExchange具有列StudentIDFrom,StudentIdTo和Amount。该表将提供学生之间交换货币的详细信息。 StudentIdFrom和StudentIDTo都引用Student Table中的StudentId列。我想构建一个具有以下结果集的查询:

StudentIdFrom,FirstNameFrom,LastNameFrom,StudentIdTo,FirstNameTo,LastNameTo,Amount(金额的前15个升序)。

最佳答案

您需要为表加上别名,以便可以同时引用它们:

select
    money_ex.StudentIdFrom,
    st_from.FirstName,
    st_from.LastName,
    money_ex.StudentIdTo,
    st_to.FirstName,
    st_to.LastName,
    money_ex.Amount
from
    MoneyExchange as money_ex
    join Student as st_from on money_ex.StudentIdFrom = st_from.StudentId
    join Student as st_to on money_ex.StudentIdTo = st_to.StudentId
order by
    money_ex.Amount asc
limit 0 , 15;

关于mysql - 自连接表和另一个MySQL的结果集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28052397/

10-12 17:12