SQL 并排显示两个结果 [英] SQL display two results side-by-side

查看:83
本文介绍了SQL 并排显示两个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子,正在对每张桌子进行有序选择.我希望在一个结果中看到两个订单的结果.

I have two tables, and am doing an ordered select on each of them. I wold like to see the results of both orders in one result.

示例(简化):

"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
.
.
.

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

I want to display the results as two columns so I can visually get a feeling if the most frequent visitors are also the best buyers. (I know this example is bad DB design and not a real scenario. It is an example)

我想得到这个:

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

我正在使用 SQLite.

I am using 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 并排显示两个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆