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

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


当前回答

Select *是一个SQL反模式。它不应该在生产代码中使用,原因有很多,包括:

它需要更长的时间来处理。当程序运行数百万次时,这些微小的部分就会产生影响。在缓慢的数据库中,这种缓慢是由这种类型的草率编码引起的,是最难进行性能调优的类型。

这意味着你发送的数据可能比你需要的多,这会导致服务器和网络瓶颈。如果您有一个内部连接,那么发送超过所需数据的可能性是100%。

这会导致维护问题,特别是当您添加了不想到处都看到的新列时。此外,如果您有一个新列,您可能需要对接口做一些事情,以确定对该列做什么。

它可以打破视图(我知道这在SQl server中是真的,在mysql中可能是真的,也可能不是真的)。

If someone is silly enough to rebuild the tables with the columns in a differnt order (which you shouldn't do but it happens all teh time), all sorts of code can break. Espcially code for an insert for example where suddenly you are putting the city into the address_3 field becasue without specifying, the database can only go on the order of the columns. This is bad enough when the data types change but worse when the swapped columns have the same datatype becasue you can go for sometime inserting bad data that is a mess to clean up. You need to care about data integrity.

如果在插入中使用它,如果在一个表中添加了新列,而在另一个表中没有添加,那么它将中断插入。

它可能会破坏触发器。触发问题可能很难诊断。

将所有这些与添加列名所花费的时间加起来(哎呀,你甚至可能有一个允许你拖拽列名的界面(我知道我在SQL Server中这样做,我打赌有一些方法可以做到这一点,你用一些工具来编写mysql查询)。让我们看看,“我可以引起维护问题,我可以引起性能问题,我可以引起数据完整性问题,但是嘿,我节省了5分钟的开发时间。”只需要填入你想要的具体列。

我也建议你读这本书: http://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers-ebook/dp/B00A376BB2/ref=sr_1_1?s=digital-text&ie=UTF8&qid=1389896688&sr=1-1&keywords=sql+antipatterns

其他回答

如果愿意,可以使用SQL生成SQL,并对生成的SQL进行评估。这是一种通用的解决方案,因为它从信息模式中提取列名。下面是一个Unix命令行的示例。

替换

MYSQL的MYSQL命令 带有表名的TABLE 包含排除字段名的EXCLUDEDFIELD

echo $(echo 'select concat("select ", group_concat(column_name) , " from TABLE") from information_schema.columns where table_name="TABLE" and column_name != "EXCLUDEDFIELD" group by "t"' | MYSQL | tail -n 1) | MYSQL

实际上,您只需要以这种方式提取列名一次,就可以构造排除该列的列列表,然后只需使用已构造的查询。

比如:

column_list=$(echo 'select group_concat(column_name) from information_schema.columns where table_name="TABLE" and column_name != "EXCLUDEDFIELD" group by "t"' | MYSQL | tail -n 1)

现在可以在构造的查询中重用$column_list字符串。

起初我以为你可以使用正则表达式,但我一直在阅读MYSQL文档,似乎你不能。如果我是你,我会使用另一种语言(如PHP)来生成您想要获取的列的列表,将其存储为字符串,然后使用它来生成SQL。

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

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

我的主要问题是在连接表时获得了许多列。虽然这不是您问题的答案(如何从一个表中选择除某些列之外的所有列),但我认为值得一提的是,您可以指定表。从特定表中获取所有列,而不是仅指定。

下面是一个很有用的例子:

select users.*, phone.meta_value as phone, zipcode.meta_value as zipcode

from users

left join user_meta as phone
on ( (users.user_id = phone.user_id) AND (phone.meta_key = 'phone') )

left join user_meta as zipcode
on ( (users.user_id = zipcode.user_id) AND (zipcode.meta_key = 'zipcode') )

结果是用户表中的所有列,以及从元表中连接的两个附加列。

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