Problem Solution Discussion See Also

unit = seq - 1 12 + 1 box = int seq - 1 12 6 + 1 case = int seq - 16 12 + 1 The t able shown below illust rat es t he relat ionship bet ween som e sam ple sequence num bers and t he corresponding case, box, and unit num bers: seq case box unit 1 1 1 1 12 1 1 12 13 1 2 1 72 1 6 12 73 2 1 1 144 2 6 12

11.19 Numbering Query Output Rows Sequentially

11.19.1 Problem

You want t o num ber query out put rows.

11.19.2 Solution

I f youre writ ing your own program , j ust add t he row num ber yourself.

11.19.3 Discussion

A t ype of sequence t hat has not hing t o do wit h t he cont ent s of your dat abase is t o num ber out put rows from a query. When working wit hin an API , you can num ber t he rows by m aint aining a count er and displaying it s current value wit h each rows cont ent s. Here is an exam ple in Pyt hon, using t he insects t able. I t sim ply displays a num bered list of t he dist inct values in t he origin colum n of t he t able: cursor = conn.cursor cursor.execute SELECT DISTINCT origin FROM insect count = 1 while 1: row = cursor.fetchone if row == None: break print count, row[0] count = count + 1 cursor.close

11.19.4 See Also

The m ysql program provides no explicit row- num bering facilit ies, alt hough you can use a SQL variable t o include an ext ra row num ber colum n in a querys out put . Anot her way t o produce result s t hat m ay be suit able for your purposes is t o filt er m ysql out put t hrough anot her program t hat adds row num bers. These t echniques are described in Recipe 1.27 .

Chapter 12. Using Multiple Tables

Sect ion 12.1. I nt roduct ion Sect ion 12.2. Com bining Rows in One Table wit h Rows in Anot her Sect ion 12.3. Perform ing a Join Bet ween Tables in Different Dat abases Sect ion 12.4. Referring t o Join Out put Colum n Nam es in Program s Sect ion 12.5. Finding Rows in One Table That Mat ch Rows in Anot her Sect ion 12.6. Finding Rows wit h No Mat ch in Anot her Table Sect ion 12.7. Finding Rows Cont aining Per- Group Minim um or Maxim um Values Sect ion 12.8. Com put ing Team St andings Sect ion 12.9. Producing Mast er- Det ail List s and Sum m aries Sect ion 12.10. Using a Join t o Fill in Holes in a List Sect ion 12.11. Enum erat ing a Many- t o- Many Relat ionship Sect ion 12.12. Com paring a Table t o I t self Sect ion 12.13. Calculat ing Differences Bet ween Successive Rows Sect ion 12.14. Finding Cum ulat ive Sum s and Running Averages Sect ion 12.15. Using a Join t o Cont rol Query Out put Order Sect ion 12.16. Convert ing Subselect s t o Join Operat ions Sect ion 12.17. Select ing Records in Parallel from Mult iple Tables Sect ion 12.18. I nsert ing Records in One Table That I nclude Values from Anot her Sect ion 12.19. Updat ing One Table Based on Values in Anot her Sect ion 12.20. Using a Join t o Creat e a Lookup Table from Descript ive Labels Sect ion 12.21. Delet ing Relat ed Rows in Mult iple Tables