简体   繁体   English

Mysqli从两个不同的表中获取信息

[英]Mysqli Get Information from two different tables

I have two different tables one hold comments and the other user information my first mysqli_prepare statement looks like this 我有两个不同的表,一个持有注释,另一个用户信息,我的第一个mysqli_prepare语句看起来像这样

    if($stmt = mysqli_prepare($mysqli, "SELECT text,username,id,likes,dislikes FROM post WHERE school = '$sname' ORDER BY id DESC LIMIT 0,10"))

and the other looks like this 而另一个看起来像这样

   if($stmts = mysqli_prepare($mysqli, "SELECT avatar FROM users WHERE username = '$user'"))

basically i need to combine these two statements so that I can access both sets in the same while loop which looks like this 基本上我需要结合这两个语句,以便我可以在相同的while循环中访问这两个集合,如下所示

    while(mysqli_stmt_fetch($stmt))

any help is appreciated thank you in advance and I will vote up. 感谢任何帮助提前谢谢你,我会投票。

JOIN operation on common field username 公共字段用户名上的JOIN操作

SELECT post.text,post.username,post.id,post.likes,post.dislikes FROM post 
INNER JOIN users 
ON post.username = users.username
WHERE post.school = '$sname' 
AND users.username = '$user'
ORDER BY post.id DESC LIMIT 0,10

To do so with SQL you will need to combine your queries using JOIN . 要使用SQL执行此操作,您需要使用JOIN组合查询。

Making some assumptions, the following is an example: 做一些假设,以下是一个例子:

SELECT ... FROM post JOIN users ON post.username = users.username WHERE ...

Note: An alternative would be to SELECT the data separate (as you have) and stitch the data together with PHP. 注:另一种方法是,以SELECT分离数据(如你),并用PHP 缝合在一起的数据。 Sometimes this is preferred over a complex JOIN . 有时这比复杂的JOIN However, what you have seems fine. 但是,你有什么似乎很好。

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

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