Page 1 of 1

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

Posted: Tue May 26, 2009 11:46 pm
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

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

Posted: Wed May 27, 2009 1:18 am
by VladSun

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

Posted: Wed May 27, 2009 2:31 am
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.