在具有两个字段而不是一个字段的两个表上执行内部联接

时间:2017-11-22 08:23:50

标签: android sqlite join inner-join

我有一个包含4个表的数据库。

设置

拖车

评论

final String SQL_CREATE_MOVIE__SETTING_TABLE = "CREATE TABLE " + MovieContract.MovieSettings.TABLE_NAME + " (" +
            MovieContract.MovieEntry._ID + " INTEGER PRIMARY KEY, " +
            MovieContract.MovieSettings.COL_MOVIE_SETTING + " TEXT UNIQUE NOT NULL " +
            " );";

    final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " + MovieContract.MovieEntry.TABLE_NAME + " (" +
            MovieContract.MovieEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            MovieContract.MovieEntry.COL_MOVIE_KEY + " INTEGER NOT NULL, " +
            MovieContract.MovieEntry.COL_MOVIE_ID + " INTEGER NOT NULL, " +
            MovieContract.MovieEntry.COL_BACKDROP_PATH + " TEXT NOT NULL, " +
            MovieContract.MovieEntry.COL_ORIGINAL_LANGUAGE + " TEXT NOT NULL, " +
            MovieContract.MovieEntry.COL_ORIGINAL_TITLE + " TEXT NOT NULL," +
            MovieContract.MovieEntry.COL_OVERVIEW + " TEXT NOT NULL, " +
            MovieContract.MovieEntry.COL_TITLE + " TEXT NOT NULL, " +
            MovieContract.MovieEntry.COL_VOTE_AVERAGE + " REAL NOT NULL, " +
            MovieContract.MovieEntry.COL_VOTE_COUNT + " INTEGER NOT NULL, " +
            MovieContract.MovieEntry.COL_POSTER_PATH + " TEXT NOT NULL, "  +
            MovieContract.MovieEntry.COL_DATE + " TEXT NOT NULL, " +

            // Set up the location column as a foreign key to location table.
            " FOREIGN KEY (" + MovieContract.MovieEntry.COL_MOVIE_KEY + ") REFERENCES " +
            MovieContract.MovieSettings.TABLE_NAME + " (" + MovieContract.MovieSettings._ID + "), " +

            // To assure the application have just one weather entry per day
            // per location, it's created a UNIQUE constraint with REPLACE strategy
            " UNIQUE (" + MovieContract.MovieEntry.COL_DATE + ", " +
            MovieContract.MovieEntry.COL_MOVIE_KEY + ") ON CONFLICT REPLACE);";

    final String SQL_CREATE_TRAILER_TABLE = "CREATE TABLE " + MovieContract.TrailerEntry.TABLE_NAME + " (" +
            MovieContract.TrailerEntry._ID + " INTEGER PRIMARY KEY," +
            MovieContract.TrailerEntry.COL_MOVIE_ID + " INTEGER NOT NULL, " +
            MovieContract.TrailerEntry.COL_TRAILER_ID + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_ISO_369_1 + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_KEY + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_NAME + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_SITE + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_SIZE + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_TYPE + " TEXT NOT NULL, " +


            // Set up the movie_id column as a foreign key to movie table.
            " FOREIGN KEY (" + MovieContract.TrailerEntry.COL_MOVIE_ID + ") REFERENCES " +
            MovieContract.MovieEntry.TABLE_NAME + " (" + MovieContract.MovieEntry.COL_MOVIE_ID + ") " +

            " UNIQUE (" + MovieContract.TrailerEntry.COL_MOVIE_ID + ", " +
            MovieContract.TrailerEntry.COL_TRAILER_ID + ") ON CONFLICT REPLACE);";

    final String SQL_CREATE_REVIEW_TABLE = "CREATE TABLE " + MovieContract.ReviewEntry.TABLE_NAME + " (" +
            MovieContract.ReviewEntry._ID + " INTEGER PRIMARY KEY," +
            MovieContract.ReviewEntry.COL_MOVIE_ID + " INTEGER NOT NULL, " +
            MovieContract.ReviewEntry.COL_REVIEW_ID + " TEXT NOT NULL, " +
            MovieContract.ReviewEntry.COL_AUTHOR + " TEXT NOT NULL, " +
            MovieContract.ReviewEntry.COL_CONTENT + " TEXT NOT NULL, " +
            MovieContract.ReviewEntry.COL_URL + " TEXT NOT NULL, " +

            // Set up the movie_id column as a foreign key to movie table.
            " FOREIGN KEY (" + MovieContract.ReviewEntry.COL_MOVIE_ID + ") REFERENCES " +
            MovieContract.MovieEntry.TABLE_NAME + " (" + MovieContract.MovieEntry.COL_MOVIE_ID + ") " +

            " UNIQUE (" + MovieContract.ReviewEntry.COL_MOVIE_ID + ", " +
            MovieContract.ReviewEntry.COL_REVIEW_ID + ") ON CONFLICT REPLACE);";

我现在想要将trailers表内连接到movie表中。因此新的预告片表应该包含以下列。

----------------------------------------------------------------------------------
|_id  |movie_id |poster_path  |id  |iso_639_1  |key  |name  |site  |size  |type  |

movie_id poster_path 列都属于电影表格。我执行内连接

sTrailerByMovieIdQueryBuilder.setTables(
            MovieContract.TrailerEntry.TABLE_NAME + " INNER JOIN " +
            MovieContract.MovieEntry.TABLE_NAME + " ON " +
            MovieContract.TrailerEntry.TABLE_NAME + "." + 
            MovieContract.TrailerEntry.COL_MOVIE_ID + " = " +
            MovieContract.MovieEntry.TABLE_NAME + "." + 
            MovieContract.MovieEntry.COL_MOVIE_ID + " AND " +
            MovieContract.MovieEntry.TABLE_NAME + "." + 
            MovieContract.MovieEntry.COL_POSTER_PATH
        );

但是我得到了这张桌子

--------------------------------------------------------------------
|_id  |movie_id |id  |iso_639_1  |key  |name  |site  |size  |type  |

缺少 poster_path 列。我在内心联系中做错了什么但我不知道如何修复它。

谢谢,

西奥。

1 个答案:

答案 0 :(得分:0)

缺少poster_path列的原因是您无法在查询中调用另一个内部联接。

目前,您的查询如下所示:

trailers 
INNER JOIN movies
ON trailers.movie_id = movies.movie_id
AND movies.poster_path

您的查询应如下所示:

... 
INNER JOIN movies m1
ON trailers.movie_id = m1.movie_id
INNER JOIN movies m2
ON trailers.poster_path = m2.poster_path