简体   繁体   中英

inner join gives back multiple values

I have products, All of these products have for example. An id, name and price. All of these products are connected in the database like this:

Since I don't have 10 reputation... Here is a link to a picture http://puu.sh/oS95c/b7b5b17427.png

What I want to achieve is to have products that are connected to another product show up on the screen using inner join. However instead of getting the connected items back I get back every item of the right column even if they are not connected.

Here is a link to get a better view of the database: http://puu.sh/oSBF2/1af1ce3751.png

$sql = "SELECT   AFBEELDING_KLEIN, PRODUCTNAAM, PRIJS
    FROM PRODUCT
    inner JOIN PRODUCT_GERELATEERD_PRODUCT
    ON PRODUCT.PRODUCTNUMMER=PRODUCT_GERELATEERD_PRODUCT.PRODUCTNUMMER_GERELATEERD_PRODUCT";

$result = sqlsrv_query($db, $sql);
$data = sqlsrv_fetch_array($result);

while($data = sqlsrv_fetch_array($result)) {
    $big_picture = '<img src="../' . $data["AFBEELDING_KLEIN"] . '"' . 'alt="product">';
    $link = '<a href="../productpaginas/' . $data["PRODUCTNAAM"] . '.php"<p>&nbsp;' . $data["PRODUCTNAAM"] . '</p></a>';
    $price = '<h2>&nbsp; &#8364;' . $data["PRIJS"] . '</h2>';

    echo '<div class="product">';
    echo $big_picture;
    echo $link;
    echo $price;
    echo '</div>';
}

Since you immune to questions and don't provide more information, we can only guess.

You might want am:n link back to the product table itself.

SELECT
  p1.PRODUCTNUMMER, p2.AFBEELDING_KLEIN, p2.PRODUCTNAAM, p2.PRIJS
FROM
  PRODUCT AS p1
INNER JOIN
  PRODUCT_GERELATEERD_PRODUCT AS pgp
ON
  p1.PRODUCTNUMMER = pgp.PRODUCTNUMMER
INNER JOIN
  PRODUCT AS p2
ON
  pgp.PRODUCTNUMMER_GERELATEERD_PRODUCT = p2.PRODUCTNUMMER
;

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