如果我在这样的数据库中有4行。
|____A____|____B_____|
| a1 | b1 |
| a2 | b2 |
| a3 | b3 |
| a4 | b4 |
但我需要添加NO
列来显示10行,以获取每行的序列号
__NO__|____A____|____B_____|
1 | a1 | b1 |
2 | a2 | b2 |
3 | a3 | b3 |
4 | a4 | b4 |
5 | | |
6 | | |
7 | | |
8 | | |
9 | | |
10 | | |
如何通过sql server查询?
答案 0 :(得分:7)
在这里小提琴:http://sqlfiddle.com/#!3/9a9dd/1
public void Showdata(View view)
{
Cursor c = db.rawQuery("SELECT * FROM Student2", null);
int count = c.getCount();
c.moveToFirst();
TableLayout tablelayout = new TableLayout(getApplicationContext());
tablelayout.setVerticalScrollBarEnabled(true);
TableRow tablerow;
TextView textview, textview1, textview3, textview5;
tablerow = new TableRow(getApplicationContext());
textview = new TextView(getApplicationContext());
textview.setText("Name");
textview.setTextColor(Color.RED);
textview.setTypeface(null, Typeface.BOLD);
textview.setPadding(20, 20, 20, 20);
tablerow.addView(textview);
textview5 = new TextView(getApplicationContext());
textview5.setText("Email");
textview5.setTextColor(Color.RED);
textview5.setTypeface(null, Typeface.BOLD);
textview5.setPadding(20, 20, 20, 20);
tablerow.addView(textview5);
tablelayout.addView(tablerow);
for(Integer j=0; j< count; j++)
{
tablerow = new TableRow(getApplicationContext());
textview1 = new TextView(getApplicationContext());
textview1.setText(c.getString(c.getColumnIndex(name)));
textview3 = new TextView(getApplicationContext());
textview3.setText(c.getString(c.getColumnIndex(email)));
textview1.setPadding(20, 20, 20, 20);
textview3.setPadding(20, 20, 20, 20);
tablerow.addView(textview1);
tablerow.addView(textview3);
tablelayout.addView(tablerow);
c.moveToNext();
}
setContentView(tablelayout);
db.close();
}
答案 1 :(得分:1)
这是另一种不使用递归cte和LEFT JOIN
的方法。
;WITH Cte AS(
SELECT
NO = ROW_NUMBER() OVER (ORDER BY A, B), A, B
FROM tbl
UNION ALL
SELECT
t.n, NULL, NULL
FROM (VALUES
(11), (12), (13), (14), (15), (16), (17), (18), (19), (20)
)t(n)
),
CteFinal AS(
SELECT *, rn = ROW_NUMBER() OVER (ORDER BY NO)
FROM Cte
)
SELECT
rn AS NO, A, B
FROM CteFinal
WHERE rn < = 10