suprsnipes Posted June 11, 2009 Share Posted June 11, 2009 Hi, I need a little bit of help. I'm want to update the data in field type1 based on the following working query and just can't seem to get it right. SELECT * FROM test2 t1 INNER JOIN test2 t2 ON t1.id1 = t2.id1-1 WHERE t2.type1 = 'N' AND t1.type1 = 'a' AND t2.price1 >= t1.price1 Appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/ Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 What help? You didn't ask a question or tell us what you want to do. Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853433 Share on other sites More sharing options...
suprsnipes Posted June 11, 2009 Author Share Posted June 11, 2009 Sorry. I want to 'modify' the working SQL SELECT query to an UPDATE query. The table I want to update is named `test2` and the field is named type1, the data to be updated in this case will simply = buy Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853436 Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 "UPDATE test2 SET type1=buy" Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853490 Share on other sites More sharing options...
suprsnipes Posted June 11, 2009 Author Share Posted June 11, 2009 My apologies, I'm sure it's because I'm a slow learner or that I just don't know what I am doing, or both but I've tried the following and there is an error in the SYNTAX. #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT * FROM test2 t1 INNER JOIN test2 t2 ON t1.id1 = t2.id1-1 WHERE t2.type1 ' at line 1 UPDATE test2 SET type1 = 'buy' (SELECT * FROM test2 t1 INNER JOIN test2 t2 ON t1.id1 = t2.id1-1 WHERE t2.type1 = 'N' AND t1.type1 = 'a' AND t2.price1 >= t1.price1) Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853513 Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 Well your syntax is wrong, but it's not really apparent what you are trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853517 Share on other sites More sharing options...
suprsnipes Posted June 11, 2009 Author Share Posted June 11, 2009 I'm trying to update the type 1 column, currently the working SQL is creating a virtual table from the `test2` table and joining itself WHERE type1 is = 'N' and previous (previous id) column's type 1 is equal to 'a'. As you can tell I'm comparing one entries data to the previous entries data using the id column which is set to auto_increment. SELECT * FROM test2 t1 INNER JOIN test2 t2 ON t1.id1 = t2.id1-1 WHERE t2.type1 = 'N' AND t1.type1 = 'a' AND t2.price1 >= t1.price1 What I want to do is when the data meets the above criteria I want the type1 column to be updated in the original table `test2` from 'N' to 'buy'. I can run an UPDATE query on an existing table no problem but I'm uncertain as to how I can do this in conjunction with a JOIN statement like the above where I'm using a Virtual Table. Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853523 Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 You will need to select something other than *. If you do that, you can have something like this: UPDATE table1 SET table1.column1 = 'something' WHERE table1.id IN (SELECT id FROM anothertable). The sub query (in parenthesis) returns a list of ids. If the id from the main query is one of the returned IDs, it will update. Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853527 Share on other sites More sharing options...
suprsnipes Posted June 11, 2009 Author Share Posted June 11, 2009 This UPDATE statement is a real pain in the butt. SELECT t1.id1,t2.id1 FROM test2 t1 JOIN test2 t2 ON t1.id1 =t2.id1-1 WHERE t2.type1 = 'N' AND t1.type1 = 'a' AND t2.price1 >= t1.price1 Where the above code works but I can't seem to get the UPDATE statement below too. UPDATE (Select t1.id1,t2.id1 FROM test2 t1 JOIN test2 t2 ON t1.id1 =t2.id1-1) SET t2.type1 = buy WHERE t2.type1 = 'N' AND t1.type1 = 'a' AND t2.price1 >= t1.price1 I have also tried SET test2.type1 = buy. In both instances I'm getting the following error: #1248 - Every derived table must have its own alias Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853561 Share on other sites More sharing options...
suprsnipes Posted June 11, 2009 Author Share Posted June 11, 2009 This UPDATE statement is a real pain in the butt. SELECT t1.id1,t2.id1 FROM test2 t1 JOIN test2 t2 ON t1.id1 =t2.id1-1 WHERE t2.type1 = 'N' AND t1.type1 = 'a' AND t2.price1 >= t1.price1 Where the above code works and displays the following; id1 id1 25 26 27 28 58 59 60 61 62 63 64 65 101 102 121 122 142 143 151 152 164 165 196 197 203 204 234 235 236 237 251 252 292 293 297 298 349 350 355 356 366 367 446 447 475 476 579 580 624 625 626 627 674 675 676 677 708 709 789 790 I can't seem to get the SYNTAX right for the UPDATE statement below. UPDATE (Select t1.id1,t2.id1 FROM test2 t1 JOIN test2 t2 ON t1.id1 =t2.id1-1) SET t2.type1 = buy WHERE t2.type1 = 'N' AND t1.type1 = 'a' AND t2.price1 >= t1.price1 I have also tried SET test2.type1 = buy. In both instances I'm getting the following error: #1248 - Every derived table must have its own alias Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853583 Share on other sites More sharing options...
fenway Posted June 11, 2009 Share Posted June 11, 2009 So? give it an alias. Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853702 Share on other sites More sharing options...
suprsnipes Posted June 11, 2009 Author Share Posted June 11, 2009 ??? I know this is a stupid question but how do I give it an alias? Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-853778 Share on other sites More sharing options...
Ken2k7 Posted June 12, 2009 Share Posted June 12, 2009 What is buy? A column name or just a matching string? As for aliases, Google. Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-854176 Share on other sites More sharing options...
fenway Posted June 15, 2009 Share Posted June 15, 2009 As for aliases, Google. Not helpful whatsoever... in the same number of characters you could have provided a useful response. Just add "AS derviedTable" after the table. Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-856455 Share on other sites More sharing options...
Ken2k7 Posted June 16, 2009 Share Posted June 16, 2009 Not helpful whatsoever... in the same number of characters you could have provided a useful response. That's what you think. Quote Link to comment https://forums.phpfreaks.com/topic/161752-need-help-to-incorporate-update-statement-in-working-sql/#findComment-856737 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.