MySql: Show columns but exclude everything except the field names

前端 未结 6 671
陌清茗
陌清茗 2021-01-30 13:36

I\'d like to pull a table\'s field names from MySql into python, and I know that

\'show columns from project\'

will work. And I\'ve read that

6条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 13:51

    If your goal is a comma-delimited list (Not very Python literate, but that's mostly what you'd want in PHP and Perl) GROUP_CONCAT is your friend:

    SELECT GROUP_CONCAT(column_name) FROM information_schema.columns WHERE table_name='your-table-name-here' GROUP BY TABLE_NAME ORDER BY ORDINAL_POSITION
    

    If you need them quoted this gives you all EXCEPT the outermost quotes:

    SELECT GROUP_CONCAT(column_name SEPARATOR '","') FROM information_schema.columns WHERE table_name='your-table-name-here' GROUP BY TABLE_NAME ORDER BY ORDINAL_POSITION
    

提交回复
热议问题