PDO sub query Function sub query with select database operation using GOModel

Description

A Subquery or Inner query or Nested query is a query within another SQL query and embedded within the WHERE clause. GOModel subquery function allow us to specify subquery to use with existing sql query. You need to specify subquery to be embedded, alias(optional) and array of values if data binding is used.

Function Defination

  
                                void function subQuery($sql, $alias = "", $data = array()) ;
                                

Parameters Details

Details Type Example
$sql - subquery/inner query to be embedded string Any valid sql e.g. "select * from emptable where empId=9"
$alias - alias for subquery/inner query string Any valid alias e.g. "empTable"
$data - data to be used for binding array Any valid array e.g. array(10,20)

Examples

  
                                $GOModel = new GOModel(); //create object of the GOModel class
                                $GOModel->connect("localhost", "root", "", "database");//connect to database
                                /* Sub query function */
                                $GOModel->subQuery("select post_id from wp_postmeta where meta_id=?","postmeta", array(20));
                                $result =  $GOModel->select("wp_posts");                            
                            

More Examples

 
                                //Example 1 
                                //subquery with where and order by etc.
                                $GOModel->subQuery("select post_id from wp_postmeta where meta_id=?","postmeta", array(20));
                                $GOModel->where("p.id", 10, ">=");
                                $GOModel->orderByCols = array("p.id");
                                $result =  $GOModel->select("wp_posts as p")
                            

Result/Output

By default, it returns data in associative array format. You can set return type using the fetch mode.

Debug

Option Details Example
$GOModel->getLastQuery(); Return last query executed Query: SELECT * ,(select post_id from wp_postmeta where meta_id=?) AS postmeta FROM `wp_posts` AS `p` WHERE `p`.`id`>= ? ORDER BY `p`.`id`
print_r($GOModel->error) Print errors (if any)
$GOModel->totalRows; Total rows returned for select query 10