我试图使用一个选择语句从某个MySQL表中获得除一个以外的所有列。有什么简单的方法吗?

编辑:在这个表格中有53列(不是我的设计)


当前回答

您可以使用DESCRIBE my_table并使用其结果动态地生成SELECT语句。

其他回答

即使要查询所有列,也最好指定要查询的列。

因此,我建议您在语句中写下每一列的名称(不包括您不想要的列)。

SELECT
    col1
    , col2
    , col3
    , col..
    , col53

FROM table

据我所知,没有。你可以这样做:

SELECT col1, col2, col3, col4 FROM tbl

并手动选择所需的列。然而,如果你想要很多列,那么你可能只需要做一个:

SELECT * FROM tbl 

忽略你不想要的。

针对你的特殊情况,我建议:

SELECT * FROM tbl

除非你只想要几列。如果你只想要四列,那么:

SELECT col3, col6, col45, col 52 FROM tbl

这很好,但如果您想要50个列,那么任何使查询变得(太?)难以阅读的代码。

只做

SELECT * FROM table WHERE whatever

然后用您最喜欢的编程语言php删除该列

while (($data = mysql_fetch_array($result, MYSQL_ASSOC)) !== FALSE) {
   unset($data["id"]);
   foreach ($data as $k => $v) { 
      echo"$v,";
   }      
}

我很晚才想出一个答案,坦率地说,这是我一直在做的事情,它比最好的答案要好100倍,我只希望有人能看到它。发现它很有用

    //create an array, we will call it here. 
    $here = array();
    //create an SQL query in order to get all of the column names
    $SQL = "SHOW COLUMNS FROM Table";
        //put all of the column names in the array
        foreach($conn->query($SQL) as $row) {
            $here[] = $row[0];
        }
    //now search through the array containing the column names for the name of the column, in this case i used the common ID field as an example
    $key = array_search('ID', $here);
    //now delete the entry
    unset($here[$key]);

我同意@Mahomedalid的回答,但我不想做一些准备好的语句,我不想输入所有的字段,所以我有一个愚蠢的解决方案。

去phpmyadmin->sql->select表,它转储查询:复制,替换和完成!:)