Loading ...
Sorry, an error occurred while loading the content.

How to order the results of a query?

Expand Messages
  • michaelbluejay
    I know that the documentation purportedly explains how to do queries, but I find it exceptionally difficult to understand, and the lack of practical examples
    Message 1 of 4 , Aug 30, 2012
    • 0 Attachment
      I know that the documentation purportedly explains how to do queries, but I find it exceptionally difficult to understand, and the lack of practical examples doesn't help.

      So given this query:

      mydata = db(fieldValue).select("col1", "col2", "col3");

      How would I order the results by col2? I saw on the "Working with Data" page about db.order(), but I have no idea where it goes or what the syntax looks like when I'm trying to apply it to an existing query.

      Thank you very much for your help.
    • Jason Wright
      You could use something like: $queryResults=db().filter({col4:value}).order( col2 ); Would give an array of rows which match the query col4=value and sorts
      Message 2 of 4 , Aug 30, 2012
      • 0 Attachment
        You could use something like:

        $queryResults=db().filter({col4:value}).order("col2");

        Would give an array of rows which match the query "col4=value" and sorts by col2

        $queryResults[0]["col1"] will give the value of "col1" on the first row. (assuming that there are rows in the result)

        -Jason

        On Thu, Aug 30, 2012 at 6:52 PM, michaelbluejay <yh327@...> wrote:
         

        I know that the documentation purportedly explains how to do queries, but I find it exceptionally difficult to understand, and the lack of practical examples doesn't help.

        So given this query:

        mydata = db(fieldValue).select("col1", "col2", "col3");

        How would I order the results by col2? I saw on the "Working with Data" page about db.order(), but I have no idea where it goes or what the syntax looks like when I'm trying to apply it to an existing query.

        Thank you very much for your help.


      • michaelbluejay
        ... Thank you, though I can t figure out how to make your answer applicable to what I m trying to do. Again, here s a query, which works fine: mydata =
        Message 3 of 4 , Aug 30, 2012
        • 0 Attachment
          --- In taffydb@yahoogroups.com, Jason Wright <jasonwright365@...> wrote:
          >
          > You could use something like:
          >
          > $queryResults=db().filter({col4:value}).order("col2");
          >
          > Would give an array of rows which match the query "col4=value" and sorts by
          > col2
          >
          > $queryResults[0]["col1"] will give the value of "col1" on the first row.
          > (assuming that there are rows in the result)


          Thank you, though I can't figure out how to make your answer applicable to what I'm trying to do.

          Again, here's a query, which works fine:

          mydata = db({column:"value"}).select("col1", "col2", "col3");

          All I'm trying to do is to do the same query, but order it by "col2".

          Your example doesn't appear to select any rows. When I try it anyway, e.g.:

          mydata = db().filter({column:"value"}).order("col2");

          ...the result is some kind of object. It's not an array of data like I get with my original query.
        • Karl
          Do db(fieldValue).order( col2 ).select( col1 , col2 , col3 ) In reply to your response to Jason, the way to get an array back if you aren t using .select()
          Message 4 of 4 , Aug 31, 2012
          • 0 Attachment
            Do db(fieldValue).order("col2").select("col1", "col2", "col3")

            In reply to your response to Jason, the way to get an array back if you aren't using .select() is to just use .get(). This gives you all of the columns back.

            The other thing you could do is .each(). For example:

            db(fieldValue).order("col2").each(function(r) {
            // Process each record
            });

            This loops over the record set, with the variable 'r' holding the current record for the current iteration of the loop. To access the record you would just do r.col1. I too find the documentation lacking a lot, but the .each() function is something it does cover pretty well. But if you want an array back, select() or get() is what you'd want to use.

            Karl

            --- In taffydb@yahoogroups.com, "michaelbluejay" <yh327@...> wrote:
            >
            > I know that the documentation purportedly explains how to do queries, but I find it exceptionally difficult to understand, and the lack of practical examples doesn't help.
            >
            > So given this query:
            >
            > mydata = db(fieldValue).select("col1", "col2", "col3");
            >
            > How would I order the results by col2? I saw on the "Working with Data" page about db.order(), but I have no idea where it goes or what the syntax looks like when I'm trying to apply it to an existing query.
            >
            > Thank you very much for your help.
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.