Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Bagaimana cara mengekstrak hanya kolom yang memiliki nilai bukan nol di mysql dan php?

Coba ini:

$result = mysqli_query($conn, "SELECT * FROM $dbname.statusinfo WHERE soid = '$userinput1' AND date_column <> '0000-00-00' ") or die(mysqli_error($conn));

Meskipun, dengan mysql Anda bahkan dapat melakukan ini:

$result = mysqli_query($conn, "SELECT * FROM $dbname.statusinfo WHERE soid = '$userinput1' AND date_column > '0000-00-00' ") or die(mysqli_error($conn));

Semoga membantu

EDIT

Saya dapat melihat apa yang Anda inginkan sekarang setelah Anda mengubah pertanyaan :) Sayangnya saya tidak tahu cara untuk apa yang Anda inginkan menggunakan SQL (seseorang mungkin).

Anda mengeluarkan judul kolom sehingga tidak mengeluarkan kolom tertentu akan menyebabkannya muncul di kolom yang salah sehingga Anda tidak perlu mengeluarkan apa pun di mana waktunya adalah 0000-00-00

Ini adalah bagaimana saya akan melakukannya di PHP. (dan jika saya melewatkan poin Anda lagi, saya mungkin akan menembak diri saya sendiri :))

<?php
    $userinput1 = $_POST['soid'];

    $servername = "localhost";
    $username   = "root";
    $password   = "";
    $dbname     = "status";

    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_errno) {
        printf("Connect failed: %s\n", $conn->connect_error);
        exit();
    }

    $result = mysqli_query($conn, "SELECT * FROM $dbname.statusinfo WHERE soid = '$userinput1'  ") or die(mysqli_error($conn));

    $arrayHeadings = array(
        "dept"                  => "Department", 
        "samplerecived"         => "Sample Recived",
        "molbioextraction"      => "Mol-Bio Extraction",
        "molbioextractionqc"    => "Extraction QC",
        "libraryprep"           => "Library Prep",
        "libraryqc"             => "Library QC",
        "sequencing"            => "Sequencing",
        "datacheck"             => "Data Check",
        "resequencing"          => "RE Sequencing",
        "qccheck"               => "QC Check",
        "analysisstarted"       => "Analysis Started",
        "analysiscompleted"     => "Analysis Completed",
        "report"                => "Report",
        "outbound"              => "Outbound",
    );

?>
<style>
    th{
        color: blue;
    }

    td{
        color: black;
    }
</style>

<table border='1'>
    <tr>
        <?php foreach($arrayHeadings as $key => $name): ?>
            <th><?= $name; ?></th>
        <?php endforeach; ?>
    </tr>
    <tr>
        <?php while($row = mysqli_fetch_assoc($result)): ?>
            <?php foreach($arrayHeadings as $key => $name): ?>
                <?php if($row[$key] != "0000-00-00"): ?>
                    <td><?= $row[$key]; ?></td>
                <?php else: ?>
                    <td></td>
                <?php endif; ?>
            <?php endforeach; ?>
        <?php endwhile; ?>
    </tr>
</table>

EDIT

Judul tabel tidak dikeluarkan jika bidang berisi 0000-00-00. Ini hanya bergantung pada satu elemen yang dikeluarkan pada satu waktu.

<?php
    $userinput1 = $_POST['soid'];

    $servername = "localhost";
    $username   = "root";
    $password   = "";
    $dbname     = "status";

    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_errno) {
        printf("Connect failed: %s\n", $conn->connect_error);
        exit();
    }

    $result = mysqli_query($conn, "SELECT * FROM $dbname.statusinfo WHERE soid = '$userinput1'  ") or die(mysqli_error($conn));

    $arrayHeadings = array(
        "dept"                  => "Department", 
        "samplerecived"         => "Sample Recived",
        "molbioextraction"      => "Mol-Bio Extraction",
        "molbioextractionqc"    => "Extraction QC",
        "libraryprep"           => "Library Prep",
        "libraryqc"             => "Library QC",
        "sequencing"            => "Sequencing",
        "datacheck"             => "Data Check",
        "resequencing"          => "RE Sequencing",
        "qccheck"               => "QC Check",
        "analysisstarted"       => "Analysis Started",
        "analysiscompleted"     => "Analysis Completed",
        "report"                => "Report",
        "outbound"              => "Outbound",
    );

?>
<style>
    th{
        color: blue;
    }

    td{
        color: black;
    }
</style>

<table border='1'>
    <tr>
        <?php foreach($arrayHeadings as $key => $name): ?>
            <?php if($row[$key] != "0000-00-00"): ?>
                <th><?= $name; ?></th>
            <?php endif; ?>
        <?php endforeach; ?>
    </tr>
    <tr>
        <?php while($row = mysqli_fetch_assoc($result)): ?>
            <?php foreach($arrayHeadings as $key => $name): ?>
                <?php if($row[$key] != "0000-00-00"): ?>
                    <td><?= $row[$key]; ?></td>
                <?php endif; ?>
            <?php endforeach; ?>
        <?php endwhile; ?>
    </tr>
</table>



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cara mengambil hasil yang tidak cocok di mysql

  2. Cara Membuat Script dari Diagram di MySQL Workbench

  3. Bisakah saya menggunakan batasan kunci asing yang sama di dua tabel berbeda?

  4. MYSQL Pilih dari tabel berdasarkan beberapa baris

  5. Panjang DECIMAL untuk waktu mikro (benar)?