mysql獲取某錶所有字段,並且新增字段字首

2021-10-02 02:37:46 字數 822 閱讀 6679

一句sql可以直接獲取目標表的字段資訊,不用在model內乙個乙個寫出來。

select group_concat(column_name separator 『,bwpd.』) from information_schema.columns where table_name = 'adi_business_weight_price_detail』

表:『adi_business_weight_price_detail』;

中間間隔:,bwpd. 【注意這裡如果不指定,則預設是用 『,』 逗號間隔】

如果只是直接需要字段,用逗號隔開則sql為:select group_concat(column_name) from information_schema.columns where table_name = 'adi_business_weight_price_detail』

取出來的樣子差不多為 『id,name,age…』 這種樣子

如果是鍊錶,表的別名為 『abcd』,出來的字段需要加字首,需要的字段為 『abcd.id, abcd.name, abcd.age…』這種,則修改sql為:select group_concat(column_name separator 『,abcd.』) from information_schema.columns where table_name = 'adi_business_weight_price_detail』

這個技巧的作用:當乙個表的字段較多的時候,不用每個都去手打出來,一句sql就拿到了,再刪除不需要的sql就行了。

MYSQL向某錶新增字段

使用alter table命令來向乙個表新增字段,示例如下 向t user表新增user age欄位 alter table t user add column user age int 11 default null comment 年齡 after user email 命令解釋 t user ...

mysql獲取某個表的所有欄位名

mysql安裝成功後可以看到已經存在mysql information schema和test這個幾個資料庫,information schema庫中有乙個名為columns的表,這個表中記錄了資料庫中所有表的字段資訊。知道這個表後,獲取任意表的字段就只需要一條select語句即可。例如 selec...

mysql獲取某個表的所有欄位名

mysql安裝成功後可以看到已經存在mysql information schema和test這個幾個資料庫,information schema庫中有乙個名為columns的表,這個表中記錄了資料庫中所有表的字段資訊。知道這個表後,獲取任意表的字段就只需要一條select語句即可。例如 selec...