zoukankan      html  css  js  c++  java
  • mysql字段按逗号,拆分并按条件查找

    mysql字段按逗号,拆分并按条件查找

    mysql 表结构

    CREATE TABLE `subid` (
      `id1` varchar(255) DEFAULT NULL,
      `id2` varchar(255) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    INSERT INTO `mysql`.`subid`(`id1`, `id2`) VALUES ('1,2,3', 'A,G,H');
    INSERT INTO `mysql`.`subid`(`id1`, `id2`) VALUES ('2,3,4', 'D,F,G');
    INSERT INTO `mysql`.`subid`(`id1`, `id2`) VALUES ('1,4,5', 'H,L,K');
    INSERT INTO `mysql`.`subid`(`id1`, `id2`) VALUES ('1,3,2', 'gg,uu,kk');
    
    

    查找2所对应的值

    SELECT
    	idone,
    	idtwo 
    FROM
    	(
    SELECT
    	substring_index( substring_index( t.id1, ',', b.help_topic_id + 1 ), ',', - 1 ) AS idone,
    	substring_index( substring_index( t.id2, ',', b.help_topic_id + 1 ), ',', - 1 ) AS idtwo 
    FROM
    	subid t
    	JOIN mysql.help_topic b ON b.help_topic_id < ( LENGTH( t.id1 ) - LENGTH( REPLACE ( t.id1, ',', '' ) ) + 1 ) 
    	) AS allId 
    WHERE
    	idone = 2
    

    结果

    result

  • 相关阅读:
    线程原理 创建方式
    Chapter17 【异常、线程】
    驱动
    java中Super到底是什么意思
    Java 8后的首个长期支持版本Java 11
    OpenJDK和JDK区别
    异常
    模拟斗地主洗牌发牌
    Debug追踪
    Python 垃圾回收机制详细
  • 原文地址:https://www.cnblogs.com/ants_double/p/11088864.html
Copyright © 2011-2022 走看看