内联接的sqlLiteDatabase.query() [英] sqlLiteDatabase.query() for INNER JOIN

查看:101
本文介绍了内联接的sqlLiteDatabase.query()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Android SQLite数据库中进行如下查询:

I want to do a query as follows from an Android SQLite db:

select place.permit_name, local.local_distance
from tblLocal local
inner join tblPlaces place
on place._id = local._id
order by local.local_distance asc

我正在尝试使用query(),而不是rawQuery(). 我不知道如何指定INNER JOIN/ON. 我该怎么办?

I'm trying to use query(), instead of rawQuery(). I don't know how to specify the INNER JOIN/ON. How do I do this?

final String table = PlacesDataContract.PlacesDataEntry.TABLE_NAME;
final String[] columns =  { PlacesDataContract.PlacesDataEntry.COLUMN_NAME_PERMIT_NAME. , LocalDataContract.LocalDataEntry.COLUMN_NAME_LOCAL_DISTANCE};
final String orderBy = LocalDataContract.LocalDataEntry.COLUMN_NAME_LOCAL_DISTANCE + " ASC"; 

Cursor cursor = sqLiteDatabase.query(table, // table
        columns, // columns
        null, // selection
        null, // selectionArgs
        null, // groupBy
        null, // having
        orderBy, // orderBy
        null); // limit

推荐答案

您可以将联接放在table变量中.

You can put the join in the table variable.

String table = "tblLocal local " +
    "inner join tblPlaces place " +
    "on place._id = local._id";

有关示例,请参见Google的IOSched应​​用.看看

See the IOSched app from Google for an example. Take a look at the provider package and the SelectionBuilder class.

一个SQLiteQueryBuilder用于构造查询字符串,它所做的只是将表变量连接到查询的其余部分.参见

A SQLiteQueryBuilder is used to construct the query string, and all it does is concatenate the table variable to the rest of the query. See https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/database/sqlite/SQLiteQueryBuilder.java#201

这篇关于内联接的sqlLiteDatabase.query()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆