如何在动态获取表字段的同时,动态获取表字段数目?请告诉帮忙

解决方案 »

  1.   


    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | csdn               |
    | discuz             |
    | ecshop             |
    | images             |
    | mysql              |
    | phpmyadmin         |
    | sourcedata         |
    | teachdata          |
    | test               |
    +--------------------+
    10 rows in set (0.00 sec)mysql> use information_schema;
    Database changed
    mysql> show tables;
    +---------------------------------------+
    | Tables_in_information_schema          |
    +---------------------------------------+
    | CHARACTER_SETS                        |
    | COLLATIONS                            |
    | COLLATION_CHARACTER_SET_APPLICABILITY |
    | COLUMNS                               |
    | COLUMN_PRIVILEGES                     |
    | KEY_COLUMN_USAGE                      |
    | PROFILING                             |
    | ROUTINES                              |
    | SCHEMATA                              |
    | SCHEMA_PRIVILEGES                     |
    | STATISTICS                            |
    | TABLES                                |
    | TABLE_CONSTRAINTS                     |
    | TABLE_PRIVILEGES                      |
    | TRIGGERS                              |
    | USER_PRIVILEGES                       |
    | VIEWS                                 |
    +---------------------------------------+
    17 rows in set (0.00 sec)mysql> select t1.column_name,t2.cc from columns t1,
        -> (select count(*) cc from columns where table_schema='csdn'
        -> and table_name='orders') t2
        -> where t1.table_schema='csdn'
        -> and t1.table_name='orders';
    +-------------+----+
    | column_name | cc |
    +-------------+----+
    | id          |  4 |
    | bno         |  4 |
    | type        |  4 |
    | dd          |  4 |
    +-------------+----+
    4 rows in set (0.01 sec)mysql> desc csdn.orders;
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | id    | int(11)  | YES  |     | NULL    |       |
    | bno   | int(11)  | YES  |     | NULL    |       |
    | type  | int(11)  | YES  |     | NULL    |       |
    | dd    | datetime | YES  |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    4 rows in set (0.00 sec)
      

  2.   


    mysql> select group_concat(t1.column_name) as fields,t2.cc
        -> from columns t1,
        -> (select count(*) cc from columns
        -> where table_schema='csdn'
        -> and table_name='orders')t2
        -> where t1.table_schema='csdn'
        -> and t1.table_name='orders'
        -> group by t1.table_name;
    +----------------+----+
    | fields         | cc |
    +----------------+----+
    | id,bno,type,dd |  4 |
    +----------------+----+
    1 row in set (0.00 sec)