Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何将结果列拆分为多个列_Mysql - Fatal编程技术网

Mysql 如何将结果列拆分为多个列

Mysql 如何将结果列拆分为多个列,mysql,Mysql,我有一张像这样的桌子: id ip -- ----------- 1 192.168.2.1 需要得到如下结果: id ip A B C D -- ----------- --- --- --- --- 1 192.168.2.1 192 168 2 1 可行吗 SELECT `id`, `ip`, SUBSTRING_INDEX( `ip` , '.', 1 ) AS a, SUBSTRING_IND

我有一张像这样的桌子:

id  ip
--  -----------
1   192.168.2.1
需要得到如下结果:

id  ip           A    B    C    D
--  -----------  ---  ---  ---  ---
1   192.168.2.1  192  168  2    1
可行吗

SELECT `id`, `ip`,
    SUBSTRING_INDEX( `ip` , '.', 1 ) AS a,
    SUBSTRING_INDEX(SUBSTRING_INDEX( `ip` , '.', 2 ),'.',-1) AS b,
    SUBSTRING_INDEX(SUBSTRING_INDEX( `ip` , '.', -2 ),'.',1) AS c,
    SUBSTRING_INDEX( `ip` , '.', -1 ) AS d
FROM unicorns

示例

SET @ip = '192.168.1.1';

SELECT @ip,
    SUBSTRING_INDEX( @ip , '.', 1 ) AS a,
    SUBSTRING_INDEX(SUBSTRING_INDEX( @ip , '.', 2 ),'.',-1) AS b,
    SUBSTRING_INDEX(SUBSTRING_INDEX( @ip , '.', -2 ),'.',1) AS c,
    SUBSTRING_INDEX( @ip , '.', -1 ) AS d
结果

| @IP | A | B | C | D | ----------------------------------- | 192.168.1.1 | 192 | 168 | 1 | 1 | |@IP | A | B | C | D| ----------------------------------- |192.168.1.1 | 192 | 168 | 1 | 1 |可能重复见类似问题