dousi4950 2016-05-05 12:00
浏览 144
已采纳

如何在sql查询中使用动态数组

My query:

SELECT MAX(num)
FROM (SELECT nomInteretUser,idUser,COUNT(nomInteretUser) as num
      FROM userInteret
      WHERE nomInteretUser IN ('piano','flute','chien') GROUP BY idUser
     )

The goal is to change the IN ('piano','flute','chien') to a dynamique array like

SELECT nomInteretUser, idUser, COUNT(nomInteretUser) as num
FROM userInteret
WHERE nomInteretUser IN ($array)
GROUP BY idUser

NB: $array is an array houw contain the list of interet

  • 写回答

2条回答 默认 最新

  • duanke2012 2016-05-05 12:04
    关注

    Use implode(). Try below code. You need to convert your array to string. So here we will implode it with comma separated with quotes like ',' string and use it in query.

    $array =array('piano','flute','chien');
    
    $string = implode("','", $array);
    
    $query = "SELECT nomInteretUser, idUser, COUNT(nomInteretUser) as num
    FROM userInteret
    WHERE nomInteretUser IN ('".$string."')
    GROUP BY idUser";
    

    Check online Demo: Click Here

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

报告相同问题?

悬赏问题

  • ¥15 请问不小心下载到了钓鱼软件怎么办?
  • ¥15 求国博抢票 求国博抢票 有的私
  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]