dongtan1009 2015-10-14 15:16
浏览 239
已采纳

PHP PDO SQlite单个查询,用于查找仅出现一个值的值,以逗号分隔值的行

For example, I have the following table:

 id  Name - Target 
 1    A       B     
 2    C       D,G      
 3    E       D
 4    F       G,B
 5    L       M,D,B
 6    Q       G,N

I have an over 2 million records database, and I think I made an error by storing values with a comma inside a row. My goal is to query the whole database on the "Target" row, for unique, non-repeating values, in my example the query should return only "M" and "N", because "B", "D" and "G" are repeating..

Is there a single query line to get this done ?

  • 写回答

1条回答 默认 最新

  • douxian5076 2015-10-14 15:59
    关注

    I agree that you should change your database schema.

    Your question looks like duplicate of:

    https://stackoverflow.com/a/17942691/4421474

    So your approach if you really need one could be like:

    http://sqlfiddle.com/#!9/0c3ce/4

    SELECT
      SUBSTRING_INDEX(SUBSTRING_INDEX(t1.Target, ',', numbers.n), ',', -1) letter
    FROM
      numbers 
    INNER JOIN t1
      ON CHAR_LENGTH(t1.Target)
         -CHAR_LENGTH(REPLACE(t1.Target, ',', ''))>=numbers.n-1
    GROUP BY letter
    HAVING COUNT(*)=1
    

    Notice that you need to set numbers table with values from 1 to max index of values in target column of your table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题