Solved

sql find and replace multiple records

Posted on 2008-10-04
8
1,170 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…

911 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

23 Experts available now in Live!

Get 1:1 Help Now