LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2006, 10:45 PM   #1
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
MySQL If Statement troubles


I'm trying to write an SQL script that will do the following:

CHECK to see if the user is in the user table
If the User is NOT in the user table THEN ADD the user.

I'm very new to MySQL. This is probably very simple I just can't figure it out at the moment.

Thanks.
 
Old 04-24-2006, 04:50 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

First you need to make a UNIQUE index on the column that should be unique (like username). Then you can just insert, and the insert will fail if the username is already in use.

You can also use REPLACE INTO instead of INSERT INTO if you dont want an error when this happens.

Hope this helps,
Guttorm.
 
Old 04-24-2006, 05:43 AM   #3
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Original Poster
Rep: Reputation: 30
This table is built into MySQL... I don't want to be adding an index to it if I don't have to. That is why I'm trying to figure out how to use the IF statement.
 
Old 04-24-2006, 08:03 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi again.

Ok, so you mean the MySQL users table. You should look into the GRANT command instead then to give the privileges (as few as possible). But still it makes no difference - why do you need to make this check?

If you simply grant some privileges to some user - if that user existed before, nothing will happen. Why do you need such a check?

BTW, the UNIQUE thing in the MySQL users table seems to be a combination of username and hostname.
 
Old 04-25-2006, 02:59 AM   #5
smallville
Member
 
Registered: Dec 2005
Posts: 44

Rep: Reputation: 15
hi! what applications are you using? just Mysql? or mysql and php? mysql and perl? or just mysql?
 
Old 04-25-2006, 07:20 AM   #6
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Original Poster
Rep: Reputation: 30
Just MySQL
 
Old 04-26-2006, 01:31 AM   #7
smallville
Member
 
Registered: Dec 2005
Posts: 44

Rep: Reputation: 15
try this :

insert into users_table select uname from
(select case when count(aa.username_field)=0 then 'whatever_name' else '' end as uname
from
(select username_field from users_table where username_field='whatever_name') as aa) as bb
where uname<>''

but make sure that your select statement fields has the same number as your insert.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql troubles Algernon Linux - Software 1 09-28-2004 11:06 PM
how to run the mysql load statement in cron gschrade Linux - Software 5 07-27-2004 02:27 PM
mysql troubles! cav Slackware 2 02-07-2004 03:25 AM
mysql sql statement help mrtwice Programming 4 12-02-2003 10:04 AM
mysql statement... johnyy Linux - Software 1 11-02-2003 03:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration