简体   繁体   中英

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

I have a standard select query which selects all records from the products table and then display it in a table. I need another query to run after that based on the result, in order to get the a field from the customers table.

So for example. my query from the products table shows the user_id field. I need to run a SQL query simultaneously in order to get the user_name from the customers table.

$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>";

With the given table structure

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

You can get all the products associated with customer as

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

You can add where condition for any additional filter of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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