簡體   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