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
Query a row of data against previous row of data in MySQL an
Moderator: General Moderators
-
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
You can use subqueries or in some cases - user defined variables.
http://dev.mysql.com/doc/refman/5.1/en/subqueries.html
http://dev.mysql.com/doc/refman/5.0/en/ ... ables.html
http://dev.mysql.com/doc/refman/5.1/en/subqueries.html
http://dev.mysql.com/doc/refman/5.0/en/ ... ables.html
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
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.
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 (5.21 KiB) Viewed 1476 times