Query BD and show all results except the first

Asked

Viewed 425 times

3

I have the table tbl_galeria, what happens is that I want to make a consultation, but the consultation cannot show the first result. That is, the query will return all values except the one with the smallest id.

$query = mysql_query("SELECT * FROM tbl_galeira ORDER BY id ASC");
  • 2

    By the way try using the function mysqli_ since the mysql_ will be discontinued.

  • Okay, thanks for the tip!

4 answers

5


You can do with the MIN mysql:

$query = mysql_query("SELECT * FROM tbl_galeira WHERE id > (SELECT MIN(id) FROM tbl_galeira) ORDER BY id ASC");

4

Simple and direct solution without subquery:

$query = mysql_query( 'SELECT * FROM tbl_galeira ORDER BY id LIMIT 1,999999999' ).

The second parameter of LIMIT is absurdly larger than its intended application. Remember that 0 is the first record, so we use 1 to catch from the second on.

Syntax may seem strange, but it’s more effective than engine compare Ids line by line (plus more readable).

  • I thought of exactly the same solution, but I get a bit apprehensive about the maximum value being a value, not a constant like MAX_VAL.

  • @Jorgeb. I did a search before answering, each engine has a different way of working, and I didn’t really find any variables with this function. Anyway, probably if a user needs a realistic query with more than 999999999 return records at once, they should have problems in some other part of the application (remembering that the number is an example and can be increased). Your remark really is valid, however.

2

You can do a sub-discharge:

SELECT * 
FROM tbl_galeira, 
    (SELECT id as fId FROM tbl_galeira ORDER BY id ASC limit 1) f 
WHERE id > f.fId

0

It is interesting to know how many records you want to return. Let’s say 10, you would do the following:

$query = mysql_query("SELECT * FROM tbl_galeira ORDER BY id ASC LIMIT 10, 1");
  • 1

    Unlike... " LIMIT 1, 10" rsrsrsr

  • yes, but I’ll never know for sure the limit of records. thanks anyway!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.