简体   繁体   English

一个查询,一张表,两个count()

[英]one query, one table, two count()

I want to check if username and email taken in my registration script. 我想检查我的注册脚本中是否包含用户名和电子邮件。

This is how I check with query: 这是我检查查询的方式:

$emailcheck = "SELECT COUNT(email) AS nume FROM members WHERE email = :email";
//bindValue
//execute
//fetch
if($rowe['nume'] > 0){
$errors[] = "E-mail exist.";
} 

And also I'm doing the same thing for username; 而且我对用户名也做同样的事情;

$usercheck = "SELECT COUNT(username) AS numu FROM members WHERE username = :username";
//bindValue
//execute
//fetch
if($rowu['numu'] > 0){
$errors[] = "Username taken.";
} 

* *

I want to go one step further and handle all of stuff with one query. 我想更进一步,用一个查询处理所有内容。

But I couldn't came up with such query. 但是我无法提出这样的查询。

I tried: 我试过了:

$check = "SELECT COUNT(username) AS numu and COUNT(email) AS nume FROM members WHERE username = :username OR email = :email";

but probably It's ridiculous. 但这很荒谬。

How to handle what I want with one query? 如何用一个查询处理我想要的?

And after I want to check like that: 在我想要检查之后:

if($row['numu'] > 0){
$errors[] = "Username taken.";
} 
if($rowe['nume'] > 0){
$errors[] = "E-mail exist.";
} 

So it will be less code, instead of connecting same table twice and bindValue, execute, fetch for second time. 因此,它将减少代码,而不是将同一张表连接两次并bindValue, execute, fetch第二次bindValue, execute, fetch

You can just do Union All to unite those queries: 您可以执行“ Union All合并Union All以合并这些查询:

SELECT COUNT(email) AS num FROM members WHERE email = :email
UNION ALL
SELECT COUNT(username) AS num FROM members WHERE username = :username

Then extract 2 according rows. 然后根据行提取2。

OR, MySQL allows this thing: 或者,MySQL允许以下内容:

SELECT 
    (SELECT COUNT(email) FROM members WHERE email = :email) as nume,
    (SELECT COUNT(username) FROM members WHERE username = :username) as numu

if you want 1 rows with 2 columns. 如果要1行2列。

Do that only if you need to see which one is already present. 仅当您需要查看已经存在的那个时才这样做。 Otherwise just do this: 否则,只需执行以下操作:

SELECT 1 FROM members WHERE email = :email OR username = :username LIMIT 1

Yes, consider not doing count() because you don't need to count all the rows. 是的,考虑不执行count()因为您不需要对所有行进行计数。 You just need to stop if you find just one. 如果只找到一个,就只需要停下来。 So either do a LIMIT or IF EXISTS() 因此,请执行LIMITIF EXISTS()

I don't think you really need to count. 我认为您真的不需要数数。 Assuming you want to check if either username or email already exist because they are required to be unique on your user table, you can do this: 假设您要检查用户名或电子邮件是否已经存在(因为要求用户名或电子邮件在用户表中是唯一的),可以执行以下操作:

First, add a unique index to each of those columns in your database. 首先,向数据库中的每个列添加唯一索引。 You may already have this, but if you want those values to be unique, this will ensure that even if your PHP code fails to do so for some reason. 您可能已经有了它,但是如果您希望这些值唯一,那么即使您的PHP代码由于某些原因而失败,也可以确保这样做。

Then you can use this query: 然后,您可以使用以下查询:

SELECT username, email FROM members WHERE username = :username OR email = :email

This will return either zero, one, or two rows, where: 这将返回零,一或两行,其中:

  • 0 = neither username nor email was found 0 =既未找到用户名,也未找到电子邮件
  • 1 = one row was found having either username, email, or both 1 =发现一行包含用户名,电子邮件或两者兼有
  • 2 = username was found in one row and email was found in another 2 =在一行中找到用户名,在另一行中找到电子邮件

Then you can loop over your results, comparing them to the user input, and set your errors. 然后,您可以遍历结果,将其与用户输入进行比较,并设置错误。

while ($row = //fetch) {
    if ($row['username'] == $username) {
        $errors[] = "Username taken.";
    } 
    if ($row['email'] == $email) {
        $errors[] = "E-mail exist.";
    } 
}

You can try this after removing and between count 您可以在删除之后and计数之间尝试

   $check = "SELECT COUNT(username) AS uname , 
       COUNT(email) AS uemail FROM members 
       WHERE (username = :username OR email = :email)";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM