从复选框查询

时间:2013-03-01 14:39:09

标签: php mysql sql

我的mysql数据库中有以下表格。我正在尝试创建一个应用程序,可以虚拟评估是否有人可能正在发展食道癌

#tblsymptoms - holds all symptoms
######################################
symptomID  | symptom    
-------------------------------------
1            Mass in the throat        
2            Difficulty in swallowing   
3            Lost weight lately
4            Heartburn
5            Hoarse-sounding cough
6            Vomit blood


#tblresponse - holds all responses
######################################
responseID  | response   
-----------------------------------------------------
1            Your symptoms suggest you have a problem       
2            You may be having Oesophageal carcinoma  
3            You dont have a throat carcinoma


#tblrelation - holds relation between response and symptoms
######################################
relationID  | responseID | symptomID
-----------------------------
1         1          3
2         1          4
3         1          5
4         2          1
5         2          2
6         2          3
7         4          2
8         2          5
9         2          6
10        3          3
11        3          4

我想使用html表单中的复选框来查询数据库,这里是表单的代码:

<form method="post" action="process.php">
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Difficulty in swallowing" id="">Difficulty in swallowing<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Difficulty in swallowing" id="">Difficulty in swallowing<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Lost weight lately" id="">Lost weight lately<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Heartburn" id="">Heartburn<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Hoarse-sounding cough" id="">Hoarse-sounding cough<br>
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Vomit blood" id="">Vomit blood<br>
<input type="submit" name="formSubmit" value="Diagnose me" />
</form>

现在,万一有人选择:Lost weight lately and Heartburn脚本应该从tblResponse&gt;&gt;&gt;&gt; responseID = 1获取数据并显示给用户:Your symptoms suggest you have a problem.

我试过但它返回0值

SELECT tblresponse.response
FROM tblrelation 
   LEFT JOIN tblresponse ON tblrelation.responseID = tblresponse.responseID
WHERE tblrelation.symptomID = '3' AND tblrelation.symptomID = '4' 

任何对代码有所了解的人都应该添加到html表单中,并带有复选框来查询数据库?

3 个答案:

答案 0 :(得分:0)

一列不能有多个值。试试这个:

SELECT tblresponse.response
FROM tblrelation LEFT JOIN
     tblresponse
      ON tblrelation.responseID = tblresponse.responseID
GROUP BY tblresponse.response
having sum(case when tblrelation.symptomID = '3' then 1 else 0 end) > 0 and
       sum(case when tblrelation.symptomID = '4' then 1 else 0 end) > 0

对于给定的响应,它将所有关系组合在一起。 having子句计算的数字为'3',单独计为'4'。如果它们都大于0,则返回响应。

顺便说一句,如果ID是数字,则不需要引号。

答案 1 :(得分:0)

#tblrelation存在设计问题。

听起来嘶哑的咳嗽症状会既会出现“问题”又可能会出现“你可能患有食道癌”。

导致特定响应的症状组合需要在单行上以将关系建立为唯一。

在你做任何其他事情之前解决这个问题,否则你永远不会得到你的结果。

您可以像这样创建表格:

relationID  | responseID | symptomID
-----------------------------
1         1          3, 4, 5

但是当它需要检索它时,这将导致一个丑陋的SQL查询。

如果是我,我想我会将该处理移到应用程序中。您可以评估哪些框被选中以及哪些框未被检查,并且无需将其传递给sql即可找到相应的响应。

答案 2 :(得分:0)

试试这个:

SELECT response from tblresponse where responseID = (
SELECT responseID from tblrelation 
 where symptomID=4 or symptomID=3
 group by responseID
 having count(*) = 2
)

你还需要传递全部计数(所选值的数量),在这种情况下我用2等于(选择的选项)

DEMO

编辑:3,4和5

SELECT * from tblresponse where responseID = (
SELECT responseID from tblrelation 
 where symptomID=4 or symptomID=3 or symptomID=5
 group by responseID
 having count(*) = 3
)

DEMO2