I am currently using the following two queries:
$get_forum=$this->prepare("SELECT * FROM ".TBL_PREFIX."forums WHERE id=:forum_id");
$get_forum->bindParam(":forum_id", $forum_id);
$get_forum->execute();
$forum=$get_forum->fetchAll();
$get_parent=$this->prepare("SELECT * FROM ".TBL_PREFIX."forums WHERE id=:parent_id");
$get_parent->bindParam(":parent_id", $forum["parent_id"]);
$get_parent->execute();
I would like to combine them into one query something like this where it uses the bound param or a value from a field in the database. I don’t know what the parent ID is until I get the forum data back from the database.
Parent forums have a parent_id of -1, while child forums have the parent_id set to the id of the parent.
$get_forums=$this->prepare("SELECT * FROM ".TBL_PREFIX."forums WHERE id=:forum_id OR forum_id=parent_id");
$get_forums->bindParam(":forum_id", $forum_id);
$get_forums->execute();
Is it possible to use a field value while inside the query itself to compare it with another value?
Database sample:
http://sqlfiddle.com/#!2/2b354/1
You can make a self-join: