Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: The fine art of database programming

by pdcawley (Hermit)
on Apr 30, 2002 at 12:47 UTC ( #163042=note: print w/ replies, xml ) Need Help??


in reply to Re: The fine art of database programming
in thread The fine art of database programming

And there was me thinking it was:

SELECT * FROM table LIMIT 99, 21;
No WHERE clause in sight. If you want be be a bit more specific on how things are ordered (rather than insertion order (which I believe is defined as being the standard ordering)), you do
SELECT * FROM table ORDER BY col LIMIT 99, 21;
Still no WHERE clause.


Comment on Re: Re: The fine art of database programming
Select or Download Code
is that standard though?
by cebrown (Pilgrim) on Apr 30, 2002 at 14:04 UTC
    I believe that LIMIT is a DBMS-specific extension. For example...
    in MS SQL Server:
    SELECT TOP 10 col FROM table
    in IBM DB2:
    SELECT col FROM table FETCH FIRST 10 ROWS ONLY
    I think the only platform independent way to do this is:
    SELECT col FROM table A WHERE 10 > (SELECT COUNT(*) FROM table B WHERE A.col < B.col) ORDER BY col DESC
(OT) Re: The fine art of database programming
by gmax (Abbot) on Apr 30, 2002 at 14:38 UTC
    I am sorry, pdcawley, but I have to disagree.
    One of the fundaments of the relational model is that you don't make assumptions on the physical storage of the records. What you are suggesting is quite dangerous, as the following example shows.
    mysql> describe test_order; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | | PRI | 0 | | | name | char(10) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> select * from test_order; #all the records +----+------+ | id | name | +----+------+ | 1 | aaaa | | 2 | bbbb | | 3 | cccc | | 4 | dddd | | 5 | eeee | | 6 | ffff | | 7 | gggg | | 8 | hhhh | | 9 | iiii | | 10 | jjjj | +----+------+ 10 rows in set (0.01 sec) mysql> select * from test_order limit 4,3; #1st request. OK so far +----+------+ | id | name | +----+------+ | 5 | eeee | | 6 | ffff | | 7 | gggg | +----+------+ 3 rows in set (0.00 sec) # now we remove one of the records we were selecting through LIMIT mysql> delete from test_order where id = 5; Query OK, 1 row affected (0.00 sec) # and we insert another record mysql> insert into test_order values (11, "kkkk"); Query OK, 1 row affected (0.00 sec) # Then we insert the deleted record again mysql> insert into test_order values (5, "eeee"); Query OK, 1 row affected (0.00 sec) mysql> select * from test_order limit 4,3; +----+------+ | id | name | +----+------+ | 11 | kkkk | | 6 | ffff | | 7 | gggg | +----+------+ 3 rows in set (0.00 sec)
    The same query, after a couple of delete/insert statements, gives you different results.
    It is true that you can correct the result using the ORDER BY clause, but you are not dealing with "record numbers".
    The LIMIT clause, for those database that support it, guarantees that N records from the result dataset, starting at a given position are returned. There is no assumption about their position in the table.
    If you want to return correct records, use the WHERE clause.
     _  _ _  _  
    (_|| | |(_|><
     _|   
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://163042]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-07-31 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls