sql-parser
sql-parser copied to clipboard
Building statement with FORCE INDEX results in wrong SQL
Hello,
I have query that's not parsed or built properly when FORCE INDEX is used:
SELECT a.id, a.name, b.order_id, b.total
FROM customers a
INNER JOIN orders b FORCE INDEX (idx_customer_id)
ON a.id = b.customer_id
WHERE a.status = 'active'
The query becomes:
SELECT a.id, a.name, b.order_id, b.total
FROM customers AS `a` FORCE INDEX (idx_customer_id)
INNER JOIN orders AS `b`
WHERE a.status = 'active'
-
FORCE INDEXgot moved from tableordersontocustomers. - The
a.id = b.customer_idjoin condition was removed.
I see the same linting errors as #497, so this is probably related.
Example code
<?php
require __DIR__ . '/vendor/autoload.php';
$in = <<<SQL
SELECT a.id, a.name, b.order_id, b.total
FROM customers a
INNER JOIN orders b FORCE INDEX (idx_customer_id)
ON a.id = b.customer_id
WHERE a.status = 'active'
SQL;
$parser = new PhpMyAdmin\SqlParser\Parser($in);
$statement = $parser->statements[0];
echo $statement->build();