duanre1891 2011-03-25 05:48
浏览 33
已采纳

有人可以帮我解决这个问题吗?

I have id's stored in a row of my database, and the ids are seperated by commas ex:

12,3,5,2,7

and i am trying to create a query by imploding the string and selecting each one of the id's, from another table, but i am trying to avoid looping the query because of performance issues. is there anyway around a loop? I am currently using mysqli prepared statements.

$stmt = $DBH->prepare("SELECT ids FROM list WHERE user = ?");
    $stmt->bind_param("s",$userid);
    $stmt->execute();
    $stmt->bind_result($ids);
        $stmt->fetch();

stored in ids is just a list of numbers, now how can i query another table looking for the rows that match each one of the numbers in the list, similar to a tagging system. The numbers or the amount of numbers are not predetermined or known before making the query.

EXAMPLE:

Table 1: Where the actual Information is stored

ID    Information

1     /*information stored for id number 1*/
2     /*information stored for id number 2*/
3     /*information stored for id number 3*/
4     /*information stored for id number 4*/
5     /*information stored for id number 5*/



Table 2: The users name and a list of id's stored (The ID_LIST will be constantly changing and expanding, the numbers contained will not be predefined.)

USER      ID_LIST

exampleuser    1,3,5
exampleuser2   1,4,12,22

The first query will be obtaining the list of ids from Table 2 and separating them. Then i need to get the information from each id in table 1 from the ids in the id list in table 2. but i am trying to avoid a loop, hopefully this is specific enough, if not please let me know.

  • 写回答

3条回答 默认 最新

  • dongqiabei7682 2011-03-25 06:06
    关注
    SELECT * FROM tbl WHERE FIND_IN_SET(id, '12,3,5,2,7')
    

    So you can specify any field that contains a list of numbers separated by comma as a second parameter of FIND_IN_SET

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

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角