dongxiong5546 2015-07-11 18:26
浏览 447
已采纳

SQL查询 - 如何从数组中获取值?

I've been using the following SQL query to output values from my WordPress user profiles.

$divisions = $wpdb->get_col("
    SELECT Distinct(c.meta_value) 
    FROM $wpdb->usermeta AS r  
    INNER JOIN $wpdb->usermeta AS y
      ON r.user_id = y.user_id
    INNER JOIN $wpdb->usermeta AS g
      ON r.user_id = g.user_id
    INNER JOIN $wpdb->usermeta AS re
      ON r.user_id = re.user_id
    INNER JOIN $wpdb->usermeta AS c
      ON r.user_id = c.user_id
    WHERE r.meta_value LIKE '%subscriber%'
    AND y.meta_key = 'CERTIFIED_DISPLAY'
    AND y.meta_value NOT LIKE 'no'
    AND g.meta_key = 'CERTIFIED_CATEGORY'
    AND g.meta_value = '$category'
    AND re.meta_key = 'dbem_region'
    AND re.meta_value = '$region'
    AND c.meta_key = 'dbem_school_division'
    ORDER BY c.meta_value ASC
    ");

It worked great until I had to change one of the meta keys ('CERTIFIED_CATEGORY') to an array. That particular field used to be a select box with a single value, and then I changed it to a checkbox list which is an array of values. This is the part of the query I now need to fix:

AND g.meta_key = 'CERTIFIED_CATEGORY'
AND g.meta_value = '$category'

$category is one of the values in the array. I thought I could just change the '=' to 'LIKE', but it didn't work:

AND g.meta_key = 'CERTIFIED_CATEGORY'
AND g.meta_value LIKE '$category'

I also tried 'IN', but no luck. How can I do this?

  • 写回答

2条回答 默认 最新

  • duan0403788996 2015-07-13 18:45
    关注

    It seems all I needed to do was change this line:

    AND g.meta_value = '$category'
    

    to this:

    AND g.meta_value LIKE '%$category%'
    

    I had already tried using LIKE, but I didn't use the % signs around the variable. Apparently the % was necessary, because now it works. Wish I knew more to explain why.

    Thanks!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题