dongmao3131 2013-08-13 11:27
浏览 44
已采纳

从mysql查询中的数组中选择WHERE

I'm trying to SELECT from an array inside a mysql query

result = mysql_query("
SELECT * FROM fruit_db
WHERE fruit='".$url."'
")

$url has been created by $_GET and exploded into an array. It can be 'apple' or 'apple,banana,orange' for example. Not sure whether to implode back into string or not.

I need to be selecting 'apple' OR 'banana' OR 'orange', not 'apple,banana,orange'.

I'm used to counting the elements of the string, and looping through them, but not inside a mysql query, I can't work out if there's a simple way. I've tried something like

/*    
    result = mysql_query("
    SELECT * FROM fruit_db
    WHERE fruit='".$url[0]."'
    OR  fruit='".$url[1]."'
    // etc
    ")
*/

but I don't know how many elements there will be

  • 写回答

4条回答 默认 最新

  • doukuizuo1795 2013-08-13 11:29
    关注
    "SELECT * FROM fruit_db WHERE fruit IN ('".implode("','", $url)."')";
    

    Suppose your $url = array('apple','banana','orange');

    Then sql will be :

    SELECT * FROM fruit_db WHERE fruit IN ('apple','banana','orange')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: