vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   SQL gurus: what is the fastest way to do a select and update (https://vborg.vbsupport.ru/showthread.php?t=154368)

mihai11 08-05-2007 12:44 PM

SQL gurus: what is the fastest way to do a select and update
 
Hi,



I am selecting a group of rows and I need to update 1 field (set a certain field to 1) for all the rows that meet the search criteria. Can this be done just with SQL (no PHP scripting) ?

I did some research: it looks like I cannot use a sub-query because MySql does not supports this use of sub-queries. For example, my query should look like this:

Code:

UPDATE story SET story.story_status=2
WHERE story.thread_id IN (SELECT thread_id FROM story WHERE (condition))

In MySql you cannot use the same table (in this case table "story) for both the subquery's FROM clause and the UPDATE target.



Regards,
Razvan

consolegaming 08-05-2007 04:21 PM

Wouldn't it just be something like:
Code:

UPDATE story SET story_status=2
WHERE thread_id IN (1,2,3)

where 1,2,3 are the various values you're looking for. If not then I think you probably need to explain why you need to use a sub query to be honest.

mihai11 08-05-2007 05:21 PM

Quote:

Originally Posted by consolegaming (Post 1310518)
Wouldn't it just be something like:
Code:

UPDATE story SET story_status=2
WHERE thread_id IN (1,2,3)

where 1,2,3 are the various values you're looking for. If not then I think you probably need to explain why you need to use a sub query to be honest.

This is almost what I need except that the numbers from the "IN" clause must be retrieved from the same table (using another select).

Business logic: I am working on a digg-like clone that integrates well in VB. At this point I need to develop a bot (that will be run on the server at certain intervals) that will promote stories once certain conditions are met. In order to achieve this, my bot would need to scan the stories table then update all the stories that satisfy the given criterias.

I don't want to give more details about how the system is working because that would allow users to cheat my system :rolleyes:


Regards,
Razvan

consolegaming 08-05-2007 05:45 PM

Quote:

Originally Posted by mihai11 (Post 1310565)
This is almost what I need except that the numbers from the "IN" clause must be retrieved from the same table (using another select).

Business logic: I am working on a digg-like clone that integrates well in VB. At this point I need to develop a bot (that will be run on the server at certain intervals) that will promote stories once certain conditions are met. In order to achieve this, my bot would need to scan the stories table then update all the stories that satisfy the given criterias.

I don't want to give more details about how the system is working because that would allow users to cheat my system :rolleyes:


Regards,
Razvan

Surely you can just replace thread_id IN (1,2,3) with the conditions you're looking for i.e. if the conditions was that the story had title of 'whatever' and a type of "post" then you'd just put:

UPDATE story SET story_status=2
WHERE title='whatever' AND type='post'

those fields where just examples though as I don't know what conditions you are wanting to be met, if that doesn't sort it I think we'd need to know the conditions you're trying to meet or at least examples of such for us to help any further.

mihai11 08-05-2007 06:03 PM

Quote:

Originally Posted by consolegaming (Post 1310574)
Surely you can just replace thread_id IN (1,2,3) with the conditions you're looking for i.e. if the conditions was that the story had title of 'whatever' and a type of "post" then you'd just put:

UPDATE story SET story_status=2
WHERE title='whatever' AND type='post'

those fields where just examples though as I don't know what conditions you are wanting to be met, if that doesn't sort it I think we'd need to know the conditions you're trying to meet or at least examples of such for us to help any further.

The WHERE clause MUST contain a SELECT from the same table. Please see my example from the first post. This is not supported by MySql.

Anyway, I am thinking to do this the old fashion way, with a PHP script. That means, I will read all the required rows then I will apply the processing to each row. I am thinking that with this I will have better error-control.

consolegaming 08-05-2007 06:39 PM

Unless you explain why you need a select you don't need one. The Update does exactly what you're saying with the where conditions in place it find all rows that meet those conditions and then updates them. There isn't any need for a subquery as far as I can see. Everything you've said points at exactly the query I just gave you. You don't require a sub query.

mihai11 08-06-2007 07:04 AM

Quote:

Originally Posted by consolegaming (Post 1310610)
Unless you explain why you need a select you don't need one. The Update does exactly what you're saying with the where conditions in place it find all rows that meet those conditions and then updates them. There isn't any need for a subquery as far as I can see. Everything you've said points at exactly the query I just gave you. You don't require a sub query.

This is quite embarrassing. I think that you are right: this can be done without the subquery !:eek:

I guess I just put into my mind that the subquery is necessary then I saw no way to do it without a subquery.

Wayne Luke 08-07-2007 03:58 PM

Upgrade to MySQL 5 and create a view for your subquery in the original post. Then it should work properly. Then you don't need to update your code if for instance you want to add additional threads in the future. Without the subquery, you would most likely need a prior query that builds your array to check against the IN clause. Though depending on the where clauses, two queries may be more intensive. Unless you use stored procedures.


All times are GMT. The time now is 12:10 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01606 seconds
  • Memory Usage 1,736KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete