可能吗?按 Visit.VstMobile 分组,具有 ( (count(Visit.VstMobile)) >1 多个选项
Is it possible? group by Visit.VstMobile having ( (count(Visit.VstMobile)) >1 for multiple options
让我知道这在 mysql 查询中是否可行:
我在 Whosebug 中提到了更多选项。
分组 Visit.VstMobile
具有 ( (count(Visit.VstMobile)>=0 和 count(Visit.VstMobile)<=10) 或 (count(Visit.VstMobile)>=10 和 count(Visit.VstMobile)< =20))
我有一个范围 selection 和多个 select 比如:
[0-10]
[10-20]
[20+]
我可以在上面的下拉列表中 select 多个选项。
基于这个 post 值,我必须在 having 子句中形成查询。如果还有其他选择,请告诉我。
这是我的查询:
select Distinct(Visit.VstMobile) as mobile, count(VstMobile) as cnt from Visit
inner join centertemp on Visit.RegistrationCentreCode=centertemp.SysNo
inner join patient on Visit.VstPatCode=patient.Patcode
inner join result on result.TrJobCode=Visit.VstCode
inner join Test on Test.TestCode=result.TrTestCode
inner join Param on Param.ParamCode=result.TrParamCode
----[some where conditions]----
group by Visit.VstMobile
having ( (count(Visit.VstMobile)>=0 and count(Visit.VstMobile)<=10) or (count(Visit.VstMobile)>=10 and count(Visit.VstMobile)<=20))
-- 没有返回一行。
根据您的 fiddle,我了解到范围的数量可能会有所不同。因此 having 子句中的条件数量可能会有所不同。
因此,最简单的选择是根据用户输入在代码中动态编写查询并执行查询。这是一个可能的解决方案:
<?php
$range1 = "2-5"; //get from user
$range2 = "5-10"; //get from user
$range3 = ""; //get from user
$sql = "SELECT VstMobile, count(VstMobile) as cnt FROM Venkatesh group by VstMobile ";
//all ranges are optional, hence check if we need having clause
if($range1 != "" or $range2 == "" or $range3 ==""){
$sql = $sql . " having ("
if($range1 != ""){
$val1 = explode('-',$range1);
$sql = $sql . " (count(VstMobile) >=". $val1[0] . "and count(VstMobile) <= ".$val1[1].")";
}
if($range2 != ""){
$val2 = explode('-',$range2);
//check previous condition string exists before adding 'or' condition
if($range1 != ""){
$sql .= " or ";
}
$sql = $sql . " (count(VstMobile) >=". $val2[0] . "and count(VstMobile) <= ".$val2[1].")";
}
if($range3 != ""){
$val3 = explode('-',$range3);
//check previous condition string exists before adding 'or' condition
if($range2 != "" or $range1 != ""){
$sql .= " or ";
}
$sql = $sql . " (count(VstMobile) >=". $val3[0] . "and count(VstMobile) <= ".$val3[1].")";
}
$sql = $sql . " )"
}
$result = $conn->query($sql);
?>
我不太擅长php,但是既然你标记了php,我就尝试了,所以如果有语法错误请原谅,我希望你能理解它的基本思想。
来自德鲁的编辑
CREATE TABLE Venkatesh (VstMobile varchar(10));
INSERT INTO Venkatesh (`VstMobile`) VALUES ('1234567890'),('1234567890'),('1223345547'),('1223345547');
('9876543210'),('9876543210'),('1223345547'),('1223345547');
('9876543210'),('9876543210'),('9876543210'),('9876543201');
('9876543201'),('9876543201');
// select vstmobile,count(*)
// from Venkatesh
// group by vstmobile
SELECT VstMobile,
count(VstMobile) as cnt
FROM Venkatesh
group by VstMobile
having (
(count(VstMobile)>=0 and count(VstMobile) <=10)
or (count(Vstmobile)>=10 and count(VstMobile)<=20)
or (count(Vstmobile)>=20));
-- counts for all
SELECT VstMobile,
count(VstMobile) as cnt
FROM Venkatesh
group by VstMobile
having (
(count(VstMobile)>=0 and count(VstMobile) <=10)
or (count(Vstmobile)>=20));
-- counts for all
SELECT VstMobile,
count(VstMobile) as cnt
FROM Venkatesh
group by VstMobile
having (
(count(Vstmobile)>=20));
-- 0 rows
效果很好有什么问题?
让我知道这在 mysql 查询中是否可行: 我在 Whosebug 中提到了更多选项。
分组 Visit.VstMobile 具有 ( (count(Visit.VstMobile)>=0 和 count(Visit.VstMobile)<=10) 或 (count(Visit.VstMobile)>=10 和 count(Visit.VstMobile)< =20))
我有一个范围 selection 和多个 select 比如:
[0-10]
[10-20]
[20+]
我可以在上面的下拉列表中 select 多个选项。
基于这个 post 值,我必须在 having 子句中形成查询。如果还有其他选择,请告诉我。
这是我的查询:
select Distinct(Visit.VstMobile) as mobile, count(VstMobile) as cnt from Visit
inner join centertemp on Visit.RegistrationCentreCode=centertemp.SysNo
inner join patient on Visit.VstPatCode=patient.Patcode
inner join result on result.TrJobCode=Visit.VstCode
inner join Test on Test.TestCode=result.TrTestCode
inner join Param on Param.ParamCode=result.TrParamCode
----[some where conditions]----
group by Visit.VstMobile
having ( (count(Visit.VstMobile)>=0 and count(Visit.VstMobile)<=10) or (count(Visit.VstMobile)>=10 and count(Visit.VstMobile)<=20))
-- 没有返回一行。
根据您的 fiddle,我了解到范围的数量可能会有所不同。因此 having 子句中的条件数量可能会有所不同。 因此,最简单的选择是根据用户输入在代码中动态编写查询并执行查询。这是一个可能的解决方案:
<?php
$range1 = "2-5"; //get from user
$range2 = "5-10"; //get from user
$range3 = ""; //get from user
$sql = "SELECT VstMobile, count(VstMobile) as cnt FROM Venkatesh group by VstMobile ";
//all ranges are optional, hence check if we need having clause
if($range1 != "" or $range2 == "" or $range3 ==""){
$sql = $sql . " having ("
if($range1 != ""){
$val1 = explode('-',$range1);
$sql = $sql . " (count(VstMobile) >=". $val1[0] . "and count(VstMobile) <= ".$val1[1].")";
}
if($range2 != ""){
$val2 = explode('-',$range2);
//check previous condition string exists before adding 'or' condition
if($range1 != ""){
$sql .= " or ";
}
$sql = $sql . " (count(VstMobile) >=". $val2[0] . "and count(VstMobile) <= ".$val2[1].")";
}
if($range3 != ""){
$val3 = explode('-',$range3);
//check previous condition string exists before adding 'or' condition
if($range2 != "" or $range1 != ""){
$sql .= " or ";
}
$sql = $sql . " (count(VstMobile) >=". $val3[0] . "and count(VstMobile) <= ".$val3[1].")";
}
$sql = $sql . " )"
}
$result = $conn->query($sql);
?>
我不太擅长php,但是既然你标记了php,我就尝试了,所以如果有语法错误请原谅,我希望你能理解它的基本思想。
来自德鲁的编辑
CREATE TABLE Venkatesh (VstMobile varchar(10));
INSERT INTO Venkatesh (`VstMobile`) VALUES ('1234567890'),('1234567890'),('1223345547'),('1223345547');
('9876543210'),('9876543210'),('1223345547'),('1223345547');
('9876543210'),('9876543210'),('9876543210'),('9876543201');
('9876543201'),('9876543201');
// select vstmobile,count(*)
// from Venkatesh
// group by vstmobile
SELECT VstMobile,
count(VstMobile) as cnt
FROM Venkatesh
group by VstMobile
having (
(count(VstMobile)>=0 and count(VstMobile) <=10)
or (count(Vstmobile)>=10 and count(VstMobile)<=20)
or (count(Vstmobile)>=20));
-- counts for all
SELECT VstMobile,
count(VstMobile) as cnt
FROM Venkatesh
group by VstMobile
having (
(count(VstMobile)>=0 and count(VstMobile) <=10)
or (count(Vstmobile)>=20));
-- counts for all
SELECT VstMobile,
count(VstMobile) as cnt
FROM Venkatesh
group by VstMobile
having (
(count(Vstmobile)>=20));
-- 0 rows
效果很好有什么问题?