我希望编写一个查询,根据列中出现的次数过滤输出。例如(房间号1
在room number
列中出现5次。如果出现次数在0-5
之间,房间号5
应该在输出中,如果不是它不应该是输出)
这是我的疑问:
$query = 'SELECT room_no, COUNT(room_no) as count FROM banabas_sr GROUP BY room_no HAVING count BETWEEN 0 AND 6';
但即使表格中的所有房间只出现一次,它也不会输出任何内容。
当我点击此按钮时:
<input type="submit" name="check" value="CHECK AVALIBILITY" class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored" id="magic" />
它应该通过这个信息:
<div class="labels-input room">
<!--Hostel-->
<?php
if ($row['gender'] == 'Male') {
$option1 = 'Banabas';
$option2 = 'Banabas Annex';
}
else {
$option1 = 'Faith';
$option2 = 'Faith Annex';
}?>
<div class="labas"><div class="form-group">
<div class="input-group input-group-sm">
<select class="form-control" name="hostel" required>
<option></option>
<option><?php echo $option1; ?></option>
<option><?php echo $option2; ?></option>
</select>
</div>
</div></div>
<!--Hostel-->
<!--Room Type-->
<div class="labas"><div class="form-group">
<div class="input-group input-group-sm">
<select class="form-control" name="r_type" required>
<option></option>
<option>Executive</option>
<option>Standard</option>
</select>
</div>
</div></div>
<!--Room Type-->
到这个php脚本:
<?php
if (isset($_POST['check'])) {
$hostel = $_POST['hostel'];
$type = $_POST['r_type'];
if ($type == 'Standard') {
$_POST['r_type'] = 'SR';
}
else {
$_POST['r_type'] = 'ER';
}
if ($hostel == 'Banabas' && $type == 'SR') {
$query = 'SELECT room_no, COUNT(room_no) as count FROM banabas_sr GROUP BY room_no HAVING count(room_no) BETWEEN 0 AND 6';
$ex = mysql_query($query);
while ($row = mysql_fetch_array($ex)) {
echo $msg = "<option value='" . $row['room_no'] . "'>" . $row['room_no'] . "</option>";
}
}
}
?>
填充此div:
<div class="labas"><div class="form-group">
<div class="input-group input-group-sm">
<select class="form-control" name="av_rooms">
<?php echo $msg; ?>
</select>
</div>
</div>
答案 0 :(得分:1)
试试这个:
$query = 'SELECT room_no, COUNT(room_no) as count FROM banabas_sr GROUP BY room_no HAVING count(room_no) BETWEEN 0 AND 6';
在group之后的having子句引用了列上的聚合函数,你可以使用max,min,avg和ETC,但你必须在having clasue中计算它,而不是使用select部分中的列。
答案 1 :(得分:1)
COUNT()
始终至少有一个(room_no
不是NULL
时)。您需要另一个房间号码列表找到无与伦比的房间。我假设rooms
表:
SELECT r.room_no, COUNT(room_no) as num
FROM rooms r LEFT JOIN
banabas_sr b
ON r.room_no = b.room_no
GROUP BY r.room_no
HAVING num BETWEEN 0 AND 6;
我不喜欢使用count
作为列的名称,因为它也是函数的名称。