Mysql计数行/在1个查询上一起添加2个不同的结果

时间:2013-07-14 01:15:33

标签: php mysql

我正在使用php myAdmin
表名是productx,我有

---------------------------------------------
Id | gender | brand | category | subcategory|
---------------------------------------------
1  | women  | nike  | shoes    | sandalet   |
2  | women  | nike  | shoes    | nightshoes |
3  | women  | nike  | shoes    | sandalet   |
4  | women  | nike  | shoes    | nightshoes |
5  | women  | puma  | shoes    | sandalet   |
6  | women  | nike  | shoes    | eveningshoes|
7  | women  | puma  | shoes    | sandalet   |
8  | women  | nike  | shoes    | sandalet   |

比我有一个带有复选框的表单,比如这个表单用javascript提交自己

<?php if(isset($_POST['shoes'])): ?>
      <input type="checkbox" name="eveningshoes" value="<?php echo $eveningshoes; ?>"onclick="formSubmit()"<?php if(isset($_POST['eveningshoes'])):?>checked<?php endif;?>><?php echo $eveningshoes; antalSkor_subcategory_eveningshoes($shoes_post_var,$eveningshoes);?><br>
      <input type="checkbox"name="nightshoes" value="<?php echo $nightshoes; ?>"  onclick="formSubmit()"<?php if(isset($_POST['nightshoes'])):?>checked<?php endif;?>><?php echo $nightshoes; antalSkor_subcategory_nightshoes($shoes_post_var,$nightshoes);?><br>
      <input type="checkbox" name="sandalet" value="<?php echo $sandalet; ?>"onclick="formSubmit()"<?php if(isset($_POST['sandalet'])):?>checked<?php endif;?>><?php echo $sandalet; echo antalSkor_subcategory_sandalet($shoes_post_var,$sandalet);?><br>       
<?php endif;?>

如果用户单击带有值sandalet的复选框 gona使用此函数输出结果 对于其他品牌而言,功能是一样的,只想在这里保持简短

function antalSkor_nike($shoes_post_var,$sandalet_post_var,$nightshoes_post_var,$eveningshoes_post_var,$nike) {
        if(isset($shoes_post_var) && isset($sandalet_post_var)){
        //if sandalet checked counting how many NIKE sandalet we have
            $result1=mysql_query("SELECT count(*) from productsx where category='$shoes_post_var ' AND subcategory='$sandalet_post_var' AND brand ='$nike'")or die (mysql_errno());
            $total1 = mysql_result($result1, 0);    
            echo ' '.'('.$total1.')';

        }elseif(isset($shoes_post_var) && isset($nightshoes_post_var)){     
        //if night shoes checked counting how many NIKE night shoes we have
            $result2=mysql_query("SELECT count(*) from productsx where category='$shoes_post_var ' AND subcategory='$nightshoes_post_var' AND brand ='$nike'")or die (mysql_errno());
            $total2 = mysql_result($result2, 0);    
            echo ' '.'('.$total2.')';
        }elseif(isset($shoes_post_var) && isset($eveningshoes_post_var)){       
        //if evening shoes checked counting how many NIKE evening shoes we have
            $result3=mysql_query("SELECT count(*) from productsx where category='$shoes_post_var ' AND subcategory='$$eveningshoes_post_var' AND brand ='$nike'")or die (mysql_errno());
            $total3 = mysql_result($result3, 0);    
            echo ' '.'('.$total3.')';
        }else{
        if(isset($shoes_post_var)){
                $result=mysql_query("SELECT count(*) from productsx where category='$shoes_post_var ' AND brand ='$nike'")or die (mysql_errno());
            $total = mysql_result($result, 0);  
            echo ' '.'('.$total.')';
        }
    }
}

并且结果是

    <input type="checkbox" name="nike"value="<?php echo @$nike; ?>" onclick="formSubmit()"<?php    if(isset($_POST['nike'])):?>checked<?php endif;?>><?php echo @$nike; echo antalSkor_nike($shoes_post_var,$sandalet_post_var,$nightshoes_post_var,$eveningshoes_post_var,$nike);?><br>
       <input type="checkbox" name="puma"value="<?php echo @$puma; ?>" onclick="formSubmit()"<?php    if(isset($_POST['puma'])):?>checked<?php endif;?>><?php echo @$puma; echo antalSkor_puma($shoes_post_var,$sandalet_post_var,$nightshoes_post_var,$eveningshoes_post_var,$puma);?><br>

Sandalet NIKE(3)
如果用户只检查夜棚结果,那就是耐克
Nightshoes NIKE(2)

问题是如果用户同时检查它们

如何使用查询将它们计算到(5)? 感谢

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT COUNT(id) FROM productx WHERE ... WHEREs for brand, gender, and category ... AND subcategory IN ('sandalet', 'nightshoes')

来源:WHERE IN MySQL Clause