gpt4 book ai didi

SQL并排显示两个结果

转载 作者:行者123 更新时间:2023-12-02 19:59:58 25 4
gpt4 key购买 nike

我有两个表,并且正在对每个表进行有序选择。我希望在一个结果中看到两个订单的结果。

示例(简化):

"SELECT * FROM table1 ORDER BY visits;"
name|# of visits
----+-----------
AA | 5
BB | 9
CC | 12
.
.
.

"SELECT * FROM table2 ORDER BY spent;"
name|$ spent
----+-------
AA | 20
CC | 30
BB | 50
.
.
.

我想将结果显示为两列,这样我就可以直观地感受到最常访问的访客是否也是最佳买家。 (我知道这个例子是糟糕的数据库设计,而不是真实场景。它只是一个例子)

我想得到这个:

name by visits|name by spent
--------------+-------------
AA | AA
BB | CC
CC | BB

我正在使用 SQLite。

最佳答案

    Select A.Name as NameByVisits, B.Name as NameBySpent
From (Select C.*, RowId as RowNumber From (Select Name From Table1 Order by visits) C) A
Inner Join
(Select D.*, RowId as RowNumber From (Select Name From Table2 Order by spent) D) B
On A.RowNumber = B.RowNumber

关于SQL并排显示两个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13065105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com