从 SHOW COLUMNS mysql 中排除某些列

发布于 2024-10-12 21:36:32 字数 648 浏览 1 评论 0原文

我有这个查询:SHOW COLUMNS FROM mash,它在我的 while 循环中运行良好,用于构建由表列名称组成的选择元素。但在我的表中,我在选择元素中有“id”和“tstamp”,我不希望这些列出现,是否可以排除这些列?

echo "<form action='".$_SERVER['PHP_SELF']."' method='get'>";
            connectDB();
            $result = mysql_query("SHOW COLUMNS FROM mash") or die(mysql_error());
            echo '<select name="column" class="column">';
            while ($row = mysql_fetch_array($result)) {
                echo "<option value='".$row[0]."'>".ucwords($row[0])."</option>";
            }
            closeConn();
            echo '</select>';
echo "</form>";

I have this query: SHOW COLUMNS FROM mash which works fine in my while loop for building a select element made from table column names. But in my table i have "id" and "tstamp" which i dont want in the select element, is this possible to exclude these columns?

echo "<form action='".$_SERVER['PHP_SELF']."' method='get'>";
            connectDB();
            $result = mysql_query("SHOW COLUMNS FROM mash") or die(mysql_error());
            echo '<select name="column" class="column">';
            while ($row = mysql_fetch_array($result)) {
                echo "<option value='".$row[0]."'>".ucwords($row[0])."</option>";
            }
            closeConn();
            echo '</select>';
echo "</form>";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

成熟的代价 2024-10-19 21:36:32

PHP方式:

当获取这些字段时,在 while 循环中使用 continue ,例如this:

echo "<form action='".$_SERVER['PHP_SELF']."' method='get'>";
    connectDB();
    $result = mysql_query("SHOW COLUMNS FROM mash") or die(mysql_error());
    echo '<select name="column" class="column">';
    while ($row = mysql_fetch_array($result))
    {
        if($row[0] == 'id' || $row[0] == 'tstamp')
              continue;
        echo "<option value='".$row[0]."'>".ucwords($row[0])."</option>";
    }
    closeConn();
    echo '</select>';
echo "</form>";

这将跳过 id 和 tstamp 字段并处理所有其他字段。 continue 用于循环结构中,以跳过当前循环迭代的其余部分,并在条件评估时继续执行,然后在下一次迭代开始时继续执行。


MySQL 方式:

删除查询中的这些字段,如下所示:

SHOW COLUMNS FROM mash WHERE Field NOT IN ('id', 'tstamp');

PHP Way:

Use a continue in the while loop, when those fields are fetched, like this:

echo "<form action='".$_SERVER['PHP_SELF']."' method='get'>";
    connectDB();
    $result = mysql_query("SHOW COLUMNS FROM mash") or die(mysql_error());
    echo '<select name="column" class="column">';
    while ($row = mysql_fetch_array($result))
    {
        if($row[0] == 'id' || $row[0] == 'tstamp')
              continue;
        echo "<option value='".$row[0]."'>".ucwords($row[0])."</option>";
    }
    closeConn();
    echo '</select>';
echo "</form>";

This will just skip the id and tstamp fields and process all others. continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.


MySQL Way:

Remove those fields in the query like this:

SHOW COLUMNS FROM mash WHERE Field NOT IN ('id', 'tstamp');
大姐,你呐 2024-10-19 21:36:32

是的,这是可能的。不要使用 SHOW COLUMNS 使用 INFORMATION_SCHEMA

INFORMATION_SCHEMA 是从关系数据库中提取元数据的 ANSI 方法。在 MySQL 中,您可以使用:

$sql = "select column_name from information_schema.columns c " + 
       " where c.table_schema = 'db_name' " + 
       "   and c.table_name='table_name' " + 
       "   and c.column_name like 'name%'";

信息模式的优点是符合 SQLANSI。您可以在MySQL、PostgreSQL、SQLServer和其他关系数据库中使用它。

Yes, it's possible. Instead of using SHOW COLUMNS use INFORMATION_SCHEMA.

INFORMATION_SCHEMA is the ANSI way of extracting metadata from a relational database. In MySQL you can use:

$sql = "select column_name from information_schema.columns c " + 
       " where c.table_schema = 'db_name' " + 
       "   and c.table_name='table_name' " + 
       "   and c.column_name like 'name%'";

Information Schema has the advantage that is SQLANSI compliant. You can use it in MySQL, PostgreSQL, SQLServer and other relational databases.

帅冕 2024-10-19 21:36:32

您可以使用 LIKE 运算符。

SHOW COLUMNS FROM mash LIKE "name%"

You can use LIKE operator.

SHOW COLUMNS FROM mash LIKE "name%"
泅人 2024-10-19 21:36:32

要仅查看一个字段的 SHOW FULL COLUMNS from tablename 中显示的“排序规则”列,请执行以下操作:

select COLLATION_NAME from information_schema.columns
where TABLE_SCHEMA = 'tableschemaname' and TABLE_NAME = 'tablename' and COLUMN_NAME = 'fieldname';

fieldname 拖放到 where 中>,如果您想查看该列的所有字段名称。

要查看从 SHOW FULL COLUMNS 显示的所有可能的列名称,以便您可以选择所需的内容:

select * from information_schema.columns 
where TABLE_SCHEMA = 'tableschemaname' and TABLE_NAME = 'tablename' and COLUMN_NAME = 'fieldname';

To see just the "Collation" column that shows up in SHOW FULL COLUMNS from tablename for just one field, it's:

select COLLATION_NAME from information_schema.columns
where TABLE_SCHEMA = 'tableschemaname' and TABLE_NAME = 'tablename' and COLUMN_NAME = 'fieldname';

Ddrop the fieldname in the where, if you want to see that column for all field names.

To see all possible column names that show up from SHOW FULL COLUMNS so that you can select what you want:

select * from information_schema.columns 
where TABLE_SCHEMA = 'tableschemaname' and TABLE_NAME = 'tablename' and COLUMN_NAME = 'fieldname';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文