Query a row of data against previous row of data in MySQL an

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
suprsnipes
Forum Newbie
Posts: 3
Joined: Mon May 25, 2009 11:26 pm

Query a row of data against previous row of data in MySQL an

Post by suprsnipes »

Hi,

I've only learned about MySQL and PHP this week and have a basic grasp of running an SQL query after reading some tutorials.

I have data in a MySQL database and I want to classify each row of data based on meeting certain criteria and to do this I need to be able to refer to the previous rows data. How would I go about doing this?

Thank you in advance,
suprsnipes
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Query a row of data against previous row of data in MySQL an

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
suprsnipes
Forum Newbie
Posts: 3
Joined: Mon May 25, 2009 11:26 pm

Re: Query a row of data against previous row of data in MySQL an

Post by suprsnipes »

I have placed an attachment asking for a little help with putting this query together.

ID is assigned auto-increment, the other columns are Type, Price and Size.

Please forgive my explanation but I was previously calculating this in Excel and need to move to MySQL database. I want to do the following;

If Type = 'S' I want to compare it to the previous rows data and insert a new column in the existing database, let's call the new column 'nc';

If the previous rows type = b & the price of the current column is equal to or below then insert in 'nc' the word 'sell'
If the previous rows type = b & the price of the current column is above then insert in 'nc' the word 'buy'
If the previous rows type = a & the price of the current column is equal to or above then insert in 'nc' the word 'buy'
If the previous rows type = a & the price of the current column is below then insert in 'nc' the word 'sell'

Once I get a kick start I'll be ok. I really appreciate the help.
Attachments
sample (3).png
sample (3).png (5.21 KiB) Viewed 1474 times
Post Reply