我有两张表:Category
& Articles
类别表:
CID | CategoryName
----+--------------
1 | AAA Catg
2 | BBB Catg
3 | CCC Catg
4 | DDD Catg
5 | EEE Catg
6 | FFF Catg
文章表:
AID | CID | ArticleTitle | ArticleURL | Descrption | Date
----+-----+--------------+-----------------+-----------------+-----------
1 | 1 | AA1 Title | http://aa1.html | AA1 Description | 01-07-2017
2 | 1 | AA2 Title | http://aa2.html | AA2 Description | 02-07-2017
3 | 1 | AA3 Title | http://aa3.html | AA3 Description | 03-07-2017
4 | 1 | AA4 Title | http://aa4.html | AA4 Description | 04-07-2017
5 | 1 | AA5 Title | http://aa5.html | AA5 Description | 05-07-2017
6 | 2 | BB1 Title | http://bb1.html | BB1 Description | 01-07-2017
7 | 2 | BB2 Title | http://bb2.html | BB2 Description | 02-07-2017
8 | 2 | BB3 Title | http://bb3.html | BB3 Description | 03-07-2017
9 | 3 | CC1 Title | http://cc1.html | CC1 Description | 01-07-2017
10 | 3 | CC2 Title | http://cc2.html | CC2 Description | 02-07-2017
11 | 4 | DD1 Title | http://dd1.html | DD1 Description | 01-07-2017
我需要通过将Category表与Articles表连接到一个列来生成html,通过获取每个类别的Article表的前3条记录来生成html。 列html应该是:
<li>
<a href="http://aa1.html">aa1 Title</a>
<p>AA1 Description</p>
</li>
<li>
<a href="http://aa2.html">AA2 Title</a>
</li>
<li>
<a href="http://aa3.html">AA3 Title</a>
</li>
第一个<li>
将有一个锚标记,一个段落标记将出现在第一篇文章
<a href="http://aa1.html">aa1 Title</a>
<p>AA1 Description</p>
两个<li>
标签的其余部分将只有锚标签将是接下来的2篇文章
<a href="http://aa2.html">AA2 Title</a>
以下是结果表
CID | Category | HTML
----+----------+---------------------------------------------------------
1 | AAA Catg | <li><a href="http://aa1.html">AA1 Title</a><p>AA1 Description</p></li>
| | <li><a href="http://aa2.html">AA2 Title</a></li>
| | <li><a href="http://aa3.html">AA3 Title</a></li>
2 | BBB Catg | <li><a href="http://bb1.html">BB1 Title</a><p>BB1 Description</p></li>
| | <li><a href="http://bb2.html">BB2 Title</a></li>
| | <li><a href="http://bb3.html">BB3 Title</a></li>
3 | CCC Catg | <li><a href="http://cc1.html">CC1 Title</a><p>CC1 Description</p></li>
| | <li><a href="http://cc2.html">CC2 Title</a></li>
4 | DDD Catg | <li><a href="http://dd1.html">DD1 Title</a><p>DD1 Description</p></li>
5 | EEE Catg |
6 | FFF Catg |
这是我以前的查询
SELECT * FROM
(
SELECT DISTINCT [CID]
FROM [Articles]
) A
CROSS APPLY
(
SELECT TOP 3 '<li><a href="' + B.[ArticleURL] + '">' + B.[ArticleTitle] + '</a><p>' + B.[Description] + '</p></li>'
FROM [Articles] AS B
WHERE A.[CID] = B.[CID]
FOR XML PATH('')
) C (ArticleList)
上述查询将在所有li
代码
<li>
<a href="http://aa1.html">aa1 Title</a>
<p>AA1 Description</p>
</li>
<li>
<a href="http://aa2.html">AA2 Title</a>
<p>AA2 Description</p>
</li>
<li>
<a href="http://aa3.html">AA3 Title</a>
<p>AA3 Description</p>
</li>
但是我只需要在li
个标签中使用段落标记,而不是全部3个。
并且html标签也将采用
< li>
< a href="http://aa3.html">AA3 Title< /a>
< p>AA3 Description< /p>
</li>
如何以html格式获取它?
答案 0 :(得分:0)
尝试以下查询
;with testdata as (
select Articles.CID, CategoryName, ArticleTitle , ArticleURL,Description,Date
,row_number() over(partition by Category.CID order by AID) as rowNumber
from Category inner join Articles on Category.CID= Articles.CID
)
select
case when rowNumber=1 then '<li>' else '' End + '<a href="'
+ [ArticleURL] + '">' + [ArticleTitle] + '</a><p>' + [Description]
+ '</p>' + case when rowNumber=3 then '</li>' else '' End
from testdata where rowNumber<=3
for xml path('') ;
select CategoryName as '@id', (SELECT top 3
ArticleURL as '@href'
, ArticleTITLE as'p'
FROM
Articles sub
WHERE
sub.CID= Category.CID
order by sub.AID
FOR
XML path('a'),
type
)
from Category
for XML path('li')
答案 1 :(得分:0)
我认为您可以使用此查询:
public ArrayList<HashMap<String,Integer>> getAllScores() {
ArrayList<HashMap<String,Integer>> scoresList = new ArrayList<>();
String selectQuery = "SELECT " + COLUMN_ID + ", " + COLUMN_SCORE + " FROM " + TABLE_HIGHSCORE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String,Integer> hashmap = new HashMap<>();
int id = cursor.getInt(0);
int score = cursor.getInt(1);
hashmap.put("score",score);
hashmap.put("id",id);
scoresList.add(hashmap);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return scoresList;
}
public class ScoreAdapter extends ArrayAdapter<HashMap<String,Integer>> {
Context context;
ArrayList<HashMap<String,Integer>> alScore;
int resource;
TextView tvScore;
public ScoreAdapter(Context context, int resource, ArrayList<HashMap<String,Integer>> scores) {
super(context, resource, scores);
this.context = context;
this.resource = resource;
this.alScore = scores;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.scorerow, parent, false);
tvScore = (TextView)view.findViewById(R.id.scores);
HashMap<String,Integer> hashmap = alScore.get(position);
tvScore.setText("Score: " + String.valueOf(hashmap.get("score"));
return view;
}
ListView lv;
ArrayAdapter aa;
ArrayList<HashMap<String,Integer>> al;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score_records);
lv = (ListView)findViewById(R.id.lvScoreRecord);
DBHelper db = new DBHelper(ScoreRecords.this);
al = db.getAllScores();
aa = new ScoreAdapter(this, R.layout.scorerow, al);
lv.setAdapter(aa);
aa.notifyDataSetChanged();
db.close();
}
<强> [ SQL Fiddle Demo ]
强>
的 [ SQL Fiddle Demo - original tags ]
强>