获取两个表的结果

发布于 2024-12-08 04:08:03 字数 845 浏览 0 评论 0原文

我有两个表“链接”和“类别”,如何从链接中获取 4 列,从类别中获取一列?

**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 但它不起作用。

I got two tables "links" and "categories" how do i get 4 colunms from links and one from categories?

**Links**
_id
link_title
link_desc
link_date

**Categories**
_id
cat_title
cat_desc

i need a single row like this
_id, link_title, link_desc, link_date, cat_title

and then use this in my cursor

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);
}

I tried SQL UNION but it dont worked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜柠檬 2024-12-15 04:08:03

您想要的称为联接。但要做到这一点,您需要提供两个 id,或者向链接表添加一个 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 = ?

更多信息:

What you want is called a join. But to do this you need to either give two ids, or add a category_id column to the Links-table.

SELECT
    l._id,
    link_title, 
    link_desc,
    link_date, 
    cat_title
FROM Links l,  Categories c
WHERE l._id = ?
AND c._id = ?

or after adding the column

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 = ?

More information:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文