Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: Re: mysql auto_incremented id

by cchampion (Curate)
on Dec 17, 2003 at 22:08 UTC ( #315398=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: mysql auto_incremented id
in thread mysql auto_incremented id

How do you deal with first and last image then?

I insist on my belief that a primary key should be doing only one task, i.e. identifying the record uniquely. If you need a display id, you have two choices:

  • Adding a field to your table for display purposes, but then you would have two further problems.
    • filling the gaps when you delete a record.
    • Always displaying the records in the same order. No chance of diplaying with different criteria.
  • Creating a display number when you select the records. And you can do it either in (at least) two ways:
    • with a MySQL variable.
      SELECT @count := @count+1 as sequence, id, name, filename from mytable
    • In your Perl script.
      my $query = "SELECT id, name, filename from mytable"; my $sth=$dbh->prepare($query); $sth->execute(); my @results; my $count =0; while (my $row = $sth->fetchrow_arrayref) { push @results, [ $count++ , @$row ] }
      Now your @results have a counter that you can use for displaying purposes.
      Modifying your query with a different WHERE or ORDER BY will change the display order.


Comment on Re: Re: Re: mysql auto_incremented id
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2015-07-02 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (25 votes), past polls