Search code examples
phpmysqlselectwhere-clauseinformation-schema

How to select Unknown Number of Columns in mysql?


Given the table and names of some columns, I have the following Information schema select query.

SELECT `COLUMN_NAME`
                FROM `INFORMATION_SCHEMA`.`COLUMNS`
                    WHERE `TABLE_SCHEMA` = 'm_college'
                    AND `TABLE_NAME` = 'm_fee'
                    AND COLUMN_NAME NOT IN ('id', 'classid', 'semid')

But this select does not give me the rows value for each unknown column I select. All I got is names of the unknown columns. Is it possible to select the values of the rows so that I can have column as key and rows as value pair in my php script? I need to insert the column names and row values in other table. Please help or suggest.


Solution

  • Below is a very quick attempt at showing an intersect table.

    This allows you to have a fixed structure and add fee types on the fly.

    CREATE TABLE IF NOT EXISTS `mz_fee222` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `classid` int(11) NOT NULL,
      `semid` int(11) NOT NULL,
      `batch` year(4) NOT NULL,
      `session` int(11) NOT NULL
    );
    
    create table fee_type
    (   fee_id int auto_increment primary key,
        description varchar(100)
    );
    insert fee_type (description) values ('exam'),('marksheet'),('admitcard'),('centre'),('practical'); -- etc as you go
    
    create table mz_fee_intersect
    (   -- here is where the fee amount is stored. Flexible.
        id int auto_increment primary key,
        mz_id int not null, -- this is the id from my_fee222 table
        fee_id int not null, -- this is the fee_id from fee_type table
        fee int not null, -- here is the actual fee, like 100 bucks
        -- FK's below (not shown):
    
        -- 
        unique key (mz_id,fee_id) -- forbids duplicates
    );