简体   繁体   English

选择除MySQL中的主键外的所有列

[英]Select all columns except primary key in MySQL

How can I select all columns except the primary key from a table? 如何从表中选择除主键之外的所有列? It should be noted that I don't know field names in this table, the operation should be able to run automatically. 应该注意的是,我不知道该表中的字段名称,该操作应该能够自动运行。

Thank you for the help. 感谢您的帮助。

Use: 采用:

To get All columns Without primary use 获取所有没有主要用途的列

 SELECT `COLUMN_NAME` FROM `information_schema`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'wpresstest') AND (`TABLE_NAME` = 'wp_countries') AND (`COLUMN_KEY` <> 'PRI')

See you cant do all using simple SQL.So 看到您无法使用简单的SQL来完成所有工作。

Either use mysql Prepare Execute statement Or use PHP way like: 使用mysql Prepare Execute语句或使用PHP方式如下:

1)This would be your 1st Query as above.You will get All column names Excluding primary.OK? 1)这将是上面的第一个查询,您将获得所有列名称不包括primary.OK?

2)Now fetch the resulted array in PHP .and build dynamic query for Comma separated Column names you just got. 2)现在在PHP获取结果数组。并为刚得到的逗号分隔列名构建动态查询。

3)Now fire 2nd query to fetch ROWS .Here you will include SELECT (Column1,Column2) from TBL . 3)现在触发第二个查询以获取ROWS。这里将包含SELECT (Column1,Column2) from TBL

And after getting column names , you can fire query to return Result based on Returned columns 在获取列名之后,您可以触发查询以基于返回的列返回结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM