show 2 table in 1 using inner join query

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



show 2 table in 1 using inner join query



Hey guys i want to show a table so that table have 2 foreign keys.My food and restaurant table have name column.i tried following codes but i dont know how to show restaurant name and food name different.


$sql_select_food = "SELECT food.id,restaurant.name,food.name,food.restaurant_id,foodcategory.title from food INNER join foodcategory ON food.foodcategory_id=foodcategory.id INNER join restaurant ON food.restaurant_id=restaurant.id WHERE food.isActive = 1 ORDER BY food.updateDate DESC";
$result_select_food = mysqli_query($connection, $sql_select_food);
if (mysqli_num_rows($result_select_food) > 0)
while ($row_select_food = mysqli_fetch_assoc($result_select_food))
echo "
<tr>
<td>$row_select_food[name]</td>
<td>$row_select_food[name]</td>
<td>$row_select_food[title]</td>
</tr>
";





2 Answers
2



Use column aliases:


SELECT food.id AS food_id, restaurant.name AS restaurant_name, food.name AS food_name ...



Then


$row_select_food['restaurant_name']


$row_select_food['food_name']



Note: AS keyword is optional.


AS





thx it works well.
– emen
Aug 13 at 8:20



Put an alias to the column name and call it later in the code as a column name like :


$sql_select_food = "SELECT food.id,restaurant.name as rname, food.name as fname, food.restaurant_id,foodcategory.title from food INNER join foodcategory ON food.foodcategory_id=foodcategory.id INNER join restaurant ON food.restaurant_id=restaurant.id WHERE food.isActive = 1 ORDER BY food.updateDate DESC";
$result_select_food = mysqli_query($connection, $sql_select_food);
if (mysqli_num_rows($result_select_food) > 0)
while ($row_select_food = mysqli_fetch_assoc($result_select_food))
echo "
<tr>
<td>$row_select_food[rname]</td>
<td>$row_select_food[fname]</td>
<td>$row_select_food[title]</td>
</tr>
";







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard