简体   繁体   English

MySQL从同一查询的两个表中选择

[英]MySQL selecting from two tables in the same query

I am trying to select everything from two different mysql tables. 我试图从两个不同的mysql表中选择所有内容。 I imploded an array called friends so that I can select everything about the user's friends from both tables. 我内爆了一个名为friends的数组,以便可以从两个表中选择有关用户朋友的所有内容。 Originally I wanted to perform one query on one table and then in a while loop a query on the other. 最初,我想对一个表执行一个查询,然后在一段时间内循环对另一个表进行查询。 But that didn't work. 但这没有用。 If you know I way I can nest while loops then please be sure to comment/answer this question. 如果您知道我可以在while循环中嵌套的方式,那么请务必评论/回答此问题。

Here's my code: 这是我的代码:

$friends = implode("','", $friends);
//implode the friends array for sql query

$sql = mysqli_query($con, "SELECT * FROM users, text_post WHERE username IN ('$friends')") or die(mysqli_error($con));
while ($row = mysqli_fetch_assoc($sql)) {
    echo $row['last_name']. ' ' . $row['body'];
    echo '<br><br>';
}

Note: $row['last_name'] is from the users table and $row['body'] is from the text_post table. 注意: $row['last_name']来自users表,而$row['body']来自text_post表。 I am receiving this error whenever I run my code Column 'username' in where clause is ambiguous 每当我Column 'username' in where clause is ambiguous代码Column 'username' in where clause is ambiguous时,我都会收到此错误

Please help me. 请帮我。

UPDATE: 更新:

I changed my query to: $sql = mysqli_query($con, "SELECT * FROM users, text_post WHERE users.username IN ('$friends')") or die(mysqli_error($con)); 我将查询更改为: $sql = mysqli_query($con, "SELECT * FROM users, text_post WHERE users.username IN ('$friends')") or die(mysqli_error($con)); but now it echo's every match twice: 但现在它回声每场比赛两次:


parker hello 帕克你好

parker hello 帕克你好

simms what is up Simms发生了什么

simms what is up Simms发生了什么

simms it's raining 西姆斯正在下雨

simms it's raining 西姆斯正在下雨

jorge potato 约格马铃薯

jorge potato 约格马铃薯


Why is it doing that? 为什么这样做呢?

The only thing that is the same in both tables is the username. 两个表中唯一相同的是用户名。

both tables have a username. 两个表都有一个用户名。 specify it using table.username 使用table.username指定它

apparently both tables have the same column, and you are not prefixing the username column with either table name. 显然,两个表都具有相同的列,并且您没有在用户名列之前添加任何一个表名。 Maybe what you want is a union? 也许您想要的是工会?

where块中使用表前缀users ,即users.username

$sql = mysqli_query($con, "SELECT * FROM users, text_post WHERE users.username IN ('$friends')") or die(mysqli_error($con));

Try this: 尝试这个:

$friends = implode("','", $friends);
    //implode the friends array for sql query

    $sql = mysqli_query($con, "SELECT users.last_name,text_post.body FROM users, text_post WHERE username IN ('$friends')") or die(mysqli_error($con));
    while ($row = mysqli_fetch_assoc($sql)) {
        echo $row['last_name']. ' ' . $row['body'];
    }

Your logic would work were it not for username being present in both tables. 如果两个表中都不存在用户名,则您的逻辑将起作用。 Because it is, you need to alias your table names so that you can uniquely identify which column you're trying to use in the where clause: 因为是这样,所以您需要为表名加上别名,以便可以唯一标识要在where子句中使用的列:

SELECT * from users AS u, text_post as tp WHERE u.username IN(....) SELECT * from as AS u,text_post as tp WHERE u.username IN(....)

or shorter: SELECT * from users u, text_post tp WHERE u.username IN(....) 或更短:SELECT * from users u,text_post tp WHERE u.username IN(....)

As a side note, it's bad practice to SELECT *, you should always explicitly select the columns you want, otherwise your code may crash if you add a column to a table later and forget to update your for loop to include/ignore any changed columns. 附带说明,SELECT *是一种不好的做法,您应该始终明确选择所需的列,否则,如果稍后将一个列添加到表中而忘记更新for循环以包括/忽略任何更改的列,则代码可能会崩溃。

Thanks for your question. 感谢您的提问。 The issue here is the error that you are receiving of: 这里的问题是您收到的错误:

Column 'username' in where clause is ambiguous.

What this means is that the statement that you are running is too obscure. 这意味着您正在运行的语句太模糊了。

What you need to do to avoid this is to explicitly define the tables and columns that you are attempting to access and thus be implicit in your statements. 为避免这种情况,您需要做的是显式定义要尝试访问的表和列,从而将它们隐含在语句中。

As a rule of thumb try and always define your columns in the following format: 根据经验,请始终以以下格式定义列:

{table_name}.{column_name}

With that being said the following should work: 话虽如此,以下应该起作用:

$friends = implode("','", $friends);
/* implode the friends array for sql query. */

$sql = mysqli_query($con, "SELECT * FROM users, text_post WHERE users.username IN ('$friends') AND text_post.username IN ('$friends')") or die(mysqli_error($con));
while ($row = mysqli_fetch_assoc($sql)) {
    echo $row['last_name']. ' ' . $row['body'];
}

Also I would also recommend that you use PHP's PDO Object as appose to the mysqli_query method. 另外,我还建议您使用PHP的PDO对象作为mysqli_query方法的对象。

I trust this helps. 我相信这会有所帮助。

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

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