不同于mysql中不同表的值

时间:2022-03-01 14:15:29

I have two tables in mysql that contain details about users such as email, first name and last name.

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

and I made a way to welcome the user that login but the problem is that some value in the two tables are similar which cause duplication, and I want to know how to differ between values from different tables.

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

I tried this code but it didn't work:

我试过这段代码,但它不起作用:

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

2 个解决方案

#1


1  

Your query is:

您的查询是:

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

The expression email = '".$_SESSION['Email']."' is a boolean expression -- essentially 0 or 1.

表达式email ='“。$ _ SESSION ['Email']。”'是一个布尔表达式 - 基本上是0或1。

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

Because the comparisons are the same, this is more efficiently written as:

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

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

Having an index on email in both tables will speed performance.

在两个表中都有一个电子邮件索引可以提高性能。

#2


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:

好的,这里可能是一个解决方案,如果你只想从表1中获得会话电子邮件中的用户数据,请尝试以下方法

$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 *

推荐:尝试选择你真正需要的东西......电子邮件,用户名,姓名等...不是SELECT *

#1


1  

Your query is:

您的查询是:

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

The expression email = '".$_SESSION['Email']."' is a boolean expression -- essentially 0 or 1.

表达式email ='“。$ _ SESSION ['Email']。”'是一个布尔表达式 - 基本上是0或1。

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

Because the comparisons are the same, this is more efficiently written as:

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

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

Having an index on email in both tables will speed performance.

在两个表中都有一个电子邮件索引可以提高性能。

#2


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:

好的,这里可能是一个解决方案,如果你只想从表1中获得会话电子邮件中的用户数据,请尝试以下方法

$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 *

推荐:尝试选择你真正需要的东西......电子邮件,用户名,姓名等...不是SELECT *