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 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口