Solved

[PHP][MYSQL]Get highest value

Posted on 2009-05-04
3
538 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now