B
B
Bearax2020-12-06 12:52:43
MySQL
Bearax, 2020-12-06 12:52:43

How to retrieve data from the database by comparing the result?

Good afternoon. I have such a task, perhaps easy for you, but difficult for me at the moment. There is a database with tables. Let's say there are tables: product_description and product_article . Inside which there are data of goods from the site, in both tables there is a line - product ID they are the same in both tables and are tied to a specific product. The task is this: You need to display data from the name row from the product_description table and the price row from the product_article table . At the same time, so that when displaying price , it finds exactly that name by the product ID columnand assigned to him.

Example: We have a product NIKE T-shirt with a price of 4000 rubles. The NIKE T-shirt here is the name from product_description, and 4000 rubles here is the price from product_article. This product has a common product ID equal to 100. You need to send an SQL query so that by comparing the product ID it displays the name of the product and exactly its price next to it.

Here's what I have now:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
require_once 'config.php'; // подключаем скрипт
 
$link = mysqli_connect($host, $user, $password, $database) 
    or die("Ошибка " . mysqli_error($link)); 
     
$query ="SELECT oc_product_description.name, oc_product.price FROM oc_product_description, oc_product";
 
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link)); 
if($result)
{
    $rows = mysqli_num_rows($result); // количество полученных строк
     
    echo "<table><tr><th>Имя товара</th><th>МРЦ</th><th>Цена в нашем магазине</th></tr>";
    for ($i = 0 ; $i < $rows ; ++$i)
    {
        $row = mysqli_fetch_row($result);
        echo "<tr>";
            for ($j = 0 ; $j < 3 ; ++$j) echo "<td>$row[$j]</td>";
        echo "</tr>";
    }
    echo "</table>";
     
    // очищаем результат
    mysqli_free_result($result);
}
 
mysqli_close($link);
?>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
idShura, 2020-12-06
@idShura

select pd.product_id, 
        pd.name 
        pa.price
   from product_description  pd
        left join product_article pa on pa.product_id = pd.product_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question