SQL选择同一行数次,但具有不同的查询

发布于 2025-02-09 17:24:05 字数 886 浏览 1 评论 0原文

我的SQL项目中有4个表格,我想在目前使用 2 不同的查询的1行。我设法获得了我想要的结果,但是我不知道如何在同一行中使用同一列获得另一个结果。

这是我现在的查询:

SELECT currencies.currency, swaps.you_send, swaps.you_send, currencies.currency
FROM currencies, swaps
WHERE swaps.currency_to = currencies.id

,然后我得到 this 结果

正如您在最后一行中看到的那样,有同一行,应该是不同的结果 对于不同的结果,需要此查询才能获得正确的结果

SELECT currencies.currency, swaps.you_send, swaps.you_get, currencies.currency
FROM currencies, swaps
WHERE swaps.currency_to = currencies.id

,然后我得到 this 结果

但我需要的是,这是第一行货币中的第一个是

我与Union尝试的最后一排,但随后它们在彼此之间进行了显示。

如果有人可以帮助我解决这个问题,那会很友善。

I have 4 tables in my SQL project and I want to use the same row of 1 table with 2 different queries at the moment. I managed to get the result I wanted once but I can't figure out how to get another result with the same column in the same row.

This is my query now:

SELECT currencies.currency, swaps.you_send, swaps.you_send, currencies.currency
FROM currencies, swaps
WHERE swaps.currency_to = currencies.id

Then I get this as result

as you can see in the last row, there is the same row, that should be a different result
for the different result this query is needed to get the right result

SELECT currencies.currency, swaps.you_send, swaps.you_get, currencies.currency
FROM currencies, swaps
WHERE swaps.currency_to = currencies.id

Then I get this as result

But what I need is that results the first one in the first row of currency the second in the last one

I tried it with UNION but then they get shown among themselves.

Would be very kind if somebody could help me with this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待圉鍢 2025-02-16 17:24:05

您选择了两次,这就是为什么您获得相同值的原因。

这样的事情应该可以工作:

SELECT t.currency_from, t.currency_to
FROM Table1 t
JOIN currencies c1 ON c1.name = t.currency_from
JOIN currencies c2 ON c2.name = t.currency_to

我已经缩小了示例,因为我不知道您的SQL表。

请更新您的问题,以使其更清楚

You select twice the same, thats why you get the same value.

Something like this should work:

SELECT t.currency_from, t.currency_to
FROM Table1 t
JOIN currencies c1 ON c1.name = t.currency_from
JOIN currencies c2 ON c2.name = t.currency_to

I have minifed the example because i dont know your SQL Tables.

Please update your question to make it more clear

温馨耳语 2025-02-16 17:24:05

谢谢,我设法以您的片段的灵感来完成。

SELECT s.you_send,c1.currency, s.you_get, c2.currency
FROM swaps s
JOIN currencies c1 ON c1.id = s.currency_from
JOIN currencies c2 ON c2.id = s.currency_to

    


Thanks I managed to do it with the inspiration of your snippet.

SELECT s.you_send,c1.currency, s.you_get, c2.currency
FROM swaps s
JOIN currencies c1 ON c1.id = s.currency_from
JOIN currencies c2 ON c2.id = s.currency_to

    


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文