Solved

[PHP][MYSQL]Get highest value

Posted on 2009-05-04
3
546 Views
Last Modified: 2012-05-06
In PHP I have a table with rows. The rows are currently within a SQL database and are stored by a value call position in which the will be loaded with ORDER BY ASC(position).
What I am having trouble doing is finding the row with the highest value of position WHERE parentseries='$parent' AND category='$category'

I need to do this when I am  adding a new variable. In a sort it's an auto increment of my own. But I cannot use normal auto Increment as this table is shared with other product tables.

example of table:
parentseries    |    category    |    data    |    position
1721                  14                      ...            1
1721                  14                      ...            2
1532                  14                      ...            1
1532                  14                      ...            2

What now want to happen is if I add another category 14 item that it will find all other category 14 items that are parentseries 1721 and find which one has the highest position integer.

Thank you.
0
Comment
Question by:drakeshe
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
jausions earned 500 total points
ID: 24300490
SELECT MAX(position) FROM myTable WHERE parentseries='$parent' AND category='$category'

You also want to make sure you're using a database engine that support transactions (InnoDB for instance) otherwise there's a slight chance you could run the same query at exactly the same moment and then insert the same "position" twice for different record. That of course would depend on the traffic on your database and how often you do this kind of INSERT
0
 
LVL 5

Author Closing Comment

by:drakeshe
ID: 31577818
Thank you! This worked perfectly! Exactly what I needed
0
 
LVL 5

Author Comment

by:drakeshe
ID: 24300537
Thank you! It worked perfectly! Exactly what I needed
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question