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

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


当前回答

我很晚才想出一个答案,坦率地说,这是我一直在做的事情,它比最好的答案要好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]);

其他回答

如果它总是相同的一列,那么你可以创建一个不包含它的视图。

否则,不,我不这么认为。

我有一个建议,但不是解决办法。 如果您的一些列有较大的数据集,那么您应该尝试使用以下方法

SELECT *, LEFT(col1, 0) AS col1, LEFT(col2, 0) as col2 FROM table

如果你想排除一个字段的值,例如安全问题/敏感信息,你可以检索该列为空。

e.g.

SELECT *, NULL AS salary FROM users

只做

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,";
   }      
}

我同意只选择*是不够的,如果你不需要,正如在其他地方提到的,是一个BLOB,你不希望有这个开销。

我会用所需的数据创建一个视图,然后您可以轻松地选择*——如果数据库软件支持它们的话。否则,将大量数据放到另一个表中。