mercredi 6 mai 2015

Get string from inner join query on two tables

I am looking to retrieve a random value from my SQLite table - specifically an 'answer description' value. I can get this to work absolutely fine:

public String getRandomAnswer()
{
    SQLiteDatabase db = this.getReadableDatabase();

Cursor c = db.query(TABLE_ANSWERS + " ORDER BY RANDOM() LIMIT 1",
        new String[] { ADESCR }, null, null, null, null, null, null);

if(c.moveToFirst())
  return 
          c.getString(c.getColumnIndex(ADESCR));
else 
  return "nothing";
}

But what I want to do now is retrieve the same value from two linked tables that have an inner join. This is the code I have tried (without the random order limitation):

public String getRandomQuestion1()
{
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor c = db.query("SELECT * FROM TABLE_ANSWERS ta INNER JOIN TABLE_QUESTION tq ON ta.AId=tq.ASId", 
    new String[] { ADESCR }, null, null, null, null, null, null);

    if(c.moveToFirst())
    return c.getString(c.getColumnIndex(ADESCR));

    else 
        return "nothing";
      }

It suggests a syntax error - but I am unsure what the syntax error is. Am happy to show my tables - but wondering if there is just something a lot more fundamental that I have missed?

Aucun commentaire:

Enregistrer un commentaire