1

我在 mysql 中有两个表,其中包含有关用户的详细信息,例如电子邮件、名字和姓氏。

我做了一种方法来欢迎登录的用户,但问题是两个表中的某些值是相似的,这会导致重复,我想知道如何区分不同表中的值。

我尝试了这段代码,但没有奏效:

$query=  "SELECT * FROM Table1 WHERE email = '".$_SESSION['Email']."' NOT IN (SELECT email = '".$_SESSION['Email']."' FROM Table2)";  
4

2 回答 2

1

您的查询是:

SELECT *
FROM Table1
WHERE email = '".$_SESSION['Email']."' NOT IN (SELECT email = '".$_SESSION['Email']."' FROM Table2)

该表达式email = '".$_SESSION['Email']."'是一个布尔表达式——本质上是0or 1

SELECT *
FROM Table1
WHERE email = '".$_SESSION['Email']."' and
      email NOT IN (SELECT email from Table2 where email = '".$_SESSION['Email']."')

因为比较是相同的,所以更有效地写成:

SELECT *
FROM Table1
WHERE email = '".$_SESSION['Email']."' and
      not exists (SELECT email from Table2 where email = '".$_SESSION['Email']."')

在两个表中都有一个索引email将提高性能。

于 2013-07-23T01:03:29.143 回答
0

Have the two tables the same email? Why?

Ok here is maybe a solution if you want only from table 1 the user data from you session email try this:

$query = "SELECT t1.*, t2.* FROM table t1, table t2 WHERE t1.email <> t2.email AND t1.email = '".$_SESSION["Email"]."'";

Recommend: Try to select what you really need... email, username, name etc... not SELECT *

于 2013-07-23T00:55:12.497 回答