Jump to content

insert if user exists


Destramic

Recommended Posts

hey guys im wondering if it's possible to insert a row if a value exists inside another table?

 

for instance the code below i wish to make a log in attempt record if the user actually exists in the users table

INSERT INTO login_attempts ('identity')
VALUES ('name@example.com')
SELECT user_id
FROM users
WHERE email_address = 'name@example.com'

thanks you

Link to comment
Share on other sites

You are mixing statement syntaxes here.

 

What do want to insert into the identity column? If it is the id of the user then

INSERT INTO login_attempts ('identity')
SELECT user_id
FROM users
WHERE email_address = 'name@example.com'
Edited by Barand
Link to comment
Share on other sites

well id like to check if users exists first before entering a log in attempt...after a bit more researching i think i need something like this but it doesn't work

INSERT INTO login_attempts (identity)
VALUES ('destramic@hotmail.com')
WHERE NOT EXISTS (SELECT user_id FROM users 
      WHERE email_address = 'destramic@hotmail.com')

is this even possible?

 

thank you

Link to comment
Share on other sites

INSERT INTO login_attempts ('identity')
SELECT email_address
FROM users
WHERE email_address = 'name@example.com'

If email doesn't exist in users table then no insert.

 

 

brilliant thank you...is it also possible to add custom values to this query other than what is selected from the users table?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.