casper
casper

Reputation: 43

How to write following Select in sqlite

I wanted get id of something whose barcode is either empty or barcode state is active. Barcode state is stored in another table. Here is what I tried

SELECT a.id from a 
       where a.bar='' 
          OR a.bar=(SELECT b.barcode from b where b.barcode=active)

But it gives me nothing when there are some results should come. Where did I make mistake?

Thanks in advance

Upvotes: 1

Views: 60

Answers (3)

valex
valex

Reputation: 24144

I guess your subquery returns more than one record so in this case you have to use IN instead of =. Also I guess b.barcode is a varchar field so you should use a varchar constant 'active'

SELECT a.id from a 
       where a.bar='' 
          OR a.bar IN (SELECT b.barcode from b where b.barcode='active')

Upvotes: 0

Jimmy
Jimmy

Reputation: 553

SELECT a.id FROM a 
       WHERE a.bar=''  
          OR a.bar IN (SELECT b.barcode FROM b WHERE b.barcode='active')

Upvotes: 1

Seenu69
Seenu69

Reputation: 1054

you can use raw query:

here is the code for it

Cursor getData(String a){

     return db.rawQuery(SELECT a.id from a where a.bar='' OR a.bar=(SELECT b.barcode from b where b.barcode=active));

}

Upvotes: 0

Related Questions