【问题标题】:How to find only specific tables and specific column names in my database using mysql query [duplicate]如何使用mysql查询在我的数据库中仅查找特定表和特定列名[重复]
【发布时间】:2015-12-10 01:08:54
【问题描述】:

由于我已经让管理员动态创建表单。当他创建表单时,我所做的是我已经为表单动态创建了表格。一切正常。现在我想显示特定的表格和特定的报告生成列。

我的问题是:

  1. 我不想显示 (userlogin,place,venue) 表用于报告生成和三列 (user_id,user_common_id,ayear) 这三列对于我动态创建的所有表都是通用的。

  2. 基于每年(2013,2014,2015)我想生成报告。我将在生成报告时将表单发送到mysql。我想列出我动态创建的表单。 /p>

  3. 如何为此编写查询。

到目前为止,我已经给出了我的代码,我所做的是在这里:

    <?php
        $mysqli = new mysqli("localhost", "root", "", "event");
        $result = $mysqli->query("SHOW TABLES");
        while ( $row = $result->fetch_row() )
        {
            $table = $row[0];
            echo '<h3>',$table,'</h3>';
            $result1 = $mysqli->query("SELECT * FROM $table where ayear='2014'");
            if($result1) 
            {
                echo '<table cellpadding="0" cellspacing="0" class="db-table">';
                $column = $mysqli->query("SHOW COLUMNS FROM $table");
                echo '<tr>';
                while($row3 = $column->fetch_row() ) 
                {
                    echo '<th>'.$row3[0].'</th>';
                }
                echo '</tr>';
                while($row2 = $result1->fetch_row() ) 
                {
                    echo '<tr>';
                    foreach($row2 as $key=>$value) {
                        echo '<td>',$value,'</td>';
                    }
                    echo '</tr>';
                }
                echo '</table><br />';
            }
        }
        $mysqli->close();
    ?>

这是结果

(待填)

这是预期的结果

id  Date        No_of_Days  Particulars         user_id     user_common_id  ayear
--  --------    ----------  ----------------    --------    --------------  -----
1   20/07/2013  1           Film Show on...     16          311             1
2   29/08/2013  1           Drama on ...        16          318             1
3   08/03/2014  1           Video Conferen..    10          621             1

【问题讨论】:

  • 您可以从数据库中读取架构信息并排除某些表和列。至于#2,一无所知
  • 感谢您提供的信息。我从谷歌读到的我做到了。你能帮我完成上述代码的工作吗?因为我在挣扎
  • 我无法真正理解 #2,并且会让其他人处理那个,除非您在谈论表格中的日期时间,在这种情况下,您可能和我一样了解。
  • 我让你清楚。在这个查询中,我想删除 SELECT * FROM $table where ayear='1' (表名:userlogin,place,venue )我不想显示这些表。 2.SHOW COLUMNS FROM $table('user_id,user_common_id,ayear') 我不想显示这些列。
  • 此时您已经有一个重复的问题要问这里。这是另一个one。他们关门了。

标签: php mysql schema


【解决方案1】:

首先我介绍这个小块,以后可能对其他人有用。

select table_name,column_name,ordinal_position from information_schema.columns
where table_schema = 'event'
order by table_name,ordinal_position

现在到你的。

    $mysqli = new mysqli("localhost", "root", "", "event");
    $result = $mysqli->query("SHOW TABLES");
    $dontDisplay = array('user_id','user_common_id','ayear');
    while ( $row = $result->fetch_row() )
    {   $table = $row[0];
        echo '<h3>',$table,'</h3>';
        $iterationOrdIgnore=array();
        $result1 = $mysqli->query("SELECT * FROM $table where ayear='2014'");
        if($result1) 
        {   $ff = $result1->fetch_fields();
            $iLoop=0;
            foreach ($ff as $oneF) {
                if (in_array($oneF->name, $dontDisplay)) {
                    $iterationOrdIgnore[]=$iLoop;
                }
                $iLoop++;
            }
            echo '<table cellpadding="0" cellspacing="0" class="db-table">';
            $column = $mysqli->query("SHOW COLUMNS FROM $table where field not in ('user_id','user_common_id','ayear')");
            echo '<tr>';
            while($row3 = $column->fetch_row() ) 
            {   echo '<th>'.$row3[0].'</th>';
            }
            echo '</tr>';

            while($row2 = $result1->fetch_row() ) 
            {   echo '<tr>';
                $iLoop=0;
                foreach($row2 as $key=>$value) {
                    if (!in_array($iLoop, $iterationOrdIgnore)) {
                        echo "<td>".$value."</td>";
                    }
                    $iLoop++;
                }
                echo '</tr>';
            }
            echo '</table><br />';
        }
    }
    $mysqli->close();

我使用$dontDisplay 数组不显示列名。

您处理的每个表都可以预期在不同的序数位置有列(当然)。所以数组$iterationOrdIgnore 保存了您正在处理的表格的序数位置......要忽略。

fetch_fields() 用于针对结果集获取列名以驱动上述$iterationOrdIgnore

当列值被回显时,要忽略的值将被忽略。

另外,请密切注意这两条相距甚远的线。

$dontDisplay = array('user_id','user_common_id','ayear');

and

$column = $mysqli->query("SHOW COLUMNS FROM $table where field not in ('user_id','user_common_id','ayear')");

不易出错的情况下以某种方式组合它们,在这种情况下您修改一个,而不是另一个。但这是显示它有效的快速尝试。它当然可以清理。

请参阅 fetch_fieldsin_array() 的手册页。

架构(供他人测试)

create table t1
(   id int auto_increment primary key,
    colQ int not null,
    colR int not null,
    particulars varchar(50) not null,
    user_id int not null,
    user_common_id int not null,
    ayear int not null
);
insert t1(colq,colr,particulars,user_id,user_common_id,ayear) values (1,1,'blah blah 1',1,1,2014),(2,2,'blah blah 2',2,2,2013),(3,3,'blah blah 3',3,3,2012);

create table t2
(   -- different table ordering of columns
    id int auto_increment primary key,
    particulars varchar(50) not null,
    ayear int not null,
    colQ int not null,
    user_id int not null,
    colR int not null,
    user_common_id int not null
);
insert t2(colq,colr,particulars,user_id,user_common_id,ayear) values (4,4,'Show 1',1,1,2014),(9,9,'Show 2',2,2,2013),(13,13,'Show 3',3,3,2012);
insert t2(colq,colr,particulars,user_id,user_common_id,ayear) values (99,99,'2014 2014 2014',1,1,2014),(9,9,'Show 2',2,2,2013),(13,13,'Show 3',3,3,2012);

【讨论】:

  • 看到表格动态出现,列名也动态出现。请检查问题我已经给出了我的例外结果,最后三列字段和列不应该出现在输出中
  • 现在我希望你能理解我的问题。请帮我完成这项工作。如何删除最后三列字段和列值不应出现在输出中
  • 我正在工作......几分钟
  • 如果您有任何疑问,请询问我,因为我只完成了整个申请
  • @Drew “首先我介绍这个小块” - 小吧?哇...我不想看到“大”块。
猜你喜欢
  • 2018-08-20
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 2012-06-05
相关资源
最近更新 更多