Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

sql find and replace multiple records

Posted on 2008-10-04
8
1,172 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

837 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