简体   繁体   English

有没有一种方法可以根据另一个表的另一行中保存的ID选择MYSQL中的多行?

[英]Is there a way I can select multiple rows in MYSQL based on the IDs saved in another row of another table?

<?php
$sqlstr = mysql_query("SELECT * FROM outfits")or die(mysql_error());
if (mysql_numrows($sqlstr) != 0) {
while ($row = mysql_fetch_array($sqlstr)) {

$sqlstr2 = mysql_query("SELECT * FROM products WHERE pid in ($row['tid'], $row['did'])")or die(mysql_error());

?>
<p><?= $row['pname'] ?></p>
<p><?= $row['pcat'] ?></p>
<p><?= $row['pimg1'] ?></p>
<?php
}
}
?>

You can do it in one query: 您可以在一个查询中执行此操作:

SELECT p.* FROM outfits AS o
INNER JOIN products AS p
ON p.pid IN (o.tid, o.did)

This has the additional benefit that you're not blindly pasting PHP values into your query (which is generally a big red flag, because unless you're really really careful, you're opening up a big box of SQL injection vulnerabilities). 这样做还有一个好处,就是您不会盲目地将PHP值粘贴到查询中(通常这是一个很大的危险信号,因为除非您非常谨慎,否则您会打开一大盒SQL注入漏洞)。

试试这个$sqlstr2 = mysql_query("SELECT * FROM products WHERE pid in (select id from second table where condition )")or die(mysql_error());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据mySQL中另一个表的ID省略选择结果? - How to omit select results based on IDs from another table in mySQL? 从一个表中选择行,其中基于第二个表ID的另一个第二个表中的ID显示结果的顶部以及下一个结果,因为它是+ Mysql - Select rows from a table where id in another second table based on second table ids shows results top as well as next results as it is + Mysql MySQL查询-一个表中的单行,另一表中的多行 - MySQL query - single row in one table with multiple rows in another table MYSQL:基于来自另一个表的SELECT的更新行 - MYSQL: UPDATE rows based on SELECT from another table 根据另一个表中的关联在mysql数据库中选择行 - select rows in mysql database based on associations in another table 将多行复制到另一张表中作为mysql中的单行 - Copying multiple rows into another table as a single row in mysql 如何使用联接基于另一个表中的行从一个表中获取mysql行? - How can I use joins to fetch mysql rows from one table based on rows that are found in another? MYSQL:选择与另一个表的多行相关的条目 - MYSQL: Select entries in relation to multiple rows of another table 如何在一张表的多行中添加多列,以及在另一张表中添加一行? - How can I add multiple columns from multiple rows in 1 table along with 1 row from another? mysql-根据ID和DATE从一个表中获取一行,如果不显示空值,则在另一表中获取多行 - mysql - taking one row from one table based on ID and DATE and get multiple rows in another table if not show null values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM