我使用下面的代码(a 是 var_dump 等效项):
/**
* Only works if the table names and database names don't have a space or a dot in them.
*/
$queries = [
"select * from my_table",
"select * from `my_table`",
"select * from my_db.my_table",
"select * from my_db.`my_table`",
"select id from my_table where x=2",
"select id from my_table mt inner join other_table ot on ot.my_table_id=mt.id where mt.x=2",
"SELECT mail_id, mail_date, mail_from, mail_to, mail_subject, (
(
SELECT COUNT(*)
FROM `sys_messagecenter_qmails`
WHERE qmail_mail_id = mail_id
) + (
SELECT COUNT(*)
FROM `sys_messagecenter_rels`
WHERE rel_mail_id = mail_id
)
) AS email_total, (
SELECT COUNT(*)
FROM `sys_messagecenter_rels`
WHERE rel_mail_id = mail_id
) AS email_sent, (
SELECT COUNT(*)
FROM `sys_messagecenter_rels`
INNER JOIN `sys_email_receives` ON receive_reply_to = rel_sent_id
WHERE rel_mail_id = mail_id
) AS email_reply FROM `sys_messagecenter_emails` WHERE mail_draft='No'
ORDER BY mail_id ASC LIMIT 0,10 ",
"select count(*) as count from (
select
user_id,
permission_group_id,
concat(uhpg.user_id, '. ', u.pseudo) as user_id_plus,
concat(uhpg.permission_group_id, '. ', pg.name) as permission_group_id_plus
from lud_user_has_permission_group uhpg
inner join lud_user u on uhpg.user_id=u.id
inner join lud_permission_group pg on uhpg.permission_group_id=pg.id
) as ttt",
];
$tables = [];
foreach ($queries as $query) {
a($query);
if (preg_match_all('!((FROM|JOIN)\s([\S]+))!i', $query, $matches)) {
$tables = array_unique($matches[3]);
array_walk($tables, function (&$v) {
$p = explode('.', $v, 2);
$v = array_pop($p);
$v = trim($v, '`');
});
$tables = array_filter($tables, function ($v) {
return ('(' !== $v);
});
a($tables);
}
}
这给出了以下输出:
string(22) "select * from my_table"
array(1) {
[0] => string(8) "my_table"
}
string(24) "select * from `my_table`"
array(1) {
[0] => string(8) "my_table"
}
string(28) "select * from my_db.my_table"
array(1) {
[0] => string(8) "my_table"
}
string(30) "select * from my_db.`my_table`"
array(1) {
[0] => string(8) "my_table"
}
string(33) "select id from my_table where x=2"
array(1) {
[0] => string(8) "my_table"
}
string(89) "select id from my_table mt inner join other_table ot on ot.my_table_id=mt.id where mt.x=2"
array(2) {
[0] => string(8) "my_table"
[1] => string(11) "other_table"
}
string(810) "SELECT mail_id, mail_date, mail_from, mail_to, mail_subject, (
(
SELECT COUNT(*)
FROM `sys_messagecenter_qmails`
WHERE qmail_mail_id = mail_id
) + (
SELECT COUNT(*)
FROM `sys_messagecenter_rels`
WHERE rel_mail_id = mail_id
)
) AS email_total, (
SELECT COUNT(*)
FROM `sys_messagecenter_rels`
WHERE rel_mail_id = mail_id
) AS email_sent, (
SELECT COUNT(*)
FROM `sys_messagecenter_rels`
INNER JOIN `sys_email_receives` ON receive_reply_to = rel_sent_id
WHERE rel_mail_id = mail_id
) AS email_reply FROM `sys_messagecenter_emails` WHERE mail_draft='No'
ORDER BY mail_id ASC LIMIT 0,10 "
array(4) {
[0] => string(24) "sys_messagecenter_qmails"
[1] => string(22) "sys_messagecenter_rels"
[4] => string(18) "sys_email_receives"
[5] => string(24) "sys_messagecenter_emails"
}
string(360) "select count(*) as count from (
select
user_id,
permission_group_id,
concat(uhpg.user_id, '. ', u.pseudo) as user_id_plus,
concat(uhpg.permission_group_id, '. ', pg.name) as permission_group_id_plus
from lud_user_has_permission_group uhpg
inner join lud_user u on uhpg.user_id=u.id
inner join lud_permission_group pg on uhpg.permission_group_id=pg.id
) as ttt"
array(3) {
[1] => string(29) "lud_user_has_permission_group"
[2] => string(8) "lud_user"
[3] => string(20) "lud_permission_group"
}