我为我们携带的所有部件创建了一个包含所有车辆,制造和发动机的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>";
答案 0 :(得分:1)
GROUP BY语句与聚合函数结合使用,可以按一列或多列对结果集进行分组。
SELECT column_name,aggregate_function(column_name)FROM table_name WHERE column_name运算符值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
REF:link
答案 1 :(得分: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;
答案 2 :(得分:0)
答案的组合解决了我的问题。我将它从GROUP BY更改为DISTINCT并在我的变量周围添加了一些单引号并且它有效!感谢大家的帮助!回顾过去,两个答案都来自Michael Berkowski。谢谢迈克尔!