• <legend id='zlNLj'><style id='zlNLj'><dir id='zlNLj'><q id='zlNLj'></q></dir></style></legend>
  • <tfoot id='zlNLj'></tfoot>

    • <bdo id='zlNLj'></bdo><ul id='zlNLj'></ul>

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

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

        在 MySQL 4(不是 5)中总结逗号分隔的列

        Summing a comma separated column in MySQL 4 (not 5)(在 MySQL 4(不是 5)中总结逗号分隔的列)

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

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

                  <tbody id='ghXE4'></tbody>

                <tfoot id='ghXE4'></tfoot>

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

                  本文介绍了在 MySQL 4(不是 5)中总结逗号分隔的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个查询,从一个表中选择数据到另一个表中,需要移动的列之一是 DECIMAL 列.由于我无法控制的原因,源列有时可以是逗号分隔的数字列表.有没有优雅的 sql 唯一方法来做到这一点?

                  I'm writing a query that selects data from one table into another, one of the columns that needs to be moved is a DECIMAL column. For reasons beyond my control, the source column can sometimes be a comma separated list of numbers. Is there an elegant sql only way to do this?

                  例如:

                  源列

                  10.2
                  5,2.1
                  4
                  

                  应该产生一个目标列

                  10.2
                  7.1
                  4
                  

                  顺便说一句,我使用的是 MySQL 4.

                  I'm using MySQL 4, btw.

                  推荐答案

                  要进行这种非平凡的字符串操作,您需要使用存储过程,对于 MySQL,它在 6 年前才出现在 5.0 版本中.

                  To do this kind of non trivial string manipulations, you need to use stored procedures, which, for MySQL, only appeared 6 years ago, in version 5.0.

                  MySQL 4 现在很老了,分支 4.1 的最新版本是 4.1.25,在 2008 年.不再支持.大多数 Linux 发行版不再提供它.是时候升级了.

                  MySQL 4 is now very old, the latest version from branch 4.1 was 4.1.25, in 2008. It is not supported anymore. Most Linux distributions don't provide it anymore. It's really time to upgrade.

                  这是适用于 MySQL 5.0+ 的解决方案:

                  Here is a solution that works for MySQL 5.0+:

                  DELIMITER //
                  CREATE FUNCTION SUM_OF_LIST(s TEXT)
                    RETURNS DOUBLE
                    DETERMINISTIC
                    NO SQL
                  BEGIN
                    DECLARE res DOUBLE DEFAULT 0;
                    WHILE INSTR(s, ",") > 0 DO
                      SET res = res + SUBSTRING_INDEX(s, ",", 1);
                      SET s = MID(s, INSTR(s, ",") + 1);
                    END WHILE;
                    RETURN res + s;
                  END //
                  DELIMITER ;
                  

                  示例:

                  mysql> SELECT SUM_OF_LIST("5,2.1") AS Result;
                  +--------+
                  | Result |
                  +--------+
                  |    7.1 |
                  +--------+
                  

                  这篇关于在 MySQL 4(不是 5)中总结逗号分隔的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bogus foreign key constraint fail(虚假外键约束失败)
                  how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                  Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                  How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                  Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                  ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)
                    <tbody id='09zNA'></tbody>
                    • <small id='09zNA'></small><noframes id='09zNA'>

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