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 :-)

Siddhu
  • 241
  • 2
  • 3
  • 16
  • probably substring function will do the trick. All of your query has where, group, order and limit? – jose_bacoy Mar 19 '18 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. – tadman Mar 19 '18 at 17:41
  • at present, i would get such type of queries @tadman – Siddhu Mar 19 '18 at 17:44
  • 1
    Just use a SQL parser. Regular expressions are not the answer to everything. – tadman Mar 19 '18 at 17:44
  • @Siddhu Why not use this variables in string see this answer: https://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string – Zydnar Mar 19 '18 at 18:09
  • Possible duplicate of [PHP - concatenate or directly insert variables in string](https://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string) – Zydnar Mar 19 '18 at 18:10
  • AT MY PRESENT SENIRAIO IT WILL NOT WORK @ZYDNAR – Siddhu Mar 19 '18 at 18:13
  • @Siddhu but this is not included in your question, so update it, or change. And please don't SHOUT. – Zydnar Mar 19 '18 at 18:28
  • Could you explain what that's good for and what the real problem is? – odan Mar 19 '18 at 18:37
  • 1
    I want to add extra add filers later at WHERE condition, So that I'm separating the query – Siddhu Mar 19 '18 at 18:39
  • Once you have the code to parse _that_ statement, your next statement will have some variation that breaks your REGEXP. My point is that this is too much of an energy sink to be worth pursuing. (I had the same Question 15 years ago; I have yet to find a good answer.) – Rick James Mar 29 '18 at 01:18

1 Answers1

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...

nitrex
  • 522
  • 6
  • 16