0

我非常感谢您为我提供的任何反馈和时间。

我正在开发一个 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

我想根据我的 Query A Product 的 ID 输出结果:

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.

关于如何构建它的任何想法?

提前致谢!

到目前为止我尝试过的代码(我目前尝试从两个表中获取它们,但最初它们在一个表中!!!)

$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>";
        }

更新:

这有效,但 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
4

1 回答 1

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;");
于 2012-05-16T17:07:24.710 回答