简体   繁体   中英

Mysql SELECT from 2 table

I can't seem to figure out how to make a SELECT statement that group products and displays the products under one product type.

I have a table called floors containing 15 different floor products and a table called floortypes containing 8 different product types (ftid) and a product image. Each of the 15 products have a floortype which define which type it is. Some of the products are almost the same but with different measurements which but it gives the products a different description.

Example:

Product: Product 1 Product type: 1
Product: Product 2 Product type: 1
Product: Product 3 Product type: 2
Product: Product 4 Product type: 2
Product: Product 5 Product type: 3
Product: Product 6 Product type: 4

What I want is to is the products to be displayed under a product group with 1 product image

Producttype 1 image
Product 1 description
Product 2 description 

Producttype 2 image
Product 3 description
Product 4 description  

Producttype 3 image
Product 5 description

Producttype 4 image
Product 6 description

I hope someone is able to help me!

EDIT

I used your first example which works flawlessly. This is my final code:

$sql = SQLHandling::SQLquery("SELECT A.fid, A.floorname, A.desc_dk, A.floortype, B.prodimage, GROUP_CONCAT(A.desc_dk SEPARATOR '|') AS descr, GROUP_CONCAT(A.floorname SEPARATOR '|') AS fname FROM floors A JOIN floortypes B ON A.floortype = B.ftid GROUP BY A.floortype, B.prodimage");

and then my while loop:

$counter = 0;

while($row = mysql_fetch_array($sql)) {

    $delimiter = "|";
    $descr = explode($delimiter, $row["descr"]);
    $fname = explode($delimiter, $row["fname"]);

    $markers["###FLOOR###"] .= '<div style="float: left; width: 200px; height: 425px; margin: 0px 10px 0px 10px; vertical-align: text-top; text-align: left;">';    
    $markers["###FLOOR###"] .= '<p><a href="index.php?page=sfp&room='. $_GET["room"] .'&floor='. $row["floorname"] .'&wall='. $_GET["wall"] .'&envi='. $_GET["envi"] .'&fpanel='. $_GET["fpanel"] .'"><img src="images/floors/'. $row["prodimage"] .'.jpg" width="200" /></a></p>';

    for ($i = 0; $i < count($descr); $i++) { 
        $markers["###FLOOR###"] .= '<p><a href="index.php?page=sfp&room='. $_GET["room"] .'&floor='. $fname[$i] .'&wall='. $_GET["wall"] .'&envi='. $_GET["envi"] .'&fpanel='. $_GET["fpanel"] .'">'. $descr[$i].'</a></p>';
    }

    $markers["###FLOOR###"] .= '</div>';

    if (++$counter % 4 == 0) {
        $markers["###FLOOR###"] .=  '<div style="clear: both;">&nbsp;</div>';
    }
}

gives me exactly what I was looking for!

SELECT A.producttype, B.image, Group_concat(<productid> order by <productid> SEPARATOR ',') Products
FROM   PRODUCT A JOIN producttype B
ON     A.producttype = B.producttype
GROUP  BY A.Producttype, B.image

replace "productid" with the actual column, your output will be

  • producttype 1, image, "product 1,product 2"
  • producttype 2, image, "product 3,product 4"

.... etc

Hope this helps

Maybe you are just looking for this?

SELECT B.ftid, B.prodimage, A.fid, A.floorname, A.desc_dk     
FROM   floors A JOIN floortypes B 
ON     A.floortype = B.ftid

This will give a resultset as follows,

producttype1, pt1image, product1 id, product1 name, product1 descriptin
producttype1, pt1image, product2 id, product2 name, product2 descriptin
producttype2, pt2image, product3 id, product3 name, product3 descriptin
producttype2, pt2image, product4 id, product4 name, product4 descriptin
producttype3, pt3image, product5 id, product5 name, product5 descriptin
producttype4, pt4image, product6 id, product6 name, product6 descriptin

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