Android SQLite查询返回多个相同的列

时间:2015-07-11 02:34:44

标签: android sqlite join

以下Android Sqlite中的查询返回多个相同的列。例如,它返回“meet_id”(3)次。第一个(2)“meet_id”使用正确的数字是正确的,而第三个是空的。我的“athlete_id”和“event_id”也是如此。我假设这是因为我的左连接因为第3个表现在是空的但是如果只为每个列属性返回一列(1个满足列和更多行?):

Cursor cursorTemp = db.rawQuery("SELECT * FROM " + TABLE_MEETS + " LEFT JOIN " + TABLE_MEETS_ATHLETES +
                " ON " + TABLE_MEETS + "." + COL_MEET_ID + " = " + TABLE_MEETS_ATHLETES + "." + COL_MEET_ID +
                " LEFT JOIN " + TABLE_MEETS_ATHLETES_SPLITS + " ON " + TABLE_MEETS_ATHLETES + "." + COL_MEET_ID +
                " = " + TABLE_MEETS_ATHLETES_SPLITS + "." + COL_MEET_ID + " AND " + TABLE_MEETS_ATHLETES +
                "." + COL_ATHLETE_ID + " = " + TABLE_MEETS_ATHLETES_SPLITS + "." + COL_ATHLETE_ID + " AND " +
                TABLE_MEETS_ATHLETES + "." + COL_EVENT_ID + " = " + TABLE_MEETS_ATHLETES_SPLITS + "." +
                COL_EVENT_ID + " WHERE " + TABLE_MEETS + "." + COL_MEET_ID + " = ?", new String[]{String.valueOf(i)});

我知道这有点乱,但我更喜欢原始查询

此外,meet id是第一个表中的主键,第二个和第三个表中是复合

2 个答案:

答案 0 :(得分:3)

SELECT * FROM...指令中,*符号将所有列包含在结果中。由于多个连接的结果,重复了一些列。我的建议是用您感兴趣的列名替换*

答案 1 :(得分:0)

看来罗伯托是对的。我已经在DB2中嘲笑了这个,只要我能理解你在尝试什么

create table TABLE_MEETS (COL_MEET_ID int);

insert into table_meets (COL_MEET_ID) values (1),(2),(3);

create table TABLE_MEETS_ATHLETES (
COL_MEET_ID int,
COL_ATHLETE_ID int,
COL_EVENT_ID int);

insert into TABLE_MEETS_ATHLETES (COL_MEET_ID,COL_ATHLETE_ID,COL_EVENT_ID) values 
(1,10,100),
(2,10,101),
(1,20,100),
(2,20,101);

create table TABLE_MEETS_ATHLETES_SPLITS (
COL_MEET_ID int,
COL_ATHLETE_ID int,
COL_EVENT_ID int);

insert into TABLE_MEETS_ATHLETES_SPLITS (COL_MEET_ID,COL_ATHLETE_ID,COL_EVENT_ID) values 
(1,10,100),
(2,10,101),
(1,20,100),
(2,20,101);

SELECT *将列出所有表中的列。

针对DB2运行:

SELECT * FROM TABLE_MEETS LEFT JOIN TABLE_MEETS_ATHLETES ON TABLE_MEETS.COL_MEET_ID=TABLE_MEETS_ATHLETES.COL_MEET_ID LEFT JOIN TABLE_MEETS_ATHLETES_SPLITS ON TABLE_MEETS_ATHLETES.COL_MEET_ID=TABLE_MEETS_ATHLETES_SPLITS.COL_MEET_ID AND TABLE_MEETS_ATHLETES.COL_ATHLETE_ID=TABLE_MEETS_ATHLETES_SPLITS.COL_ATHLETE_ID AND TABLE_MEETS_ATHLETES.COL_EVENT_ID=TABLE_MEETS_ATHLETES_SPLITS.COL_EVENT_ID WHERE TABLE_MEETS.COL_MEET_ID=1

COL_MEET_ID COL_MEET_ID COL_ATHLETE_ID COL_EVENT_ID COL_MEET_ID COL_ATHLETE_ID COL_EVENT_ID

      1           1             10          100           1             10          100
      1           1             20          100           1             20          100

选择了2条记录。

但您可能只想报告一个表中的给定列。如果你真的期待NULLS,你需要考虑使用哪一个。

SELECT MEETS.COL_MEET_ID,ATHLETES.COL_ATHLETE_ID,ATHLETES.COL_EVENT_ID 
FROM TABLE_MEETS MEETS LEFT JOIN TABLE_MEETS_ATHLETES ATHLETES 
ON MEETS.COL_MEET_ID=ATHLETES.COL_MEET_ID 
LEFT JOIN TABLE_MEETS_ATHLETES_SPLITS SPLITS 
ON ATHLETES.COL_MEET_ID=SPLITS.COL_MEET_ID AND
ATHLETES.COL_ATHLETE_ID=SPLITS.COL_ATHLETE_ID AND 
ATHLETES.COL_EVENT_ID=SPLITS.COL_EVENT_ID WHERE MEETS.COL_MEET_ID=1

COL_MEET_ID COL_ATHLETE_ID COL_EVENT_ID

      1             10          100
      1             20          100

选择了2条记录。