【发布时间】:2016-01-11 10:37:14
【问题描述】:
上面是我的桌子。现在我想要实现的是查询类型为“main”的查询,包括所有“sub”,其中 parent 等于当前行的 id。我怎样才能做到这一点?任何帮助将不胜感激!
以下是我当前的代码:
$conn = new PDO("mysql:host=$host;dbname=$db_name",$user,$pass) or die("DB Connection failed!!");
$stmt = $conn->prepare('SELECT * FROM tbl_categories WHERE type="main"');
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
//first loop i should get Electronics, Laptops, Mobile Phones
//second loop i should get Fashion, Men, Women
}
我不想在 while 循环中执行另一个查询。
【问题讨论】:
-
在 sql 中('普通' sql,即不是游标等)是基于集合的。因此,没有“当前行”。说明您需要什么,以及您尝试了什么。
-
当前行是谁?您是指类型为“main”的行吗?向我们展示您期望的输出
-
OP 已更新.. @AlexandruSeverin
-
OP 更新了@HoneyBadger