我们如何验证电子邮件ID [英] how can we validate the email id

查看:121
本文介绍了我们如何验证电子邮件ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查已经在数据库中的电子邮件意味着插入是否应该不存在,如果它存在,那么我们需要插入所以我们需要在存储过程中写条件。



< modified>

如何检查电子邮件是否已存在于数据库中。如果电子邮件存在,则不应发生新插入。我们是否需要在商店程序中写出类似 if else 条件的内容来检查这个?

< / modified>

how to check whether the email already in databse means insert should not happended if it doest exist then we need to insert so we need to write if condition in stored procedure.

<modified>
How to check whether the email is already existed in database or not. New insert should not happened if the email is existed. Do we need to write something like "if else " condition in store procedure to check this out?
</modified>

推荐答案

select count(*) from `table_name` where `email` = "this@email.com";





这将返回一个值。



如果值为1表示,则电子邮件存在一次。

如果2表示,则存在两次。

如果0表示...



那里,您可以检查电子邮件是否被使用。



this will return a value.

if the value is 1 means, the email is existed once.
if 2 means, existed twice.
if 0 means...

there, you can check the email is used or not.


CREATE PROCEDURE [dbo].[InsertName_sp]
(
  @Email varchar(50)
)
AS
IF EXISTS(SELECT 'True' FROM Resource WHERE Email= @Email)
BEGIN

  SELECT 'This Email already exists!'
END
ELSE
BEGIN

  SELECT 'Email Added'
  INSERT into Resource(Email) VALUES(@Email)
END


哦,所以它是一个数据库,更有意义。哪个数据库?它是否支持EXISTS?



就个人而言,我从未使用过EXISTS,但在规定的情况下可能会更好。



这种方式不使用EXISTS;如果您有大量的电子邮件需要测试和插入,可能会更好:



Oh, so it''s a database, that makes more sense. Which database? Does it support EXISTS?

Personally, I''ve never had a use for EXISTS, but it may be better in the stated situation.

This way doesn''t use EXISTS; it''s probably better if you have a number of emails to test and insert:

INSERT INTO SomeTable (SomeText)
SELECT email
FROM (SELECT @email email) A
LEFT OUTER JOIN SomeTable B
ON A.email=B.SomeText
WHERE B.SomeText IS NULL





我绝对会避免使用IF / ELSE;太不整洁了。



如果您使用的是SQL Server,您可能也对MERGE声明感兴趣

http://technet.microsoft.com/en-us/library/bb510625.aspx [ ^ ]


这篇关于我们如何验证电子邮件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆