Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表a中的字段位于表B+mysql+php的字段中_Php_Mysql_Sql_Pdo - Fatal编程技术网

表a中的字段位于表B+mysql+php的字段中

表a中的字段位于表B+mysql+php的字段中,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,我有两个表,结构如下: tblA: 我需要按照上面的示例从tblA和tblB进行查询,但第二次查询不会返回任何记录 我也试过“IN”操作符,但不太奏效 请帮我…试试这样的方法: SELECT * FROM tblb,tbla WHERE tblb.id= 1 AND FIND_IN_SET(tbla.id, tblb.aid) 详情请参阅: 通过分别从aid列提取每个ID,可以使用不同的方法 $a = $this::$db->prepare('SELECT * FROM tblB WHE

我有两个表,结构如下:

tblA:

我需要按照上面的示例从tblA和tblB进行查询,但第二次查询不会返回任何记录

我也试过“IN”操作符,但不太奏效


请帮我…

试试这样的方法:

SELECT * FROM tblb,tbla
WHERE tblb.id= 1 AND FIND_IN_SET(tbla.id, tblb.aid)
详情请参阅:


通过分别从aid列提取每个ID,可以使用不同的方法

$a = $this::$db->prepare('SELECT * FROM tblB WHERE id= :id LIMIT 1');
$a->bindValue(':id', $ID, PDO::PARAM_INT);
$a->execute();
$r = $a->fetch(pdo::FETCH_ASSOC);

if ($a->rowCount() > 0)
{
    $bInf = $r['id']   . '|*|' .
            $r['name'] . '|*|' .
            $r['aid']  . '|**|';

    //extract each of the ids in the variable 'aid'
    $tbla_ids = explode(',',$r['aid']);
    foreach($tbla_ids as $tbla_id){
        //case for the record where aid = '2,'
        if(strlen($tbla_id)==0){
            continue;
        }
        $b = $this::$db->prepare('SELECT id,name FROM tblA WHERE id= :ids');
        $b->bindValue(':ids', $tbla_id);
        $b->execute();
        //do what you need to do here. The query returns the single record
        //from tbla that matches the id $tbla_id
    }
}

我犯了一个有趣的错误,通过从aid字段中删除“字符”,问题得到了解决,并且“在集合中查找”立即生效。

将表B数据放入连接表中,它将使用索引,而且它将很快得到维护,或者我应该说它可以使用索引,我并不是说像现在这样的最左派string@Drew泰,这是不可能使用垃圾表或索引的。那位先生有一个很好的理由解释说这句话,但那句话不能使用。随着提议的解决方案的提出,他的问题有了修改。我在这里没有看到任何这样的解释。“不能”通常意味着我不想这样做。@Drew yes你是对的,因为查询只能用“IN”或“FIND_IN_SET”或相同的其他运算符或func实现,我将很快编辑我的文章,并提供更多说明。是的,这是可能的,但是性能很低。。。我不能使用循环,谢谢,在一个查询中从两个表中选择不是我的解决方案,但链接很有用。
$a = $this::$db->prepare('SELECT * FROM tblB WHERE id= :id LIMIT 1');
$a->bindValue(':id', $ID, PDO::PARAM_INT);
$a->execute();
$r = $a->fetch(pdo::FETCH_ASSOC);

if ($a->rowCount() > 0){
    $bInf = $r['id']   . '|*|' .
            $r['name'] . '|*|' .
            $r['aid']  . '|**|';

    $b = $this::$db->prepare('SELECT id,name FROM tblA WHERE FIND_IN_SET(id,:ids)');
    $b->bindValue(':ids', $r['aid']);
    $b->execute();
    $rs = $b->fetchAll(pdo::FETCH_ASSOC);

    if ($b->rowCount() > 0)
    {
        foreach ($rs as $srow => $srval)
          $aInf .= $srval['id']   . '[|]' .
                   $srval['name'] . '[#]' ;
    } else
        $aInf = ' ';
        $aInf.=  '|***|' . $bInf; 
    }
}
SELECT * FROM tblb,tbla
WHERE tblb.id= 1 AND FIND_IN_SET(tbla.id, tblb.aid)
$a = $this::$db->prepare('SELECT * FROM tblB WHERE id= :id LIMIT 1');
$a->bindValue(':id', $ID, PDO::PARAM_INT);
$a->execute();
$r = $a->fetch(pdo::FETCH_ASSOC);

if ($a->rowCount() > 0)
{
    $bInf = $r['id']   . '|*|' .
            $r['name'] . '|*|' .
            $r['aid']  . '|**|';

    //extract each of the ids in the variable 'aid'
    $tbla_ids = explode(',',$r['aid']);
    foreach($tbla_ids as $tbla_id){
        //case for the record where aid = '2,'
        if(strlen($tbla_id)==0){
            continue;
        }
        $b = $this::$db->prepare('SELECT id,name FROM tblA WHERE id= :ids');
        $b->bindValue(':ids', $tbla_id);
        $b->execute();
        //do what you need to do here. The query returns the single record
        //from tbla that matches the id $tbla_id
    }
}