Mysql正则表达式使用多个规则来匹配字符串

时间:2016-03-21 09:39:14

标签: php mysql regex laravel

我有一张表如下:(transactions_filter):

+----+------------------------------+------------+--------------------------+
| ID | ContactName                  | account_id | Expression               |
+----+------------------------------+------------+--------------------------+
|  1 | Salaries & Wages             |    6123372 | SH\-01\-[0-9]*?\-477\-0  |
|  2 | Salaries & Wages             |    6123372 | SH\-01\-00000001\-550\-0 |
|  3 | Salaries & Wages             |    6123372 | SH\-01\-00000001\-551\-0 |

然后我的交易看起来如下:(账户)

+---------+------------------------------------+-------------+--------+---------+-------------+--------------------+-------------+-------------+-------------+------------------+---------------------+---------------------+
| ID      | Name                               | category_id | Active | Balance | Description | UnallocatedAccount | IsTaxLocked | AccountType | HasActivity | DefaultTaxTypeId | Modified            | Created             |
+---------+------------------------------------+-------------+--------+---------+-------------+--------------------+-------------+-------------+-------------+------------------+---------------------+---------------------+
| 6123359 | Accounting Fees                    |           4 |      1 |    0.00 |             |                  0 |           0 |           1 |           0 |          1509579 | 2016-03-19 15:06:34 | 2016-03-19 15:06:34 |
| 6123360 | Advertising                        |           4 |      1 |    0.00 |             |                  0 |           0 |           1 |           0 |          1509579 | 2016-03-19 15:06:34 | 2016-03-19 15:06:34 |
| 6123357 | Bad Debts Recovered                |           3 |      1 |    0.00 |             |                  0 |           0 |           1 |           0 |          1509579 | 2016-03-19 15:06:34 | 2016-03-19 15:06:34 |

我的交易表如下:

+-------+------------+------------+-------------------------------------------------------------+-----------------------------+----------+------------+---------------+---------------------+---------------------+
| id    | foreign_id | date       | description                                                 | short_description           | amount   | balance    | function_code | created_at          | updated_at          |
+-------+------------+------------+-------------------------------------------------------------+-----------------------------+----------+------------+---------------+---------------------+---------------------+
| 38322 |            | 2015-12-01 | MONTHLY ACC FEE      HEADOFFICE                             |                             |   -59.50 | -181792.19 | SERM          | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
| 38323 |            | 2015-12-01 | TRANSACTION CHARGE   HEADOFFICE                             |                             |  -198.75 | -181990.94 | TRNC          | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
| 38324 |            | 2015-12-01 | CASH DEPOSIT FEE     HEADOFFICE                             |                             | -2638.01 | -184628.95 | CSHC          | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
| 38325 |            | 2015-12-01 | OD: LEDGER FEE       HEADOFFICE                             |                             |   -40.00 | -184668.95 | FACF          | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
| 38326 |            | 2015-12-01 | DEBIT INTEREST       HEADOFFICE                             |                             | -2390.35 | -187059.30 | INDR          | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
| 38327 |            | 2015-12-01 | STOP ORDER FROM      SETTLEMENT ABSA BANK 123.com           | ABSA BANK 123.com           |    30.00 | -187029.30 | SIC           | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
| 38328 |            | 2015-12-01 | STOP ORDER FROM      SETTLEMENT ABSA BANK acc8469           | ABSA BANK acc8469           |    29.90 | -186999.40 | SIC           | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
| 38329 |            | 2015-12-01 | STOP ORDER FROM      SETTLEMENT ABSA BANK 321.co.za         | ABSA BANK loxizea.co.za     |    29.99 | -186969.41 | SIC           | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
| 38330 |            | 2015-12-01 | STOP ORDER FROM      SETTLEMENT ABSA BANK cceyc             | ABSA BANK cceyc             |    30.00 | -186939.41 | SIC           | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
| 38331 |            | 2015-12-01 | STOP ORDER FROM      SETTLEMENT ABSA BANK 178444 123        | ABSA BANK 178444 123        |    35.00 | -186904.41 | SIC           | 2016-03-16 14:03:27 | 2016-03-16 14:03:27 |
+-------+------------+------------+-------------------------------------------------------------+-----------------------------+----------+------------+---------------+---------------------+---------------------+

现在,如何在不循环遍历每个过滤器的情况下将事务与我的过滤器匹配,如下所示:

    $filters = \App\Models\Transaction\Filter::All();   
    $transactions = \App\Models\Transaction::All();

    $data = [];
    foreach($transactions as $transaction)
    {
        foreach($filters as $filter)
        {
            if(preg_match("/" . $filter->Expression . "/", $transaction->description))
            {
                $data[] = [
                    'date' => $transaction->date,
                    'ContactName' => $filter->ContactName,
                    'account_id' => $filter->account_id,
                ];
            }
        }
    }

    dd($data);

1 个答案:

答案 0 :(得分:0)

您可以使用FULL OUTER JOIN和RLIKE,如下所示:

SELECT * FROM transactions_filter f, transactions t
WHERE t.Description RLIKE f.Expression

以下是要测试的SQL小提琴:http://sqlfiddle.com/#!9/ba827e/1

注意:?中存在的Expression无法与RLIKE一起使用,其余正则表达式正常。