0

我为我们携带的所有零件创建了一个包含所有车辆、品牌和引擎的 MySQL 表。我正在尝试仅使用 PHP 来制作级联下拉搜索功能。我的脚本在第二个查询中失败;

SELECT model FROM veh_types WHERE make = $vehmakeA GROUP BY model

我猜它在 GROUP BY 子句上失败了。

有人可以指出我正确的方向吗?

谢谢!

编辑 ------------- 这是我的更多代码。

echo "<p>VEHICLE MAKE"; 
    echo "<select name=\"vehmake\" id=\"vehmake\" onchange=\"this.form.submit();\">";
    echo "<option value=\"--\">SELECT VEHICLE MAKE...</option>";
    echo "<option value=\"--\">SELECT VEHICLE MAKE 2...</option>";

    $queryModel = "SELECT make, model FROM veh_types WHERE make = $vehmakeA GROUP BY model";
    $resultModel = mysql_query($queryModel) or die(mysql_error());
    while($rowModel = mysql_fetch_array($resultModel)) {
        $vehmodel = $rowModel['model'];
        echo "<option value=\"$vehmodel\">$vehmodel</option>\n";    
    }

    echo "</select></p>";
4

3 回答 3

1

GROUP BY 语句与聚合函数一起使用,以按一列或多列对结果集进行分组。

SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name

聚合(GROUP BY)函数

Name    Description

AVG()   Return the average value of the argument
BIT_AND()   Return bitwise and
BIT_OR()    Return bitwise or
BIT_XOR()   Return bitwise xor
COUNT(DISTINCT) Return the count of a number of different values
COUNT() Return a count of the number of rows returned
GROUP_CONCAT()  Return a concatenated string
MAX()   Return the maximum value
MIN()   Return the minimum value
STD()   Return the population standard deviation
STDDEV_POP()    Return the population standard deviation
STDDEV_SAMP()   Return the sample standard deviation
STDDEV()    Return the population standard deviation
SUM()   Return the sum
VAR_POP()   Return the population standard variance
VAR_SAMP()  Return the sample variance
VARIANCE()  Return the population standard variance

参考:链接

于 2013-02-05T03:46:50.300 回答
0

我只是把我认为你的表的样子放在一起,这个查询对我有用:

    CREATE TABLE IF NOT EXISTS `veh_types` (
      `id` int(10) NOT NULL auto_increment,
      `make` varchar(100) default '0',
      `model` varchar(100) default '0',
      `submodel` varchar(100) default '0',
      `type` varchar(100) default '0',
      UNIQUE KEY `id` (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

    INSERT INTO `veh_types` VALUES (1, 'Ford', 'Ranger', 'EX', 'Truck');
    INSERT INTO `veh_types` VALUES (2, 'Ford', 'Focus', 'EX', 'Car');
    INSERT INTO `veh_types` VALUES (3, 'Honda', 'Civic', 'EX', 'Car');
    INSERT INTO `veh_types` VALUES (4, 'Honda', 'Accord', 'LX', 'Car');
    INSERT INTO `veh_types` VALUES (5, 'Chevy', 'S10', 'LS', 'Truck');

    SELECT model
    FROM veh_types 
    WHERE make = 'Ford'
    GROUP BY model;
于 2013-02-05T04:09:06.587 回答
0

答案的组合解决了我的问题。我将它从 GROUP BY 更改为 DISTINCT 并在我的变量周围添加了一些单引号,它可以工作!!!感谢大家的帮助!回想起来,这两个答案都来自迈克尔·伯科夫斯基。谢谢迈克尔!

于 2013-02-05T05:12:33.497 回答