如何合并从两个单独的表数据到单个光标?光标、并从、两个、数据

2023-09-06 00:25:55 作者:我好中意你

目前,我有职位表和用户表。当然,每个用户可以与多个帖子相关联。在后表的每一行存储创建后的用户的用户ID。下面是例子行:

Currently, I have a table of posts and a table of users. Naturally, each user can be associated with multiple posts. Each row in the post table stores the user ID of the user that created the post. Here are example rows:

邮政行:标题的post_id USER_ID

Post row: post_id headline user_id

用户行:user_ID的用户名

User row: user_id user_name

我想通过匹配的职位表中的用户ID在user表中的用户ID返回一个包含交行和其对应的用户的行既是光标。什么类型的查询,我会用它来实现这一目标?结果应该是:

I want to return a Cursor containing both the post row and its corresponding user row by matching the user ID in the post table to the user ID in the user table. What type of query would I use to achieve this? The result should be:

组合行:标题的post_id USER_ID用户名

Combined row: post_id headline user_id user_name

更普遍:如何合并的基础上共享数据块的两个单独的表中的数据到一个单一的游标

More generally: How do I combine data from two separate tables based on a shared piece of data into a single Cursor?

推荐答案

您也可以在你的Andr​​oid code像这样使用原始的SQLite语句:

You can also use raw SQLite statements in your Android code like such:

SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT serials.id, serials.cabinet_number, serials.serial_number, " +
            "configuration.frequency, configuration.ag, configuration.number_alarms, configuration.failed_rapper, configuration.max_mv, configuration.max_nav " +
            "FROM serials JOIN configuration ON serials.id = configuration.serial_id WHERE serials.id = '" + current_id + "'", null);
    cursor.moveToFirst();

在选择格式为 TABLE_NAME.COLUMN_NAME 。在...上是在那里你将结合基于共享数据块的数据。

In the SELECT the format is table_name.column_name. The ON is where you would combine the data based on a shared piece of data.