我需要根据另一个表选择具有延期值的两次同一列名称
我创建了2个表 第一桌“ storages” 注意ID是pk
第二表“事务”,其中字段名称为(id,source,qty,destination) 还指出ID是PK
我的SQL语句没有给我以下内容,
Select Storages.Name as 'From', Storages.Name as 'To'
from Storages,
Transactions
where Storages.Id = Transactions.Source
and Storages.Id = Transactions.Destination
我还需要显示结果,即使它并没有不同
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用两个
JOIN
获得预期结果,一个在交易来源上,一个在目的地上:如果您不希望多次使用相同的源和目的地组合,请使用
独特的
仅向他们展示一次:请查看SQL教程或文档
JOIN> JOIN
的工作方式,因为这是编写SQL查询时最重要的事情之一。最后一个说明:您的描述说您想在“从”和“到”的结果中命名列。我建议避免这种情况,因为“来自”是SQL关键字,因此您不能仅将其用作列名。因此,我将其命名为“源”和“目的地”。如果您想使用“从”和“无论如何),则可以使用引号:
You can get your expected result using two
JOIN
, one on the source of the transaction and one on the destination:In case you don't want the same combination of source and destination multiple times, use
DISTINCT
to show them only once:Please have a look on SQL tutorials or documentations how
JOIN
works because this is one of the most important things when writing SQL queries.A last note: Your description says you want to name your columns in the result "From" and "To". I recommend to avoid this because "FROM" is a SQL key word, so you can't just use it as column name. Therefore, I named them "Source" and "Destination". If you want to use "From" and "To" anyway, you can use quotes: