Php MySQL-将部分查询连接到新查询?
我有以下查询表的代码。然后它使用结果进行另一个查询。然后使用该结果进行第三次查询 但是如何从第二个查询中获取userid字段,以便从users表中获取一个名称并将其连接到第三个查询的结果中呢 请注意,一旦我找到了代码,我将把它转换成一个准备好的语句。对我来说,在计算查询时使用遗留代码更容易Php MySQL-将部分查询连接到新查询?,php,mysql,inner-join,self-join,Php,Mysql,Inner Join,Self Join,我有以下查询表的代码。然后它使用结果进行另一个查询。然后使用该结果进行第三次查询 但是如何从第二个查询中获取userid字段,以便从users表中获取一个名称并将其连接到第三个查询的结果中呢 请注意,一旦我找到了代码,我将把它转换成一个准备好的语句。对我来说,在计算查询时使用遗留代码更容易 $selectaudioid = "SELECT audioid FROM subscribe WHERE userid = $userid"; $audioResult=
$selectaudioid = "SELECT audioid FROM subscribe WHERE userid = $userid";
$audioResult=$dblink->query($selectaudioid);
if ($audioResult->num_rows>0) {
while ($row = $audioResult->fetch_assoc()) {
$newaudio = $row[audioid];
$getallaudio = "SELECT opid, userid from audioposts WHERE audioid = $newaudio" ;
$getallresult = $dblink->query($getallaudio);
if ($getallresult->num_rows>0) {
while ($row = $getallresult->fetch_assoc()) {
$opid = $row[opid];
$opuserid = $row[userid];
$getreplies =
"SELECT * from audioposts ap WHERE opid = $opid AND opid
NOT IN (SELECT opid FROM audioposts WHERE audioposts.opid = '0' )";
$getreplyresults = $dblink->query($getreplies);
if ($getreplyresults->num_rows>0) {
while ($row = $getreplyresults->fetch_assoc()) {
$dbdata[]=$row;
}
}
}
}
}
} "SELECT * from audioposts ap WHERE opid = $opid AND opid
NOT IN (SELECT opid FROM audioposts WHERE audioposts.opid = '0' )";
$getreplyresults = $dblink->query($getreplies);
if ($getreplyresults->num_rows>0) {
while ($row = $getreplyresults->fetch_assoc()) {
$dbdata[]=$row;
}
}
}
}
}
}
echo json_encode($dbdata);
我需要的结果是
$getreplyresults
的json编码实例行,原始结果中的$row[userid]连接到每一行。下面是我最后做的。现在我只需要弄清楚如何将其转换为一个准备好的语句,以避免恶意注入
$selectaudioid = "SELECT audioid FROM subscribe WHERE userid = $userid";
$audioResult=$dblink->query($selectaudioid);
if ($audioResult->num_rows>0) {
while ($row = $audioResult->fetch_assoc()) {
$newaudio = $row[audioid];
$getallaudio = "
SELECT ap.audioid, ap.title, us.name FROM audioposts ap
INNER JOIN audioposts a2 ON a2.audioid = ap.opid
INNER JOIN users us ON us.id = a2.userid
WHERE ap.opid = $newaudio AND ap.opid <> '0'
";
$getallresult = $dblink->query($getallaudio);
if ($getallresult->num_rows>0) {
while ($row = $getallresult->fetch_assoc()) {
$dbdata[]=$row;
}}}}
$selectaudioid=“从订阅中选择audioid,其中userid=$userid”;
$audioResult=$dblink->query($selectaudioid);
如果($audioResult->num_rows>0){
而($row=$audioResult->fetch_assoc()){
$newaudio=$row[audioid];
$getallaudio=”
从audioposts ap中选择ap.audioid、ap.title、us.name
a2上的内部连接AudioPost a2.audioid=ap.opid
内部用户在us.id=a2.userid上加入我们
其中ap.opid=$newaudio和ap.opid'0'
";
$getallresult=$dblink->query($getallaudio);
如果($getallresult->num_rows>0){
而($row=$getallresult->fetch_assoc()){
$dbdata[]=$row;
}}}}
< /代码>如果你包含了第一个查询,就有可能把它变成1个SQL语句。我该怎么做呢?从你当前的问题来看,你不知道什么是“第一查询”。这是查询“SELECT opid,userid from audioposts,其中audioid=$newaudio”“?是的,您确实正确,我需要“SELECT opid,userid from audioposts,其中audioid=$newaudio”中的“userid”作为最终结果。为了进一步澄清,我进行了编辑。更新您的问题添加适当的数据样本和预期结果