1

I have a very long query with a lot of sub-queries in it (almost a thousand lines long).

What I want to do is to get the names of all the tables that are being used in the query. So take the following example of a query just to get an idea:

SQL Query

$sql = "SELECT
    ...,
    ...,
    (
        SELECT COUNT(t.id) AS t_count
        FROM _user_just_a_table_db_ jat
        WHERE ...
        AND ...
        AND ...
    ),
    ...,
    ...,
    ...
    FROM _user_main_table_db_ tbl
    LEFT JOIN _user_joining_table_db_ jt
    ON jt.id = tbl.jt_id
    WHERE ...
    AND ...
    AND tbl.id IN (
        SELECT at.id
        FROM _user_another_table_db_ at
        WHERE at.active = 'Y'
    )";

In this particular situation, I want to get maybe an array or a string with the following results:

just_a_table
main_table
joining_table
another_table

Notes

  1. Please note that the names of the table can appear multiple times. You may or may not worry about that as I can easily remove duplicates after getting the resulting array or string.
  2. As you may have already noticed, the list of strings that I want to get will ALWAYS be in between _user_ and _db_.
6
  • 1
    regex101.com/r/aV8hR2/1 Commented Apr 5, 2016 at 15:15
  • @rock321987 (_user_.*?_db_) would get the full table name. Commented Apr 5, 2016 at 15:23
  • @apokryfos see the regex once again Commented Apr 5, 2016 at 15:24
  • @rock321987 I'm using your regex but I'm getting an error that says Warning: preg_match(): Unknown modifier '('. Here's my code: preg_match('_user_(.*?)_db_/gm', $sql, $matches);. Commented Apr 5, 2016 at 15:26
  • 1
    @PatrickGregorio you're missing the opening regex delimiter Commented Apr 5, 2016 at 15:27

2 Answers 2

2

This should work

_user_(.*?)_db_

Regex Demo

PHP code

$match = array();
$re = "/_user_(.*?)_db_/"; 
$str = "SELECT\n    ...,\n    ...,\n    (\n        SELECT COUNT(t.id) AS t_count\n        FROM _user_just_a_table_db_ jat\n        WHERE ...\n        AND ...\n        AND ...\n    ),\n    ...,\n    ...,\n    ...\n    FROM _user_main_table_db_ tbl\n    LEFT JOIN _user_joining_table_db_ jt\n    ON jt.id = tbl.jt_id\n    WHERE ...\n    AND ...\n    AND tbl.id IN (\n        SELECT at.id\n        FROM _user_another_table_db_ at\n        WHERE at.active = 'Y'\n    )\""; 

$res = preg_match_all($re, $str, $match);
print_r($match[1]);

Ideone Demo

Sign up to request clarification or add additional context in comments.

1 Comment

@chris85 actually I generated the code from regex101..Updated
0
$pattern = '/_user_(.*)_db_/';
preg_match_all($pattern, $sql, $match);
var_dump($match[0]);

1 Comment

Please elaborate on how this code answers the question (this answer was in the Low Quality Posts review queue).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.