我有两个表“链接”和“类别”如何从链接获得4个colunms,从类别中获取一个?
**Links**
_id
link_title
link_desc
link_date
**Categories**
_id
cat_title
cat_desc
我需要像这样的一行 _id,link_title,link_desc,link_date,cat_title
然后在我的光标中使用它
private void fillData() {
Cursor linkCursor = mDbHelper.fetchAllLinks();
startManagingCursor(linksCursor);
String[] from = new String[]{ DbAdapter.LINK_TITLE, DbAdapter.LINK_DESC, DbAdapter.LINK_DATE, DbAdapter.LINK_ROWID, DbAdapter.CAT_DESC };
int[] to = new int[]{ R.id.title, R.id.content, R.id.date, R.id.headid, R.id.catdesc };
SimpleCursorAdapter links = new SimpleCursorAdapter(this, R.layout.linkrow, linkCursor, from, to);
//links.setViewBinder(new MyViewBinder());
setListAdapter(links);
}
我尝试了SQL UNION,但它没有用。
答案 0 :(得分:0)
您想要的是一个连接。但要做到这一点,您需要提供两个ID,或者在Links-table中添加category_id列。
SELECT
l._id,
link_title,
link_desc,
link_date,
cat_title
FROM Links l, Categories c
WHERE l._id = ?
AND c._id = ?
或添加列
之后SELECT
l._id,
link_title,
link_desc,
link_date,
cat_title
FROM Links l
LEFT JOIN Categories c ON c._id = l.link_cat_id
WHERE l._id = ?