Solved

sql find and replace multiple records

Posted on 2008-10-04
8
1,169 Views
Last Modified: 2012-05-05
i have the following search and replace sql:

update jos_lmusers
set group_id = "GROUP_ID"
where users_id = "USER_ID"

I'm able to use this to update one single field for which I have entered a user id. but i would like to update multiple fields, how do I add more user ids to this code. would it be something like this??

update jos_lmusers
set group_id = "group_id"
where users_id = "user_id-1","user_id-2"

your input would be most appreciated!!
0
Comment
Question by:ost87
  • 4
  • 2
  • 2
8 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22643155
You can search on multiple id's using an IN statement.

Alternatively you would have to use OR statements.

users_id = "user1" OR users_id = "users2"
update jos_lmusers

set group_id = "GROUP_ID"

where users_id IN ("user_id-1","user_id-2")

Open in new window

0
 
LVL 11

Accepted Solution

by:
petiex earned 500 total points
ID: 22643163
You want the "IN" operator:

update jos_lmusers
set group_id = "group_id"
where users_id IN ("user_id-1","user_id-2")

0
 
LVL 11

Expert Comment

by:petiex
ID: 22643166
oh, and you probably want to use single quotes there instead of double quotes
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22643175
For MySQL, you may be able to use ANY keyword as well:

WHERE users_id = ANY("user_id-1","user_id-2")

But IN works on other platforms as well.

http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22643186
I thought so to petiex, but I thought maybe I just don't use MySQL enough. :)
Figured it was allowed since ` is the column identifier instead of ".  Still think it is a good idea to use ' as again it is common on other platforms of SQL like IN versus ANY.

Anyway, good luck.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22643573
OK you kind of ask about updating multiple fields and also adding more users...

Add fields into the update by seperating with a comma, and use the IN clasue in the where statement, and encapsulate your literams with a single comma :

update jos_lmusers
set group_id = 'group_id', other_field = 'Other'
where users_id IN ('user_id-1','user_id-2')

 and if you want to make it conditional, then use a case statement

update jos_lmusers
set group_id = 'group_id', other_field = case when users_id = 'user_id-2' then 'Other_2' else 'Other_1' end
where users_id IN ('user_id-1','user_id-2')

Hope that covers it all off for you...
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22645026
@ost87 - interesting - why did you give it to petiex when mwvisa had also (previously) posted the same code ? Just interested that's all.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22645118
:( Wasn't even a simultaneous post, I typed in more text and posted 2 MINs ahead.

Just glad you got the answer you were looking for.  Hopefully you saw that all three of us think you should go with single quotes (') for string literals in MySQL.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now