Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple where conditions #17

Open
astroanu opened this issue Jun 25, 2014 · 23 comments
Open

multiple where conditions #17

astroanu opened this issue Jun 25, 2014 · 23 comments
Labels

Comments

@astroanu
Copy link

Is it possible to make multiple where conditions?

var q = db.from('people').where('age', '=', 25);

i want to get people who are age 25 and who live in state = ny

@yathit
Copy link
Owner

yathit commented Jun 26, 2014

Definitely.checkout out http://dev.yathit.com/ydn-db/nosql-query.html

Calling where multiple times should works also, although implementation is unfinish.

@astroanu
Copy link
Author

Thanks. i didn't dig the entire code. if the multiple where is possible by passing an array it would be cool? (may be this is a bad idea)

var q = db.from('people').where([['age', '=', 25],['state','=','ny']]);

@yathit
Copy link
Owner

yathit commented Jun 26, 2014

Can you reasonable argue why it is better than where('age', '=', 25).where('state','=','ny') ?

@paolooo
Copy link

paolooo commented Jul 26, 2014

+1

@yathit
Copy link
Owner

yathit commented Aug 22, 2014

Which file are you using? You need cur module.

On Fri, Aug 22, 2014 at 7:40 PM, Erez Pilosof [email protected]
wrote:

ydn.db.IndexValueIterator is undefined... what am I missing ?


Reply to this email directly or view it on GitHub
#17 (comment).

@gen-failure
Copy link

I also have problem with multiple conditions. I am using ydn.db-isw-sql-e-cur-qry-de and the query I want to make is simply:

where condition1 = '1'
where condition2 != '0'
order by fieldA which is integer.

Iterate over all objects...

@yathit
Copy link
Owner

yathit commented Sep 9, 2014

Index base query is possible. But

That kind of complex query is work in progress. Checkout in /query folder.

@OopDeveloper89
Copy link

Hello yathit..
I am trying to realize multiple query too...
I downloaded your last release (1.0.3) and copied "ydn.db-dev.js" into my project.

Now I am trying to fetch all entries by two conditions. In Sql it would look like:
SELECT 'name' from 'mytable' WHERE id=5 AND age=15

How can I do this in ydn db?

I did it like this:
db
.from('mytable')
.where('id, age', '=', [5, 15])
.list()
.done(function(records) {
console.log(records);
});

@yathit
Copy link
Owner

yathit commented Sep 11, 2014

@OopDeveloper89 check out compound index or key joining as described in http://dev.yathit.com/ydn-db/nosql-query.html

@OopDeveloper89
Copy link

I used as workaround sql query. Is this a correct way?
Like:
// var db = new ydn.db.Storage('MyAppName', this.getShema(), { mechanisms: ['websql', 'indexeddb']});
var sql = Select * FROM mytable where x = 4 and b = 5..
db.executeSql(sql).always(function(r) {});

@dpalou
Copy link

dpalou commented May 26, 2015

Hello there,

thank you for this library! In my case I'm struggling with multiple where conditions with OR operator. Is there any way to apply two where conditions with an OR operator without having to do two queries? I'm trying to do the following query:

WHERE (timestart >= X OR timeend >= X) AND timestart <= Y

If I chain .where functions they always use AND operator.

Thanks!

@yathit
Copy link
Owner

yathit commented May 26, 2015

Multiple equal-AND over multiple OR query is possible.

Multiple keyrange-AND is not possible. You will have to use on memory filter for the rest of range query.

@bennycode
Copy link

Hello @yathit,

I also want to combine multiple where clauses but https://dev.yathit.com/ydn-db/nosql-query.html seems to be unavailable.

Do I need to add an index for all my field names which are used in the where clauses?

I always get the error: from(...).where(...).where(...).order is not a function

@yathit
Copy link
Owner

yathit commented Jan 31, 2016

Yes, all field in where clause mutual be indexed.

It was old url. All pages are in documation section on the site.
On Feb 1, 2016 6:51 AM, "Benny Neugebauer" [email protected] wrote:

Hello @yathit https://github.com/yathit,

I also want to combine multiple where clauses but
https://dev.yathit.com/ydn-db/nosql-query.html seems to be unavailable.

Do I need to add an index for all my field names which are used in the
where clauses?


Reply to this email directly or view it on GitHub
#17 (comment).

@bennycode
Copy link

Thanks for your fast reply.

I created indexes for name, age and name, age but I still get the error: db.from(...).where(...).where(...).list.

Do you see what I am doing wrong?

var schema = {
  stores: [
    {
      name: 'users',
      autoIncrement: true,
      indexes: [
        {
          name: 'name',
          keyPath: 'name'
        },
        {
          name: 'age',
          keyPath: 'age'
        },
        {
          name: 'name, age',
          keyPath: ['name', 'age']
        }
      ]
    }
  ]
};

var db = new ydn.db.Storage('my-db', schema, {mechanisms: ['indexeddb']});

db.clear();

var values = [
  {"name": "Lara", "age": 45, "registered": "2015-12-21T10:14:59.661Z"}
  , {"name": "Sarah", "age": 25, "registered": "2016-01-12T09:00:32.736Z"}
  , {"name": "Benny", "age": 28, "registered": "2015-12-21T09:55:17.201Z"}
  , {"name": "John", "age": 39, "registered": "2015-12-18T11:14:50.426Z"}
  , {"name": "Benny", "age": 30, "registered": "2014-12-18T11:14:50.426Z"}
];

for (var i = 0; i < values.length; i++) {
  db.put('users', values[i]);
}

var query =
  db
  .from('users')
  .where('name', '=', 'Benny')
  .where('age', '<', 30)
  .list()
  .done(function (records) {
    records.forEach(function (record) {
      console.log('Result', record);
    });
  });

@bennycode
Copy link

Good news!

I solved the db.from(...).where(...).where(...).listissue by using a ydn.db custom build which has the cur module. But now my db query does not give me any result but it is expected to find 1 record. 😢

@yathit
Copy link
Owner

yathit commented Feb 2, 2016

That function is not finished yet. The multiple where clause should generate query as follow:

query = db.from('users').where('name, age', '>', ['Benny', 30'], '<', ['Benny', '\uffff']);

@bennycode
Copy link

Thanks for your answer. I changed the following:

.where('name', '=', 'Benny')
.where('age', '<', 30)

->

.where('name, age', '<', ['Benny', 30])

These changes cause now this error: Uncaught ConstraintError: Require index "name, age" not found in store "users".

But I have there is an index name, age which is defined as:

{
  name: 'name, age',
  keyPath: ['name', 'age']
}

Also... Shouldn't there be two operators for the multiple where clause? Because I need = and <.

@yathit
Copy link
Owner

yathit commented Feb 2, 2016

All look good. Please post to jsbin.

@bennycode
Copy link

Thanks for helping me!

I made a Fiddle: https://jsfiddle.net/bennyn/ynchm1tw/
If you click on "Run" you can see the output in your Browser's JavaScript console.

Your library is added under External Resources.

@yathit
Copy link
Owner

yathit commented Feb 2, 2016

Definitely a bug.

currently use https://jsfiddle.net/ynchm1tw/2/

yathit added a commit that referenced this issue Feb 2, 2016
@bennycode
Copy link

Thanks for helping out!

This works:

var iterator =
  ydn.db.IndexValueIterator
  .where('users', 'name, age', '>', ['Benny'], '<', ['Benny', 30]);

var query =
  db
  .values(iterator)
  .done(function (records) {
    records.forEach(function (record) {
      console.log('Result', record);
    });
  });

@yathit yathit added the bug label Feb 2, 2016
@yathit
Copy link
Owner

yathit commented Feb 2, 2016

As a note, none of my application use query api and the bug fix will be slow.

yathit added a commit that referenced this issue Feb 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants