473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

using subquery with insert to avoid duplicate records

2
Hello,

I am trying to use a subquery to avoid duplicate entries when someone submits a form to subscribe to a mailing list.

So I want to check if the email exists before adding the record.

I tried something like:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO mailing_list (ID,Email) VALUES ('','$Email') WHERE NOT EXISTS (SELECT * FROM mailing_list  WHERE Email LIKE '$Email'')
Using NOT IN and various other structures, but keep receiving mysql errors.

I can just "give up" and run two queries, the first to look for the email and the second to enter the email if it does not already exist; I just want to start being more efficient and using subqueries. Thanks for any advice!!!
Aug 20 '07 #1
3 4022
pbmods
5,821 Expert 4TB
Heya, Audj.

Try this:
Expand|Select|Wrap|Line Numbers
  1. REPLACE
  2.     INTO
  3.         `mailing_list`
  4.         (
  5.             `Email`
  6.         )
  7.     VALUES
  8.         (
  9.             '$Email'
  10.         )
  11.  
For best results, make sure `Email` is a unique key.
Aug 20 '07 #2
audj
2
Ah, that's a good approach. I often forget REPLACE.

Still gotta tackle those subqueries though...

Thanks...

Audj!
Aug 20 '07 #3
pbmods
5,821 Expert 4TB
Heya, Audj.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 20 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gary Lundquest | last post by:
It appears to me that MySQL version 4 returns an error messge when doing an Insert that results in duplicate entries. Version 3 did NOT return an error - it dropped the duplicate entries and ran...
3
by: Bob | last post by:
Why, in the process of creating a unique index, does SQL Server allow me to select the "Ignore duplicate keys" option? Wouldn't I just create a non-unique index if I wanted to ignore duplicate...
9
by: Curtis Stanford | last post by:
I'm in a situation where I need to load an ASCII file into a database. No sweat so far right? The records are indexed by date and I have a unique index on date. The ASCII can overlap, meaning it...
2
by: btober | last post by:
I run the following script to export some data from my development database and then update or insert the records into to the quality assurance testing database, but I get a warning notice that I...
2
by: mivey4 | last post by:
Okay I have 2 tables: Table A - holds a list of new hardware serial numbers and their corresponding model (no constraints or indexes) Table B - holds a distinct list of current serial numbers...
0
by: kiran2nirvan | last post by:
hi please help in solving this i am recieving this error"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as...
1
debasisdas
by: debasisdas | last post by:
Using Co-related sub query ======================== While a subquery is evaluated only once for each table, a correlated subquery is evaluated once for each row. Sub query can take value from...
1
by: mnymoen | last post by:
I have tried 2 variations of this query and cannot get it to work due to the fact I cant use "where email in". I get this error "Only one expression can be specified in the select list when the...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.