Düzenli ifade ile seçme deyimini bölünmüş

2 Cevap php

i ana bölümden herhangi bir mysql select ifadesi bölmek gerekir: SELECT (eğer varsa), FROM, TÜM KATILDI, WHERE (varsa ) GROUP BY (o) varsa, HAVING (o) varsa, ORDER BY (eğer varsa), LIMIT if ( o) var ...

i tried using regular expressions, but i'm not very good with them... for the SELECT the regex was simple (any SELECT that comes at the begining of the string-^SELECT), but after that i stumbled... all i know it's that the 'FROM' must not be contained in () - which will mean it's from a subselect... and i think that all the other parts of the select (JOINS, WHERE, GROUP BY, etc) will match this rule... but i don't know how to write such a rule... and i hope someone can help me...

select ifadesi için:

SELECT SQL_CALC_FOUND_ROWS (SUM(TIME_TO_SEC(audioMelodii.durata))/60) AS durataTotal, (SELECT COUNT(audioMelodii.id) FROM audioMelodii) AS nrMelodii, audioArtisti.nume AS artist, audioAlbume.*
FROM audioAlbume
LEFT OUTER JOIN audioMelodiiAlbume ON (audioMelodiiAlbume.idAlbum=audioAlbume.id)
LEFT OUTER JOIN audioMelodii ON (audioMelodii.id=audioMelodiiAlbume.idMelodie)
LEFT OUTER JOIN audioArtisti ON (audioAlbume.idArtist=audioArtisti.id)
WHERE audioAlbume.idArtist='$idArtist'
GROUP BY audioAlbume.id
ORDER BY dataLansare DESC, id DESC
LIMIT 0,10

Ben arıyorum sonuç bu gibi bir şey olacaktır:

$STATEMENT['SELECT']='SELECT SQL_CALC_FOUND_ROWS (SUM(TIME_TO_SEC(audioMelodii.durata))/60) AS durataTotal, (SELECT COUNT(audioMelodii.id) FROM audioMelodii) AS nrMelodii, audioArtisti.nume AS artist, audioAlbume.*';
$STATEMENT['FROM']='FROM audioAlbume';
$STATEMENT['JOINS']='LEFT OUTER JOIN audioMelodiiAlbume ON (audioMelodiiAlbume.idAlbum=audioAlbume.id)
LEFT OUTER JOIN audioMelodii ON (audioMelodii.id=audioMelodiiAlbume.idMelodie)
LEFT OUTER JOIN audioArtisti ON (audioAlbume.idArtist=audioArtisti.id)';
$STATEMENT['WHERE']="WHERE audioAlbume.idArtist='$idArtist'";

vs ..

thank you very much! all the best!

2 Cevap

SQL is not a language that can be parsed with regular expressions.
You really need to use a full-blown SQL parser for this task.

Ayrıca bkz:

Birkaç yerde oluşabilir SQL (alt-sorgular, alt-alt-sorgular, özyinelemeli karmaşıklığı nedeniyle düzenli ifadeler ile kolayca bunu yapamaz.

Sizin nihai hedefi açıklamak Belki biz daha iyi bir çözüm bulmanıza yardımcı olabilir.