2

Is it possible to query a websql database using both AND and OR in the same statement?

This will not work:

tx.executeSql('SELECT * FROM people 
  WHERE name="'+name1+'" OR name="'+name2+'" 
  AND category=1', [], function (tx, results) {

Both these do:

tx.executeSql('SELECT * FROM people 
 WHERE name="'+name1+'" OR name="'+name2+'", 
 [], function (tx, results) {

tx.executeSql('SELECT * FROM people 
 WHERE name="'+name1+'" 
 AND category=1', [], function (tx, results) {

Is it not possible to achieve this or is my syntax / query incorrect?

Edper
  • 9,144
  • 1
  • 27
  • 46
mao
  • 1,059
  • 2
  • 23
  • 43
  • 1
    What does the error say by the way? – Edper May 16 '14 at 02:43
  • @Edper I don't get one it just ignores the AND part of the query at the end. Using chrome developer tools / error console, is there anywhere else to check for errors? – mao May 16 '14 at 02:52

1 Answers1

1

Turns out I should have experimented more before posting, adding brackets around the OR part of the query allowed it to work:

tx.executeSql('SELECT * FROM people 
WHERE (name="'+name1+'" OR name="'+name2+'") 
AND category=1', [], function (tx, results) {
mao
  • 1,059
  • 2
  • 23
  • 43
  • Based on your solution there was no error probably in the first place. It just that it has no resulting records. – Edper May 16 '14 at 03:20
  • There were records to match all 3 queries in the original post, the broken query just didn't work as expected - records were found where the 2 names matched and the category was ignored. Adding the brackets has resolved it. The query itself ran fine in the SQLite addon for firefox without the brackets. – mao May 16 '14 at 03:31
  • Ok, thanks for the explanation @mao. But going to back to your solution without the bracket `name2` would be associated by AND `category=1` whereas using the bracket you will have the right `OR`, namely, `name=name1 OR name=name2` instead of `name=name1 OR (name=name2 AND category=1)` (brackets are mine). – Edper May 16 '14 at 03:52