1

I have Mysql query

$query = "SELECT ticket_id, subject FROM tickets WHERE status_code = 0 GROUP BY priority ORDER BY incident_id LIMIT 5";

I want to convert this

$select = "SELECT ticket_id, subject ";
$from = "FROM tickets ";
$where = " WHERE status_code = 0 ";
$group = "GROUP BY priority";
$order = "ORDER BY incident_id";
$limit = "LIMIT 5";

How can I achieve this? Is it possible to do with REGEX? Please give me your instructions?

or advise me

how to take a specific string value using REGEX?

Thank you in advance :-)

11
  • probably substring function will do the trick. All of your query has where, group, order and limit? Commented Mar 19, 2018 at 17:39
  • You need a SQL parser. This might seem like a candidate for a regular expression, but it's not. What about SELECT (SELECT id FROM x) AS y LEFT JOIN ...? It gets unbelievably complicated for all but the most trivial of queries. Commented Mar 19, 2018 at 17:41
  • at present, i would get such type of queries @tadman Commented Mar 19, 2018 at 17:44
  • 1
    Just use a SQL parser. Regular expressions are not the answer to everything. Commented Mar 19, 2018 at 17:44
  • 1
    I want to add extra add filers later at WHERE condition, So that I'm separating the query Commented Mar 19, 2018 at 18:39

1 Answer 1

1

I would suggest using a PHP Sql parser such as https://github.com/greenlion/PHP-SQL-Parser

Go through their documentation and you will find examples of how to accomplish exactly what you requested above. [Edit]: here's their manual page: https://github.com/greenlion/PHP-SQL-Parser/wiki/Parser-Manual

Regular expressions solution is going to get messy quickly...

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

Comments

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.