Hi,

I'm stumped and I need help pronto! I want to select all rows EXCEPT the FIRSt row from a MySQL table. My primary key is a unique id (which I auto increment).

I did a subquery but it didnt work. I use something like this :

$eventsquery = "SELECT * FROM idb_events WHERE events_id < (SELECT events_id FROM idb_events LIMIT 1 ) ORDER by events_id DESC";

but it returns an error.

I'm doing a listing for some events. The display(layout) for the latest event is different from other events (for highlight purposes) and I can only think of excluding the first row from the others.

Can somebody out there help me?

All help greatly appreciated. Thanks.

    Might be a way to exclude it in the select. But you'll be reading the rows in a php "while" loop, skipping the row in the first iteration is probably easier.

      Why not just do a normal SELECT query and then in your code call fetch_array() once to move the pointer and ignore the first row?

      Diego

        Thanks for the suggestion but could you please show me an example so that I get a clearer idea.

        Thanks again.

          Well, I'm not sure what your query, should be, but I'm guessting something like:

          $eventsquery = "SELECT events_id FROM idb_events";

          $result = mysql_query($eventsquery);

          mysql_fetch_array($result); // move the pointer past the first row

          while ($row = mysql_fetch_array($result)) {
          // do your stuff here, first row is ignored because the pointer is passed that
          }

          Diego

            Thanks a load to Diego Huang who has saved my day!

            Cheers mate!

            • hasni -

              Just to let you know...you can't do subqueries in MySQL..that is Oracle, MS SQL2000 etc...MySQL isnt advanced though but does allow for a good database utility for free which I like 🙂

                Dear.
                i think the easiast way is to make like some thing like:

                select *from DB where 'id'>1

                then it'll select all the rows without the first one ..

                  select *from DB where 'id'>1

                  What if the first row has an id of 2? assuming it's an auto_increment primary key field, id numbers will not be repeated/replaced. Therefore, if id #1 was deleted, then id #2 would be the first row, and would still be returned by this query. 🙂

                  Diego's suggestion is definitely the way i'd go, call mysql_fetch_array once before entering the loop, to put the pointer beyond the first row.

                  -Jim Keller
                  http://www.centerfuse.net

                    How about:

                    SELECT * FROM table WHERE id > MIN(id)

                      Write a Reply...