我将如何合并以下两个sql select语句?
//select all rows from our userlogin table where the emails match
$res1 = mysql_query("SELECT *
FROM userlogin
WHERE `email` = '".$email."'");
$num1 = mysql_num_rows($res1);
//if the number of matchs is 1
if($num1 == 1) {
//the email address supplied is taken so display error message
echo '<p class="c7">The <b>e-mail</b> address you supplied is already taken. Please go <a href="register.php">back</a> and try again.<br><img src="resources/img/spacer.gif" alt="" width="1" height="15"></p>';
include_once ("resources/php/footer.php");
exit;
} else {
//select all rows from our userlogin_fb table where the emails match
$res2 = mysql_query("SELECT *
FROM userlogin_fb
WHERE `email` = '".$email."'");
$num2 = mysql_num_rows($res2);
//if the number of matchs is 1
if($num2 == 1) {
//the email address supplied is taken so display error message
echo '<p class="c7">The <b>e-mail</b> address you supplied is already taken. Please go <a href="register.php">back</a> and try again.<br><img src="resources/img/spacer.gif" alt="" width="1" height="15"></p>';
include_once ("resources/php/footer.php");
exit;
} else {;}
答案 0 :(得分:3)
使用:
//select all rows from our userlogin table where the emails match
$query = sprintf("SELECT 1
FROM userlogin
WHERE `email` = '%s'
UNION ALL
SELECT 1
FROM userlogin_fb
WHERE `email` = '%s' ",
$email, $email);
$res1 = mysql_query($query);
$num1 = mysql_num_rows($res1);
//if the number of matchs is 1
if($num1 >= 1) {
//the email address supplied is taken so display error message
echo 'The <b>e-mail</b> address you supplied is already taken. Please go <a href="register.php">back</a> and try again.<br><img src="resources/img/spacer.gif" alt="" width="1" height="15"></p>';
include_once ("resources/php/footer.php");
exit;
}
答案 1 :(得分:1)
两种方法(仅适用于SQL部分):
解决方案1:UNION
SELECT *
FROM userlogin
WHERE email = '$email'
UNION
SELECT *
FROM userlogin_fb
WHERE email = '$email';
两个表基本上都需要相同的字段,否则调整SELECT *
- 语句的一部分。
解决方案2:链接表
SELECT *
FROM userlogin AS T1
JOIN userlogin_fb AS T2 ON T1.email = T2.email
WHERE T1.email = '$email';
首先,我更喜欢(1),因为(2)可能会受到表格设计的影响。