计算MySQL中的特定输入

时间:2015-12-11 01:00:15

标签: php mysql mysqli

我的数据库看起来像这样:

s = input("Please enter a word: ")
s = s.lower
Pal = True
def recpal(s):
    if Pal == False:
        return(False)
    else:
        return(True)

def main():
    y = recpal(s)
    first_char = 0
    last_char = -1
    if s[first_char] != s[last_char]:
        print("It is not a Palindrome")
    i = 0
    while i >= n[-1]:
        first_char = first_char + 1
        last_char = last_char -1</i>





main()

如何单独选择和计算这些输入?

我想将它们保存在以下变量中:

Column id q1 q2 q3 q4 q5 q6
Row     2  x  x  y  x  y  z
Row     3  y  y  z  z  y  x

我更喜欢SQL查询只是为了全部选择它们,并在php中计算它们,但如果这使事情变得复杂请告诉我。

提前致谢。

2 个答案:

答案 0 :(得分:0)

试试这个

$sql = "SELECT * FROM table";
$result = mysqli_query($conn, $sql);

$count = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result)) {
        $id[]=$row['id'];
        $q1[]=$row['q1'];
        $q2[]=$row['q2'];
        $q3[]=$row['q3'];
        $q4[]=$row['q4'];
        $q5[]=$row['q5'];
        $q6[]=$row['q6'];
}

答案 1 :(得分:0)

您有两种选择:

  1. 使用php添加功能

    $变量++;

  2. 查询自身,创建结果

  3. 选项1:

    这只是第一个问题,我会更新tommorow(添加其他问题),如果sql查询不是你的第一选择,你不能自己添加其他问题。

    $sql = "SELECT * FROM table";
    $result = mysqli_query($conn, $sql);
    $x=0;$y=0;$z=0;
    
    $count = mysqli_num_rows($result);
    while($row = mysqli_fetch_assoc($result)) {
            if ($row['q1'] == 'x')
             {
              $x++;
             }
            elsif ($row['q1'] == 'y')
            {
             $y++;
            }
            else ($row['q1'] == 'z')
            {
             $z++;
            }
    
    }
    

    选项2:

    只有sql的解决方案可以尝试here

    select answer, sum(ln+ln2+ln3+ln4+ln5+ln6) as count from (
    
      SELECT answer,counter1 AS ln ,'' ln2, '' ln3,'' AS ln4,'' AS ln5, '' AS  ln6
        FROM 
        (SELECT q1 as answer, count(*) as Counter1 
         FROM `answers`
         GROUP BY `q1`) AS tbl
    
     union   
    
    SELECT answer,'' ln,counter2 AS ln2 , '' ln3,'' AS ln4,'' AS ln5, '' AS  ln6
        FROM 
        (SELECT q2 as answer, count(*) as Counter2 
         FROM `answers`
         GROUP BY `q2`) AS tbl 
    
     union   
    
    SELECT answer,'' ln,'' AS ln2 ,counter3 as  ln3,'' AS ln4,'' AS ln5, '' AS  ln6
        FROM 
        (SELECT q3 as answer, count(*) as Counter3 
         FROM `answers`
         GROUP BY `q3`) AS tbl
    
     union   
    
    SELECT answer,'' ln,'' AS ln2 ,'' AS ln3 ,counter4 as  ln4,'' AS ln5, '' AS  ln6
        FROM 
        (SELECT q4 as answer, count(*) as Counter4 
         FROM `answers`
         GROUP BY `q4`) AS tbl  
    
    union
    
    SELECT answer,'' ln,'' AS ln2 ,'' AS ln3 ,'' AS ln4, counter5 as  ln5, '' AS  ln6
        FROM 
        (SELECT q5 as answer, count(*) as Counter5 
         FROM `answers`
         GROUP BY `q5`) AS tbl 
    
    union
    
    SELECT answer,'' ln,'' AS ln2 ,'' AS ln3 ,'' AS ln4, '' AS  ln5, counter6 as  ln6
        FROM 
        (SELECT q6 as answer, count(*) as Counter6
         FROM `answers`
         GROUP BY `q6`) AS tbl 
                                 ) 
    
    
    
    
         a group by a.answer