繁体   English   中英

计算两种数据类型之间MYSQL中的字段数

[英]Calculate no of fields in MYSQL between two data types

好奇怪的问题。

表由多个字段组成。 一些具有int(5)数据类型,其余的具有int(11)数据类型

所以让我们继续...

id =>int(5)
var1 =>int(11)
var2 =>int(11)
var3 =>int(11)
var4 =>int(11)
var5 =>int(5)
var6 =>int(11)
var7 =>int(11)
var8 =>int(11)

我如何计算PHP BETWEEN id(int(5))和var(int(5))中的字段我需要返回使用php之间的字段中的值...但我卡住了。 糟糕的桌子设计并没有帮助...但我坚持下去。

完整的场景是我需要创建一个if语句,它输出int(5)字段的名称和数据,如果它与下一个int(5)之间的任何字段包含数据

对不起...丑陋我知道!!!

如果到目前为止运行

    $sql2 = 'SHOW COLUMNS from services2';
    $result2 = getDBResults($sql2, $level, $testing);

    $total = count($result2);


    $i=2;
    while ($i<($total-1))
    {
        $data = $result2[$i]['Field'];

        if ($result2[$i][1]=='int(5)')
        {
            $html .= '<h2>'.preg_replace('/(?<!\ )[A-Z]/', ' $0', $result2[$i]['Field']).'</h2>';
        }
        else
        {   
            ];
            // "$result - This is a seperate query run previously to get the row
            if ($result[0][$data] == 1)
            {
                $html .= '<p>'.preg_replace('/(?<!\ )[A-Z]/', ' $0', $data).'</p>';
            }
        }
        $i++;
    }

我没有机会对它进行实际测试,所以如果你需要稍微调整一下来平滑掉任何粗糙的边缘,就不要打败我。

但它是一个相当标准的,如果不是非常简单的,处理列名称的过程在循环中产生数组,只记住变量中的最后一个int(5)列名,这样你就可以使用它,当且仅当你有在查看下一个int(5)列类型之前,在任何int(11)列类型中找到了一些数据。

现在我明白为什么你在解释你想要的东西时遇到了麻烦,我在描述解决方案时遇到了同样的麻烦。

无论如何,看看这个代码,看看它是否给你任何意义。

$sql2 = 'SHOW COLUMNS from services2';
$columns = getDBResults($sql2, $level, $testing);

$total = count($columns);

$print_column = null;
$found_data = false;

foreach ( $columns as $idx => $column ) {
    // skip first 2 columns
    if ($idx < 3 ) { continue; }

    // first time thru loop so remember the first int5 column name we saw
    if ( $column['Type'] == 'int(5)' && empty($print_column) ) {
        $print_column = $column['Field'];
        $found_data   = false;
        continue;
    }

    // found the next int5 column, do we need to print anything
    if ( $column['Type'] == 'int(5)' && $found_data ) {

        // This is where you would do any output creation, 
        // I just kept it simple.
        echo $result[0][$print_column];

        // reset state and the next field that might be printed.
        $found_data = false;
        $print_column = $column['Field'];
        continue;
    }

    // This may not need to be an if, but just in case you have other field types
    // that you are not interested in processing I made it an if.
    if ( $column['Type'] == 'int(11)' && ! empty($result[0][$column['Field']]) ) {
        $found_data = true;
    }

} // endforeach

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM