dongzhan8620 2012-07-25 17:35
浏览 39
已采纳

MySQL选择变量不在行的位置

I'm trying to do MySQL query to extract specific rows without the help of PHP if conditions,

okay,
i want to select a row where the variable is not in a specific row but here's the thing

$blocked_user = "aaron";

and in MySQL

+----------+-----------------------+
| id       | blocked               |
+----------+-----------------------+
| 1        | aaron,john            |

i want it to skip "aaron" while selecting and process to john
a false example :

SELECT * FROM table WHERE $blocked_user NOT IN blocked

What i was doing is get blocked content and seperate them using php explode function then using in_array and if to confirm its not there :

if(in_array($blocked_user, explode($row['blocked'])){
  exit;
}

but now i want to get rid of this and do it using a single query, how is it done ?

  • 写回答

4条回答 默认 最新

  • duankuang7928 2012-07-25 17:43
    关注

    You should really put each of the CSV values in different rows (perhaps in a blocked table), but what you can do is this:

    SELECT * 
    FROM tbl
    WHERE FIND_IN_SET('$blocked_user', blocked) = 0
    

    If the $blocked_user is contained within the CSV list in the blocked column, that row is not selected.


    SQLFiddle Demo

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

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集