我有两张桌子锻炼和练习。我正在尝试使用与我们点击的锻炼匹配的workout_id获取所有锻炼。我做了一个内部联接查询,但它似乎没有返回任何东西。我的查询有问题吗?
我正在使用SQLite创建我的数据库。我已经检查了,以确保练习表中有练习,并且它们有一个workout_id。
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = null;
int match = uriMatcher.match(uri);
switch (match){
case WORKOUT:
cursor = db.query(WorkoutContract.WorkoutEntry.TABLE_NAME_WORKOUT,projection,selection,selectionArgs,null,null,sortOrder);
break;
case WORKOUT_ID:
selection = WorkoutContract.WorkoutEntry._ID + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
cursor = db.query(WorkoutContract.WorkoutEntry.TABLE_NAME_WORKOUT,projection,selection,selectionArgs,null,null,sortOrder);
break;
case EXERCISE:
cursor = db.query(WorkoutContract.WorkoutEntry.TABLE_NAME_EXERCISES,projection,selection,selectionArgs,null,null,sortOrder);
break;
case EXERCISE_ID:
selection = WorkoutContract.WorkoutEntry._ID + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
cursor = db.query(WorkoutContract.WorkoutEntry.TABLE_NAME_EXERCISES,projection,selection,selectionArgs,null,null,sortOrder);
break;
case WORkOUT_EXERCISE:
final String QUERY = "SELECT workouts._id, " +
"exercise_name," +
"weight," +
"reps," +
"rpe"+
" FROM "+
"exercises"+
" INNER JOIN workouts ON workouts._id = exercises.workout_id"+
" WHERE workouts._id=1";
cursor = db.rawQuery(QUERY, null);
break;
default:
throw new IllegalArgumentException("Failed to retrieve" + uri);
}
cursor.setNotificationUri(getContext().getContentResolver(),uri);
int rowcount = cursor.getCount();
Log.d("ROWCOUNT","Number of rows = " + String.valueOf(rowcount));
return cursor; }
String CREATE_TABLE_WORKOUTS = "CREATE TABLE " + WorkoutContract.WorkoutEntry.TABLE_NAME_WORKOUT + " ("
+ WorkoutContract.WorkoutEntry._ID + " INTEGER PRIMARY KEY, "
+ WorkoutContract.WorkoutEntry.WORKOUT_TITLE + " STRING, "
+ WorkoutContract.WorkoutEntry.WORKOUT_DATE + " STRING" + ")";
String CREATE_TABLE_EXERCISES = "CREATE TABLE " + WorkoutContract.WorkoutEntry.TABLE_NAME_EXERCISES + " ("
+ WorkoutContract.WorkoutEntry._ID + " INTEGER PRIMARY KEY, "
+ WorkoutContract.WorkoutEntry.EXERCISE_NAME + " STRING, "
+ WorkoutContract.WorkoutEntry.REPS + " INTEGER, "
+ WorkoutContract.WorkoutEntry.RPE + " INTEGER, "
+ WorkoutContract.WorkoutEntry.WEIGHT + " INTEGER, "
+ WorkoutContract.WorkoutEntry.WORKOUT_ID + " INTEGER, "
+ "FOREIGN KEY"+"("+ WorkoutContract.WorkoutEntry.WORKOUT_ID+")" + " REFERENCES "
+ WorkoutContract.WorkoutEntry.TABLE_NAME_WORKOUT
+"("
+(WorkoutContract.WorkoutEntry._ID) +")" +")";
Uri uri = withAppendedId(WorkoutContract.WorkoutEntry.CONTENT_URI, id);
Uri uri = withAppendedId(WorkoutContract.WorkoutEntry.JOIN_TABLE_URI, id);
2)回到我的WorkoutProvider类中,我将selectionArgs设置为:
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
并把它传递到我的RawQuery中。
cursor = db.rawQuery(QUERY, selectionArgs);
您的问题是,您实际上是在说....其中workouts._id='_id'
因为您有selectionargs=new string[]{string.valueof(workoutcontract.workoutentry.workout_id)};
workoutContract.workoutentry.workout_id是列名_id。
因此,_id列必须是一个整数(在java中是长的,因为它最多是64位有符号整数),因为该列是rowid的别名,所以永远不会与WHERE子句匹配。
相反,你需要像这样的东西
selectionargs=new string[]{string.valueof(the_id_that_you_are_looking_for)};
case WORkOUT_EXERCISE:
String table = WorkoutContract.WorkoutEntry.TABLE_NAME_EXERCISES +
" INNER JOIN " + WorkoutContract.WorkoutEntry.TABLE_NAME_WORKOUT +
" ON " + WorkoutContract.WorkoutEntry.TABLE_NAME_WORKOUT + "." +
WorkoutContract.WorkoutEntry._ID + " = " +
WorkoutContract.WorkoutEntry.TABLE_NAME_EXERCISES + "." +
WorkoutContract.WorkoutEntry.WORKOUT_ID;
String[] columns = new String[]{
WorkoutContract.WorkoutEntry.EXERCISE_NAME,
WorkoutContract.WorkoutEntry.WEIGHT,
WorkoutContract.WorkoutEntry.REPS,
WorkoutContract.WorkoutEntry.RPE
};
String whereclause = WorkoutContract.WorkoutEntry._ID + " =?";
cursor = db.query(table,columns,whereclause,selectionArgs,null,null,null);
break;
case WORkOUT_EXERCISE:
final String QUERY = "SELECT workouts._id, " +
"exercise_name," +
"weight," +
"reps," +
"rpe"+
" FROM "+
"exercises"+
" INNER JOIN workouts ON workouts._id = exercises.workout_id"+
" WHERE workouts._id=?";
cursor = db.rawQuery(QUERY, selectionArgs);
break;
我正在尝试从查询中获取剩余的元组。在本例中,course_id3和4来自courses,因为用户admin@没有使用这些(他们只使用了1和2)。当我尝试左联接时,表已经很好地联接了,查询也正常工作... 但这将返回课程1和2,而不是3和4
我有一个使用TypeORM包与Postgres SQL交互的nestjs应用程序。 我有下面的代码, 对于键和用户实体,我需要将内部连接创建为 选择*FROM key INNER JOIN user ON key.id=user.“keyid”,其中user.“userid”=1; 如何使用typeorm编写等效的内部联接查询? 如何使用typeorm执行上面的SQL查询?
问题内容: 我了解内部和外部联接的工作方式。但是,在此上下文中,“内部/外部”一词的含义是什么?内部联接到底是什么?外部联接的外部是什么? 最好的问候,维沙尔 问题答案: 另一个角度: 最早的简单实现之一是使用嵌套循环的联接。 对于内部联接,只要联接列匹配,外部循环将遍历任何关系,内部循环将遍历其他关系并创建复合行。因此,将在内部循环中创建并填充输出行。因此,这称为INNER JOIN。 当我们希
在第二个选择,它说“'select'意外”
问题内容: 我正在尝试使用value从数据库中获取汽车。我尝试调用值为52 的方法,并检查数据库中是否存在值为52 的汽车。为什么不退还给我? 问题答案: 打电话一次,不要两次。您前进到结果的第一行,然后前进到第二行。由于查询未返回两行,因此不会获得第二行。您跳过了想要的行。
问题内容: 我绝对是SQL的新手,我一直在努力用Postgresql中的以下表结构编写一个复杂的查询: 查询的目的是获取每个用户的报告类型数量,并将其显示在一列中。有三种不同类型的报告。 使用group-by的简单查询将解决问题,但将其显示在不同的行中: 问题答案: