Location: PHPKode > scripts > SQL Parse Convert to Tree Array > sql-parse-convert-to-tree-array/example.php
<?php
/**
* Author  : NEUMANN-RYSTOW Fran├žois <hide@address.com>
* Date    : 23 Oct, 2008
* Purpose : Usage example of dqml2tree class
*/

require 'dqml2tree.php';

// simple query
$sql_query = "SELECT field1, field2 FROM table WHERE field3='val1' AND field4=5 OR field5='val2';";

$query2tree = new dqml2tree($sql_query);
$sql_tree = $query2tree->make();

print_r($sql_tree);


// complex query
$sql_query = 'SELECT 

*,
(SELECT avg(field1), field2 as alias1, field3, field4 FROM table1 WHERE field1=table2.field2) as query_field1 

FROM 
table3, 
table4,
((table5 AS alias6
LEFT JOIN table6 ON table5.field5=table6.field5)
INNER JOIN table7 ON table6.field5=table7.field5)
OUTER JOIN table8 ON table7.field5=table8.field5
JOIN table9 ON table8.field5=table9.field5

WHERE
field5 IN (SELECT field5 FROM table10 WHERE field6=\'y\')
AND 
field7=\'SELECT field8 FROM table11 WHERE field9 LIKE \'\'%plo\'\'\'\'ppy%\'\' AND field9 LIKE \'\'%WORD (OTHER WORD) IN LETTER\'\'\'\'NAMES\'\'\'
OR 
field8 IS NOT NULL
OR 
field9 IS NULL
AND 
field10>=\'value1\'


UNION

SELECT *, (SELECT max(field11), field12 FROM table12 WHERE field13=table13.field13) as field_alias1
FROM table13
WHERE field14=\'n\' AND (field15<=field16 AND field17=field18)


GROUP BY field19, field20
HAVING count(*)>0 AND avg(field1)=1 OR max(field1)<10

INTERSECT SELECT * FROM table15


ORDER BY field1 ASC, field21 DESC
LIMIT 10
OFFSET 100
';


$query2tree = new dqml2tree($sql_query);
$sql_tree = $query2tree->make();

print_r($sql_tree);

?>
Return current item: SQL Parse Convert to Tree Array