[英]PHP MySQL Query from two different tables based on result from first query
我有一個標准的選擇查詢,該查詢從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條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.