| Author |
Topic |
|
d confz
Starting Member
12 Posts |
Posted - 2008-02-19 : 08:32:10
|
Hello.. Can I update only a single row on the database?For example I have the table Product:product_name ------- qty -------- priceMonggol ------------- 12 ---------- 7Paper --------------- 10 ---------- 22Monggol ------------- 89 ---------- 51The user inputs a productname, qty and price. Then I need to update a product's qty where product name is equal to the product name from the user.Using the code:UPDATE Product SET qty = '"+number+"' WHERE product_name = '"+prodname+"';(number and prodname is the value from the user)This will update the qty of all the product_name that is equal to the prodname. But what I need is to update only the first row. I cannot use the price because the price that is input by the user is different from the price in the database, it is for checking the earnings. Is it possible for me to do it? Thanks a lot..  |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-02-19 : 08:35:37
|
| Define what do you mean by first row. In relational world, terms first and last are meaningless unless you specify record position relative to some sort order.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2008-02-19 : 08:42:51
|
| Use the following , replacing 'Prod1' with your incoming variable CREATE TABLE Products(ID INT,Product_Name VARCHAR(20),Qty INT,Price DECIMAL(10,2))INSERT INTO Products SELECT 1,'Prod1',2,2.5UNIONSELECT 2,'Prod2',3,6.5UNIONSELECT 3,'Prod1',3,6.5UPDATE Products SET qty = 7 WHERE product_name = 'Prod1' AND ID IN (SELECT TOP 1 ID FROM Products as t2 WHERE t2.product_name = 'Prod1')SELECT * FROM ProductsDROP TABLE ProductsJack Vamvas--------------------Search IT jobs from multiple sources- http://www.ITjobfeed.com |
 |
|
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2008-02-19 : 08:43:31
|
| I forgot to mention, that I assumed your order would be based on some IDJack Vamvas--------------------Search IT jobs from multiple sources- http://www.ITjobfeed.com |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-19 : 08:45:30
|
quote: Originally posted by d confz Hello.. Can I update only a single row on the database?For example I have the table Product:product_name ------- qty -------- priceMonggol ------------- 12 ---------- 7Paper --------------- 10 ---------- 22Monggol ------------- 89 ---------- 51The user inputs a productname, qty and price. Then I need to update a product's qty where product name is equal to the product name from the user.Using the code:UPDATE Product SET qty = '"+number+"' WHERE product_name = '"+prodname+"';(number and prodname is the value from the user)This will update the qty of all the product_name that is equal to the prodname. But what I need is to update only the first row. I cannot use the price because the price that is input by the user is different from the price in the database, it is for checking the earnings. Is it possible for me to do it? Thanks a lot.. 
i think this is what you wantUPDATE tSET t.qty =@qty FROM (SELECT TOP 1 * FROM Product WHERE product_name = @productname ORDER BY DateField DESC) t This will update the quantity of latest (most recent) record of that product passed by user with quantity value given. If this is not what you require, you need to specify your requirement for first record. |
 |
|
|
d confz
Starting Member
12 Posts |
Posted - 2008-02-19 : 09:11:43
|
quote: Originally posted by harsh_athalye Define what do you mean by first row. In relational world, terms first and last are meaningless unless you specify record position relative to some sort order.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
The first row is the first data in the database that is equal to the value that the user have input.. |
 |
|
|
d confz
Starting Member
12 Posts |
Posted - 2008-02-19 : 09:12:54
|
quote: Originally posted by jackv Use the following , replacing 'Prod1' with your incoming variable CREATE TABLE Products(ID INT,Product_Name VARCHAR(20),Qty INT,Price DECIMAL(10,2))INSERT INTO Products SELECT 1,'Prod1',2,2.5UNIONSELECT 2,'Prod2',3,6.5UNIONSELECT 3,'Prod1',3,6.5UPDATE Products SET qty = 7 WHERE product_name = 'Prod1' AND ID IN (SELECT TOP 1 ID FROM Products as t2 WHERE t2.product_name = 'Prod1')SELECT * FROM ProductsDROP TABLE ProductsJack Vamvas--------------------Search IT jobs from multiple sources- http://www.ITjobfeed.com
Thanks a lot.. I'll try that.. |
 |
|
|
d confz
Starting Member
12 Posts |
Posted - 2008-02-19 : 09:13:35
|
quote: Originally posted by visakh16
quote: Originally posted by d confz Hello.. Can I update only a single row on the database?For example I have the table Product:product_name ------- qty -------- priceMonggol ------------- 12 ---------- 7Paper --------------- 10 ---------- 22Monggol ------------- 89 ---------- 51The user inputs a productname, qty and price. Then I need to update a product's qty where product name is equal to the product name from the user.Using the code:UPDATE Product SET qty = '"+number+"' WHERE product_name = '"+prodname+"';(number and prodname is the value from the user)This will update the qty of all the product_name that is equal to the prodname. But what I need is to update only the first row. I cannot use the price because the price that is input by the user is different from the price in the database, it is for checking the earnings. Is it possible for me to do it? Thanks a lot.. 
i think this is what you wantUPDATE tSET t.qty =@qty FROM (SELECT TOP 1 * FROM Product WHERE product_name = @productname ORDER BY DateField DESC) t This will update the quantity of latest (most recent) record of that product passed by user with quantity value given. If this is not what you require, you need to specify your requirement for first record.
Thank you very much.. |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-02-19 : 09:14:29
|
Again what do you mean by "first data in the database"?There could be hundreds of records matching user input, which one would you consider first? Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
d confz
Starting Member
12 Posts |
Posted - 2008-02-19 : 09:17:01
|
quote: Originally posted by harsh_athalye Again what do you mean by "first data in the database"?There could be hundreds of records matching user input, which one would you consider first? Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
I'm using the FIFO.. So it is the first product_name that is equal to prodname (value input by the user).. |
 |
|
|
d confz
Starting Member
12 Posts |
Posted - 2008-02-20 : 22:27:48
|
Hi.. I tried to use the TOP syntax and it returned an error. I later found out that it can only be used in transact-SQL. I'm using MySQL 5.0.45, which I think is an ANSI-SQL. Is there any other alternative for top? Thanks a lot.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-21 : 01:21:49
|
| In Mysql, you should use LIMIT functionexSelect columns from tableorder by somecolLIMIT 10Also post mysql related questions at www.mysql.comMadhivananFailing to plan is Planning to fail |
 |
|
|
pravin14u
Posting Yak Master
246 Posts |
Posted - 2008-02-21 : 02:10:15
|
quote: Originally posted by d confz
quote: Originally posted by harsh_athalye Again what do you mean by "first data in the database"?There could be hundreds of records matching user input, which one would you consider first? Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
I'm using the FIFO.. So it is the first product_name that is equal to prodname (value input by the user)..
Sir, As Harsh had pointed out, there is no 1st, 2nd , 3rd row etc in a relational database table. The table is just a SET and ordering of rows are not defined unless you realte it to a column in the table. Can you please explain your complete requirement? |
 |
|
|
d confz
Starting Member
12 Posts |
Posted - 2008-02-21 : 04:02:34
|
quote: Originally posted by madhivanan In Mysql, you should use LIMIT functionexSelect columns from tableorder by somecolLIMIT 10Also post mysql related questions at www.mysql.comMadhivananFailing to plan is Planning to fail
Thanks a lot.. |
 |
|
|
|