MySql如何在if语句中结合2个查询

时间:2013-07-30 20:01:08

标签: mysql select if-statement union

这里是我的表单和mysql / php代码。

如果同时检查了2个复选框,则mysql不会执行查询。 有没有办法让这个表单正常工作?

的MySQL

 if (isset($_POST['A']) && !empty($_POST['A'])) {
 $query= "SELECT * FROM (SELECT * FROM myTable WHERE...) as A WHERE Column1='y'";
 }
 if (isset($_POST['B']) && !empty($_POST['B'])) {
 $query= "SELECT * FROM (SELECT * FROM myTable WHERE...) as B WHERE Column2='y'";
 }

表格

<form id="myform1" class="myform1" method="post" name="myform1">
<input type="checkbox" name="A[]" id="A" value="y" />
<input type="checkbox" name="B[]" id="B" value="y" />
<input id="submit" type="submit" name="submit" value="Submit" 
onclick="return submitForm1()" />
</form>

任何帮助表示赞赏。谢谢

1 个答案:

答案 0 :(得分:0)

只是一个建议,它丑陋而简单:

$teamsters = ''; 
$query = '';
if (isset($_POST['A'][0]) && !empty($_POST['A'][0])) {
   $query= "SELECT * FROM (SELECT * FROM myTable WHERE...) as A WHERE Column1='y'";
   $teamsters = ' UNION ';
}
if (isset($_POST['B'][0]) && !empty($_POST['B'][0])) {
   $query .= $teamsters . "SELECT * FROM (SELECT * FROM myTable WHERE...) as B WHERE Column2='y'";
}