This query below in Android Sqlite is returning multiple of the same columns. For example, it is returning "meet_id" (3) times. The first (2) "meet_id" are correct with the right number while the third one is empty. The same happens for my "athlete_id" and "event_id". I am assuming this is because of my left join since the 3rd table is empty as of now but shouldn't if just return one column for each column attribute (1 meet column and more rows?):
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)});
I understand this is a bit messy but I like raw queries more
Also, meet id is a primary key in the first table and composite in 2nd and 3rd
In the instruction SELECT * FROM...
, the *
symbol includes ALL the columns into result. As resulting of multiple joins, some columns are repeated. My suggestion is replacing the *
by the columns names of your interest.
It seems Roberto is right. I mocked this up in DB2 as far as I can understand what you are trying
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 * will list the columns from all the tables.
Running against 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
1 1 10 100 1 10 100
1 1 20 100 1 20 100
2 record(s) selected.
but you probabaly only want to report a given column from one table. If you really expect NULLS, you will need to think about which one to use.
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
1 10 100
1 20 100
2 record(s) selected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.