Solved

sql find and replace multiple records

Posted on 2008-10-04
8
1,171 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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

803 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