PHP MySQL根据第一次查询的结果从两个不同的表中查询


PHP MySQL Query from two different tables based on result from first query

我有一个标准的选择查询,从products表中选择所有记录,然后将其显示在表中。我需要根据结果运行另一个查询,以便从customers表中获得a字段。

比如说。我从products表中的查询显示了user_id字段。我需要同时运行一个SQL查询,以便从customers表中获得user_name

$result = mysqli_query($con,"SELECT * FROM products (SELECT eu_name FROM customer WHERE $id_mobile = $id_mobile");
echo "<table border='1'>
<tr>
<th>user_id</th>
<th>user_name</th>
<th>selected_product</th>

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
echo "<td>" . $row['eu_name'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['selected_product'] . "</td>";

使用给定的表结构

customers(user_id, user_name, user_email) 
products (product_id, product_name, user_id)

您可以获得与客户相关的所有产品

select p.product_id,p.product_name,u.user_name,u.user_email
from products p
inner join customers u on u.user_id = p.user_id

您可以为任何额外的数据过滤器添加where条件。