Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://315398]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-07-20 16:51 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (438 votes). Check out past polls.