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 Linux环境下CA证书更新问题
  • ¥15 sqlserver语句提取结果以外数据
  • ¥60 微信小程序如何上传QQ聊天文件
  • ¥300 开发的系统遭到无良商家的破解,请问如何防止再次发生,并追回损失
  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 安卓OpenCV人脸识别分类器加载