Android:SQLITE选择查询

时间:2017-12-29 17:40:31

标签: java android sqlite

我有一个student_fee表,其中列是:

  • id_fee
  • id_student
  • student_name
  • fee_month

我在这里根据学生的每月学费支付插入数据。 从这个方面我可以轻松地查询哪些学生支付了他们的月费,假设1月费用由查询赞:Select * From student_fee Where fee_month = "January";

但是,我怎样才能搜索哪些学生仅在1月份才支付月费?

1 个答案:

答案 0 :(得分:0)

通常你应该有规范化的数据库表,以避免一遍又一遍地存储学生名称:

<!-- sql -->
CREATE TABLE students    ( id  INTEGER,  name  varchar(60));

INSERT INTO students    ( id ,  name ) VALUES    (1, 'stud_1');
INSERT INTO students    ( id ,  name ) VALUES    (2, 'stud_2');
INSERT INTO students    ( id ,  name ) VALUES    (3, 'stud_3');
INSERT INTO students    ( id ,  name ) VALUES    (4, 'stud_4');
INSERT INTO students    ( id ,  name ) VALUES    (5, 'stud_5');

CREATE TABLE student_fee  (id INTEGER, studentId INTEGER, month varchar(20));

INSERT INTO student_fee    (id ,  studentId ,  month ) VALUES    (1, 1, 'january');
INSERT INTO student_fee    (id ,  studentId ,  month ) VALUES    (2, 2, 'january'); 
INSERT INTO student_fee    (id ,  studentId ,  month ) VALUES    (4, 2, 'february');
INSERT INTO student_fee    (id ,  studentId ,  month ) VALUES    (5, 3, 'february');
INSERT INTO student_fee    (id ,  studentId ,  month ) VALUES    (6, 4, 'february');

你会这样查询:

select * 
from  students  
where not exists (
    select 1 
    from student_fee
    where students.id = studentid 
    and month = 'january')

输出:

id  name
3   stud_3
4   stud_4
5   stud_5

(Sql语法是mysql,不确定sqlite)