是否可以使用第一个select语句的结果对第二个select语句进行排序?
我的意思是按val1,val3,val5,val2,val4的顺序。
这意味着如果第一个语句颠倒了我必须颠倒顺序的顺序。
如果可能,请告诉我。
答案 0 :(得分:1)
您尚未发布实际查询,因此我可能认为它们比实际查询更简单,但如果您的第一个陈述是:
select val from valuetable order by someothercolumn
那么你的第二个查询可能是
select table2.name, table2.phonenumber, table2.creationdate, table2.val
from table2
left join valuetable on table2.val = valuetable.val
order by valuetable.someothercolumn
换句话说,您可以将第一个语句的排序复制到第二个语句。
答案 1 :(得分:0)
我完全不确定我理解你的问题,但是我会对它进行一次拍摄。
答案 2 :(得分:0)
据推测,第一个陈述对它有一些排序(如果没有,那么“val”的顺序将是任意的,你不会关心它们。
因此,取第二个语句(返回数据),并将第一个语句保留在第一个语句(返回有序“val”)的val上,并按第一个语句的顺序排序。
答案 3 :(得分:0)
我不确定我完全理解这个问题,但试试这个。我假设你的桌子看起来像这样?
Table1:
myfield1
val1
val2
val2
Table2:
myField2 myDataField
val1 test1
val2 test2
val3 test3
然后你的sql语句看起来像这样
SELECT myDataField
FROM Table2 INNER JOIN Table1 ON Table2.myField2=Table1.myField1
ORDER BY Table1.myField1
HTH
答案 4 :(得分:0)
Simple answer: Yes...But you have to use a secondary ordering column and a nested inner select.
TableOne ValColumn +------+ | val1 | +------+ | val3 | +------+ | val5 | +------+ | val2 | +------+ | val4 | +------+
SelectSetOne rank ValColumn +--------+----------+ | 1 | val1 | +--------+----------+ | 2 | val3 | +--------+----------+ | 3 | val5 | +--------+----------+ | 4 | val2 | +--------+----------+ | 5 | val4 | +--------+----------+
TableTwo ValColumn Col
+-----------+------+ | valN | .... | +-----------+------+Final Select rank ValColumn Col
+--------+-----------+------+ | 1 | val1 | .... | +--------+-----------+------+ | 1 | .... | .... | +--------+-----------+------+ | 1 | val1 | .... | +--------+-----------+------+ | 2 | val3 | .... | +--------+-----------+------+ | 2 | .... | .... | +--------+-----------+------+ | 2 | val3 | .... | +--------+-----------+------+ | 3 | val5 | .... | +--------+-----------+------+ | 3 | .... | .... | +--------+-----------+------+ | 3 | val5 | .... | +--------+-----------+------+ | 4 | val2 | .... | +--------+-----------+------+ | 4 | .... | .... | +--------+-----------+------+ | 4 | val2 | .... | +--------+-----------+------+ | 5 | val4 | .... | +--------+-----------+------+ | 5 | .... | .... | +--------+-----------+------+ | 5 | val4 | .... | +--------+-----------+------+Here is the select statement:
SELECT SelectSetOne.rank, TableTwo.ValColumn, TableTwo.* FROM (SELECT rank=count(*), ValColumn FROM TableOne) as SelectSetOne, INNER JOIN TableTwo ON SelectSetOne.ValColumn = TableTwo.ValColumn ORDER BY SelectSetOne.rank;