2012-05-16 84 views
0

我真的很感謝您對我的任何反饋和時間。PHP MYSQL - 多重查詢和輸出

我工作的一個PHP頁面查詢數據庫配1臺,好像被卡住。 SQL不是我的強項,所以我尋求一些幫助。我正在使用附帶MYSQL的PHP​​工作。

數據庫

我有一個表格「產品」包含:

1ID, 2Product Description, 3Price, 4Category, 5Size, 6Price-Category 

我想運行一個查詢,以確定哪些產品匹配的尺寸(精確匹配)和價格 - 類別(等於或高於)。所以我想到了以下過程:

A - run queries to identify different products by Category (one for Category A, one for Category B) 
B - compare query A and B by Size and Price-Category 
C - Display Results by ID for Category A 

我想每個輸出我的查詢的ID A產品結果:

1ID|2Product Description|3Price|4Category|5Size|6Price-Category|7Query B Details 
1 Product    100 Cat  100 1    ID, Product. etc. 
                   ID, Product. etc. 
                   ID, Product. etc. 

2 Product    200 Cat  100 2    ID, Product. etc. 
                   ID, Product. etc. 
                   ID, Product. etc. 

我如何可以建立任何想法?

在此先感謝!

代碼我試過至今(我目前試圖從兩個表中得到他們,但originnaly他們是在一個!!!!)

$query = "SELECT betten.m3, betten.Shortcode, betten.CodePDT, betten.PVCons, betten.CAT, betten.size1, betten.size2, betten.m3, betten.SIZE, betten.PRICERANGE, som.CodePDT2, som.Shortcode2, som.PRICECLASS, som.PVCons2, som.CAT2, som.size1a, som.size2a, som.m3a, som.SIZETWO FROM betten LEFT JOIN som ON betten.m3 = som.m3a ORDER BY CodePDT "; 
     $result = mysql_query($query) or die(mysql_error()); 
     // Print out the contents of each row into a table 
     while($row = mysql_fetch_array($result)){ 
      echo "<tr><td>"; 
      echo $row['CodePDT']. "</td><td>".$row['Shortcode']. "</td><td>". $row['PVCons']."</td><td>". $row['CAT']. "</td><td>". $row['SIZE']. "</td><td>". $row['m3']."</td><td>". $row['PRICERANGE']. "</td><td>". $row['CodePDT2']. "</td><td>". $row['Shortcode2']. "</td><td>". $row['PVCons2']. "</td><td>". $row['CAT2']. "</td><td>". $row['SIZETWO']. "</td><td>". $row['m3a']. "</td><td>". $row['PRICECLASS']. "</td><td>"; 
      echo "</td></tr>"; 
     } 

UPDATE:

這工作,節選了GROUP_CONT 。沒有,我得到的結果,但一行一行。隨着GROUP_CONT我得到一個錯誤「檢查對應於你的MySQL服務器版本使用附近的「GROUP_CONCAT(som.S正確的語法手冊......」

SELECT `betten`.*, `som`.* 
FROM betten 
    LEFT JOIN som ON betten.m3 = som.m3a  
    WHERE SIZE LIKE SIZETWO  
GROUP_CONCAT(som.Shortcode2) AS Dude 
    GROUP BY betten.CodePDT  
    ORDER BY CodePDT ASC 
+2

PLZ粘貼一些代碼 –

+0

是普萊舍秀什麼ü試過......可能是我們將引導你.... –

+0

@Mian_Khurram_Ijaz:「PLZ」不上所以使用語聊,請複製粘貼一些代碼。 –

回答

0

通過以下實現:

$query = ("SELECT 
    r.rangename as rRangename, /* Get Ranges from Rangetable */ 
    r.rangevalue as rRangevalue, 
    m.name as mName, /* Get Fields from main Products */ 
    m.Code as mCode, 
    m.Range as mRange, 
    SUBSTRING_INDEX(GROUP_CONCAT(f.productcode ORDER BY f.index_id ASC SEPARATOR ','),',',3) as fCodes, /* Get Fields from Cross Selling and list them seperatly */ 
    SUBSTRING_INDEX(GROUP_CONCAT(f.name ORDER BY f.index_id ASC SEPARATOR ','),',',3) as fNames, 
    SUBSTRING_INDEX(GROUP_CONCAT(f.range ORDER BY f.index_id ASC SEPARATOR ','),',',3) as fRanges 

FROM 
    crossproducts f 
JOIN /* Match main products with cross via m3 */ 
    mainproduct m 
ON 
    m.m3 = f.m3 
JOIN /* Match ranges and assign values for cross */ 
    ranges r 
ON 
    r.rangename = f.range 
JOIN /* Match ranges and assign values for main */ 
    ranges s 
ON 
    s.rangename = m.Range 
WHERE /* Only get Items that are equal or higher price range */ 
    r.rangevalue - s.rangevalue > -1 

GROUP BY /* And finally group them by main Product Code */ 
    m.Code;");