我有一个标准的选择查询,该查询从products表中选择所有记录,然后在表中显示它。
我需要基于结果在此之后运行另一个查询,以便从customers表中获取字段。

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

$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条件。

10-05 20:28
查看更多