如何匹配来自多个表的两行?

时间:2018-08-12 15:58:31

标签: android android-recyclerview android-sqlite

我已经在Stackoverflow中看到了这个问题,但是我无法得到想要的东西 我有两张桌子,教师和学生

$(".list").on('click', function() {
    if ($(this).hasClass('ani')) {
        $(this).removeClass("ani");
    }
    else {
        $('.list').removeClass("ani");
        $(this).addClass("ani");
    }
});

现在,我想同时匹配学生和教职员工,并显示在Recyclerview上,但我无法做到。

简介:- 我正在制作一个出勤应用程序,当老师进入教室时 而出勤时间该应用程序将仅显示正在学习特定学科的学生,我的意思是假设我是老师并且我只教英语,所以我教室里的学生都是英语学习学生 与我使用用户名和密码登录时在应用程序中想要的相同 它将显示“我的姓名”和“我教的科目”,以及仅想学习英语科目的学生名单, 那么该怎么做呢? 我希望你明白我想说的话。

这是我到目前为止完成的代码

  

DBHELPER.JAVA

in Faculty table:-
user_id
user_name
user_subject

in Student Table:-
student_id
student_name
student_subject
  

FacultyActivity.Java(我必须在其中显示学生列表)

private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "AttendanceManager.db";

// User table name
private static final String TABLE_USER = "user";

// User Table Columns names
public static final String COLUMN_USER_ID = "user_id";
public static final String COLUMN_USER_NAME = "user_name";
public static final String COLUMN_USER_SUBJECT = "user_subject";
private static final String COLUMN_USER_EMAIL = "user_email";
private static final String COLUMN_USER_PASSWORD = "user_password";


// create table sql query
private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
        + COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + COLUMN_USER_NAME + " TEXT,"
        + COLUMN_USER_SUBJECT + " TEXT,"
        + COLUMN_USER_EMAIL + " TEXT,"
        + COLUMN_USER_PASSWORD + " TEXT"
        + ")";

// drop table sql query
private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER;


// User table name
private static final String TABLE_STUDENT = "student";

// User Table Columns names
private static final String COLUMN_STUDENT_ID = "student_id";
private static final String COLUMN_STUDENT_NAME = "student_name";
private static final String COLUMN_STUDENT_SUBJECT = "student_subject";
private static final String COLUMN_STUDENT_EMAIL = "student_email";
private static final String COLUMN_STUDENT_PASSWORD = "student_password";


// create table sql query
private String CREATE_STUDENT_TABLE = "CREATE TABLE " + TABLE_STUDENT + "("
        + COLUMN_STUDENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + COLUMN_STUDENT_NAME + " TEXT,"
        + COLUMN_STUDENT_SUBJECT + " TEXT,"
        + COLUMN_STUDENT_EMAIL + " TEXT,"
        + COLUMN_STUDENT_PASSWORD + " TEXT"
        + ")";
// drop table sql query
private String DROP_STUDENT_TABLE = "DROP TABLE IF EXISTS " + TABLE_STUDENT;

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(CREATE_USER_TABLE);
    sqLiteDatabase.execSQL(CREATE_STUDENT_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL(DROP_USER_TABLE);
    sqLiteDatabase.execSQL(DROP_STUDENT_TABLE);
    onCreate(sqLiteDatabase);
}
    public List<Student> getStdsubjectss() {


    List<Student> userList = new ArrayList<Student>();

    String rawQuery = " SELECT " + COLUMN_STUDENT_ID + " , "
            + COLUMN_STUDENT_NAME + " , " + COLUMN_STUDENT_SUBJECT + " , "
            + COLUMN_STUDENT_EMAIL + " , " + COLUMN_USER_SUBJECT + " FROM "
            + TABLE_STUDENT + " std, " + TABLE_USER + " fact WHERE std."
            + COLUMN_STUDENT_SUBJECT + " = fact." + COLUMN_USER_SUBJECT;

    String sql = "SELECT student.student_id,student.student_name,student.student_subject,student.student_email,user.user_subject FROM student, " +
            "user WHERE student.student_subject = '**What should i Add here to make it work??**' " ;

    Log.e("QUERY", sql);
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery(sql, null);

    if (cursor.moveToFirst()) {
        do {
            Student student = new Student();
            student.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_STUDENT_ID))));
            student.setName(cursor.getString(cursor.getColumnIndex(COLUMN_STUDENT_NAME)));
            student.setSubject(cursor.getString(cursor.getColumnIndex(COLUMN_STUDENT_SUBJECT)));
            student.setEmail(cursor.getString(cursor.getColumnIndex(COLUMN_STUDENT_EMAIL)));
            Faculty department = new Faculty();
            department.setSubject(cursor.getString(cursor.getColumnIndex(COLUMN_USER_SUBJECT)));
            userList.add(student);
        } while (cursor.moveToNext());

    }
    cursor.close();
    db.close();

    return userList;

}

我已经在sqli字符串中标记了应该在其中添加什么以使代码具有动态性, 如果我只是放置

AppCompatTextView facterName,facterSubj;

RecyclerView recyclerViewStudents;
List<Student> studentList;
StudentRecyclerAdapter studentRecyclerAdapter;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
Student student;
Faculty faculty;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_faculty);

    facterName = findViewById(R.id.factersNamer);
    facterSubj = findViewById(R.id.factersSubj);
    recyclerViewStudents = findViewById(R.id.recyclerViewStudents);
    Intent intent = getIntent();
    String name = intent.getStringExtra("username");
     databaseHelper = new DatabaseHelper(getApplicationContext());

     studentList = new ArrayList<>();
    studentRecyclerAdapter = new StudentRecyclerAdapter(studentList);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerViewStudents.setLayoutManager(mLayoutManager);
    recyclerViewStudents.setItemAnimator(new DefaultItemAnimator());
    recyclerViewStudents.setHasFixedSize(true);
    recyclerViewStudents.setAdapter(studentRecyclerAdapter);
    databaseHelper = new DatabaseHelper(this);
    getDataFromSQLite();

}

@SuppressLint("StaticFieldLeak")
private void getDataFromSQLite() {
    // AsyncTask is used that SQLite operation not blocks the UI Thread.
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            studentList.clear();
            studentList.addAll(databaseHelper.getStdsubjectss());
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            studentRecyclerAdapter.notifyDataSetChanged();
        }
    }.execute();

}

我显示所有具有英语科目的学生 但显示两次时间

 String sql = "SELECT student.student_id,student.student_name,student.student_subject,student.student_email,user.user_subject FROM student, " +
        "user WHERE student.student_subject = 'English' " ;

这是我面临的问题 但是现在我的主要问题是制作动态sqlite。

帮帮我 谢谢

1 个答案:

答案 0 :(得分:0)

使用LEFT JOIN连接两个表的行。

有关JOIN的更多信息,请参见下面的链接: LEFT JOIN

在您的情况下:

String sql = "SELECT student.student_id,student.student_name,student.student_subject,student.student_email,user.user_subject FROM student, " +
        "LEFT JOIN user ON student.student_subject = user.user_subject WHERE student.student_subject = 'English'  " ;