开发者

Get result of two tables

开发者 https://www.devze.com 2023-04-11 03:55 出处:网络
I got two tables \"links\" and \"categories\" how do i get 4 colunms from links and one from categories?

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.


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:

  • w3schools.com/sql - Tutorial about SQL. Generic SQL which works for most database engines.
  • sqlite.com/lang.html - Syntax reference for SQLite, which is used in Android.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号