The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
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)) Regards, Razvan |
#2
|
|||
|
|||
Wouldn't it just be something like:
Code:
UPDATE story SET story_status=2 WHERE thread_id IN (1,2,3) |
#3
|
|||
|
|||
Quote:
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 Regards, Razvan |
#4
|
|||
|
|||
Quote:
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. |
#5
|
|||
|
|||
Quote:
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. |
#6
|
|||
|
|||
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.
|
#7
|
|||
|
|||
Quote:
I guess I just put into my mind that the subquery is necessary then I saw no way to do it without a subquery. |
#8
|
||||
|
||||
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.
|
Thread Tools | |
Display Modes | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|