select first N columns of MySQL table

后端 未结 3 1762
深忆病人
深忆病人 2020-12-16 02:12

As it is possible to select top N rows from table, is there any way to select first N columns from MySQL database tables?
Thanks for your replies and maybe some parts of

相关标签:
3条回答
  • 2020-12-16 02:48

    You cannot do this directly in MySQL, you must do this server-side. In PHP this might look like this:

    <?php
    $mysqli->real_query("SELECT id, title, name FROM test ORDER BY id ASC");
    $res = $mysqli->use_result();
    
    $numberOfColumnsToShow = 2;
    
    while ($row = $res->fetch_assoc()) {
        // Only select the first $numberOfColumnsToShow columns
        $rowWithSpecifiedNumberOfColumns = array_slice($row, 0, $numberOfColumnsToShow);
    
        // $rowWithSpecifiedNumberOfColumns only contains the first two columns (id, title)
    }
    ?>
    
    0 讨论(0)
  • 2020-12-16 02:51

    Please have a look at Bill Karwin's answer first. But if you know how to order your column names there could be a solution that makes use of a dynamic query.

    To select all column names from a table, you can use a query like this:

    SELECT `column_name` 
    FROM   `information_schema`.`columns` 
    WHERE  `table_schema`=DATABASE() 
           AND `table_name`='yourtablename';
    

    (please have a look at this answer). And making use of GROUP_CONCAT:

    GROUP_CONCAT(CONCAT('`', column_name, '`') ORDER BY column_name)
    

    we can return all column names in a single row, separated by commas:

    `col1`, `col2`, `col3`, ...
    

    (I also added quotes around the name of the column, and please notice that we have to order our list of columns somehow, otherwise there are no guarantees about the order in which column names are returned).

    Then we can cut the returned string, using SUBSTRING_INDEX, in order to get, for example, the first 2 column names:

    SUBSTRING_INDEX(columns, ',', 2)
    

    and our final query, that concatenates 'SELECT ', the selected columns above, and ' FROM Tab1', and inserts the resulting string into the @sql variable is this:

    SELECT
      CONCAT(
        'SELECT ',
        SUBSTRING_INDEX(
          GROUP_CONCAT(CONCAT('`', column_name, '`') ORDER BY column_name),
          ',',
          2),
        ' FROM Tab1'
      )
    FROM
      information_schema.columns 
    WHERE
      table_schema=DATABASE() 
      AND table_name='Tab1'
    INTO @sql;
    

    It's value will be something like this:

    @sql = "SELECT `col1`, `col2` FROM Tab1"
    

    and you can then prepare your statement, and execute it:

    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    

    Please see fiddle here.

    0 讨论(0)
  • 2020-12-16 02:56

    SQL requires that you name the columns you want, or else use the * wildcard.

    In relational theory, there is no concept of "first N columns" because columns have no implicit order. Of course in any concrete implementation of SQL, they must have some storage order, but the SQL language doesn't have any support for fetching columns by "position" in the table, nor is there any support for fetching sequences of columns (except for *).

    0 讨论(0)
提交回复
热议问题