我正在尝试在 Sqlite 上的 2 个表之间进行内部连接
这是我的内部连接代码:
public PicoEvent getEvent(String eventCode)
{
//Get the readable database instance
SQLiteDatabase db = getReadableDatabase();
//create string for user table inner join
String userTable1 = UsersTable.TABLE_NAME + "1";
String userTable2 = UsersTable.TABLE_NAME + "2";
//create query for getting the event's details
String sql1 = "Select * From " + EventsTable.TABLE_NAME
//set the user tables once as users1 and once as users2 in the inner join process
+ " Inner Join " + UsersTable.TABLE_NAME + " As " + userTable1 + " On "
+ userTable1 + "." + UsersTable.USER_ID + " = " + EventsTable.TABLE_NAME + "." + EventsTable.SIGNED_IN_USER_ID
+ " Inner Join " + UsersTable.TABLE_NAME + " As " + userTable2 + " On "
+ userTable2 + "." + UsersTable.USER_ID + " = " + EventsTable.TABLE_NAME + "." + EventsTable.OWNER_USER_ID
+ " Where " + EventsTable.TABLE_NAME + "." + EventsTable.EVENT_CODE + " = ?";
//selection arguments
String[] selectionArgs = new String[] {eventCode};
//Get the cursor for event's details query
Cursor cursor = db.rawQuery(sql1, selectionArgs);
Log.d("query", sql1.replace("?", eventCode));
Log.d("cursor", String.valueOf(cursor.getCount()));
}
它创建这个查询:
Select * From Events
Inner Join Users As Users1 On Users1.User_Id = Events.Signed_In_User_Id
Inner Join Users As Users2 On Users2.User_Id = Events.Owner_User_Id
Where Events.Event_Code = 10231009
游标总是返回 0 个结果...
当我删除内部连接时 - 我得到了事件的光标..
有任何想法吗 ?
更新——添加了创建语句
/**
* Get the query to create the Users table
* @return the query for the creation of the table ready to be executed
*/
private String getCreateUsersTableQuery()
{
//Create the create query string
String createQuery = "Create Table If Not Exists " + UsersTable.TABLE_NAME + "("
+ UsersTable.USER_ID + " Varchar(50) Primary Key Not Null,"
+ UsersTable.NAME + " Varchar(50) Not Null,"
+ UsersTable.FIRST_NAME + " Varchar(50),"
+ UsersTable.PROFILE_PICTURE + " Blob"
+ ")";
return createQuery;
}
给我创建查询:
Create Table If Not Exists Users(
User_Id Varchar(50) Primary Key Not Null,
Name Varchar(50) Not Null,
First_Name Varchar(50),
Profile_Picture Blob)
/**
* Get the query to create the Events table
* @return the query for the creation of the table ready to be executed
*/
private String getCreateEventsTableQuery()
{
//Create the create query string
String createQuery = "Create Table If Not Exists " + EventsTable.TABLE_NAME + "("
+ EventsTable.EVENT_CODE + " Varchar(50)Not Null,"
+ EventsTable.SIGNED_IN_USER_ID + " Varchar(50)Not Null,"
+ EventsTable.NAME + " Varchar(50) Not Null,"
+ EventsTable.PLACE + " Varchar(100),"
+ EventsTable.START_TIME + " Integer Not Null,"
+ EventsTable.END_TIME + " Integer Not Null,"
+ EventsTable.OWNER_USER_ID + " Varchar(50) Not Null,"
+ EventsTable.IS_SHARING_PICTURES + " Integer Default 1 Not Null,"
+ EventsTable.IS_ASK_BEFORE_SHARING + " Integer Default 1 Not Null,"
+ "Primary Key(" + EventsTable.EVENT_CODE + "," + EventsTable.SIGNED_IN_USER_ID + "),"
+ "FOREIGN KEY(" + EventsTable.SIGNED_IN_USER_ID + ") REFERENCES " + UsersTable.TABLE_NAME + "(" + UsersTable.USER_ID + "),"
+ "FOREIGN KEY(" + EventsTable.OWNER_USER_ID + ") REFERENCES " + UsersTable.TABLE_NAME + "(" + UsersTable.USER_ID + ")"
+ ");";
return createQuery;
}
给我查询:
Create Table If Not Exists Events(
Event_Code Varchar(50)Not Null,
Signed_In_User_Id Varchar(50)Not Null,
Name Varchar(50) Not Null,Place Varchar(100),
Start_Time Integer Not Null,
End_Time Integer Not Null,
Owner_User_Id Varchar(50) Not Null,
Is_Sharing_Pictures Integer Default 1 Not Null,
Is_Ask_Before_Share Integer Default 1 Not Null,
Primary Key(Event_Code,Signed_In_User_Id),
FOREIGN KEY(Signed_In_User_Id) REFERENCES Users(User_Id),
FOREIGN KEY(Owner_User_Id) REFERENCES Users(User_Id));
我正在尝试根据他们的 Id 将事件 Owner_User_Id 和 Signed_In_User_Id 与来自用户表的匹配用户进行内部连接