• <small id='yuo6D'></small><noframes id='yuo6D'>

    <tfoot id='yuo6D'></tfoot>
    • <bdo id='yuo6D'></bdo><ul id='yuo6D'></ul>

    1. <legend id='yuo6D'><style id='yuo6D'><dir id='yuo6D'><q id='yuo6D'></q></dir></style></legend>
      <i id='yuo6D'><tr id='yuo6D'><dt id='yuo6D'><q id='yuo6D'><span id='yuo6D'><b id='yuo6D'><form id='yuo6D'><ins id='yuo6D'></ins><ul id='yuo6D'></ul><sub id='yuo6D'></sub></form><legend id='yuo6D'></legend><bdo id='yuo6D'><pre id='yuo6D'><center id='yuo6D'></center></pre></bdo></b><th id='yuo6D'></th></span></q></dt></tr></i><div id='yuo6D'><tfoot id='yuo6D'></tfoot><dl id='yuo6D'><fieldset id='yuo6D'></fieldset></dl></div>

        如何在mySQL中动态选择列名

        时间:2023-06-01

        <small id='8goP8'></small><noframes id='8goP8'>

        <tfoot id='8goP8'></tfoot>

      1. <i id='8goP8'><tr id='8goP8'><dt id='8goP8'><q id='8goP8'><span id='8goP8'><b id='8goP8'><form id='8goP8'><ins id='8goP8'></ins><ul id='8goP8'></ul><sub id='8goP8'></sub></form><legend id='8goP8'></legend><bdo id='8goP8'><pre id='8goP8'><center id='8goP8'></center></pre></bdo></b><th id='8goP8'></th></span></q></dt></tr></i><div id='8goP8'><tfoot id='8goP8'></tfoot><dl id='8goP8'><fieldset id='8goP8'></fieldset></dl></div>

      2. <legend id='8goP8'><style id='8goP8'><dir id='8goP8'><q id='8goP8'></q></dir></style></legend>

          <tbody id='8goP8'></tbody>
          • <bdo id='8goP8'></bdo><ul id='8goP8'></ul>

                  本文介绍了如何在mySQL中动态选择列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想选择列名,但我不知道提前表结构,它可能会改变,所以我不能只用列名硬编码 select 语句.我也不想选择每一列.有没有简单的方法可以做到这一点?

                  I want to select column names but I don't know the table structure ahead of time and it may change so I can't just hard code the select statement with column names. I also do NOT want to select every column. Is there and easy way to do this?

                  我的想法是这两个查询的某种组合,但我的 SQL 不是那么好.

                  My thoughts are it's some kind of combination of these two queries but my SQL is not that good.

                  SHOW COLUMNS FROM table_name;
                  SELECT * FROM table_name; 
                  

                  我尝试使用子选择,但没有用.似乎什么也没发生,我没有收到错误,只是没有结果

                  I tried using a sub select but it didn't work. Nothing seems to happen, I don't get an error I just get no results

                  SELECT (SELECT column_name 
                          FROM information_schema.columns 
                          WHERE table_name ='table_name') 
                  FROM table_name;
                  

                  也许我需要加入?..无论如何,任何帮助都会很棒,谢谢

                  Maybe I need to do a join?.. Anyway any help would be great, thanks

                  推荐答案

                  试试这个 SQLFiddle:

                  CREATE TABLE atable (
                    prefix1 VARCHAR(10)
                    ,prefix2 VARCHAR(10)
                    ,notprefix3 INT
                    ,notprefix4 INT
                  );
                  
                  INSERT INTO atable VALUES ('qwer qwer', 'qwerqwer', 1, 1);
                  INSERT INTO atable VALUES ('qwer qwer', 'asdfaasd', 1, 1);
                  INSERT INTO atable VALUES ('qwer qwer', 'qrt vbb', 1, 1);
                  INSERT INTO atable VALUES ('qwer qwer', 'sdfg sdg', 1, 1);
                  
                  SELECT CONCAT('SELECT ', GROUP_CONCAT(c.COLUMN_NAME), ' FROM atable;')
                  INTO @query
                  FROM INFORMATION_SCHEMA.COLUMNS c
                  WHERE c.TABLE_NAME = 'atable'
                    AND c.COLUMN_NAME LIKE 'prefix%'
                  ORDER BY c.ORDINAL_POSITION;
                  
                  PREPARE stmt FROM @query;
                  
                  EXECUTE stmt;
                  

                  一些问题:

                  您可能需要在结果集上使用某种 ORDER BY.

                  You will likely want some kind of ORDER BY on your result set.

                  就连接和事物而言,您可以执行的操作是有限的.

                  There's a limit to what you can do in terms of joins and things.

                  您将验证转移到运行时,在那里它更有可能被测试遗漏.

                  You move validation to runtime where it's more likely to be missed by testing.

                  您希望能够轻松处理架构更改.这种技术只会处理您可以预见的特定类型的架构更改,而其他技术您可能无论如何都必须更改此代码.

                  You are hoping to be able to handle schema changes easily. This technique will only handle schema changes of a certain type you can foresee, and others you will probably have to change this code anyway.

                  这篇关于如何在mySQL中动态选择列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 MySQL 中将行转置为列 下一篇:在 MySQL 4(不是 5)中总结逗号分隔的列

                  相关文章

                    <bdo id='sg8Ic'></bdo><ul id='sg8Ic'></ul>

                    <small id='sg8Ic'></small><noframes id='sg8Ic'>

                      <tfoot id='sg8Ic'></tfoot>
                    1. <legend id='sg8Ic'><style id='sg8Ic'><dir id='sg8Ic'><q id='sg8Ic'></q></dir></style></legend>
                      <i id='sg8Ic'><tr id='sg8Ic'><dt id='sg8Ic'><q id='sg8Ic'><span id='sg8Ic'><b id='sg8Ic'><form id='sg8Ic'><ins id='sg8Ic'></ins><ul id='sg8Ic'></ul><sub id='sg8Ic'></sub></form><legend id='sg8Ic'></legend><bdo id='sg8Ic'><pre id='sg8Ic'><center id='sg8Ic'></center></pre></bdo></b><th id='sg8Ic'></th></span></q></dt></tr></i><div id='sg8Ic'><tfoot id='sg8Ic'></tfoot><dl id='sg8Ic'><fieldset id='sg8Ic'></fieldset></dl></div>