Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql find and replace multiple records

Posted on 2008-10-04
8
Medium Priority
?
1,178 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 60

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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 60

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
 
LVL 60

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 60

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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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