开发者

regular expression how to match with backreference two mixed type string

Assume that i have two strings like the following.

$sa = "12,20,45"; $sb = "13,20,50";

I want to check whether any of the number in sa present in sb with back reference so that i can get those numbers back and do some calculation.

The numbers are nothing but unique id's in database. So i am checking whether the ids in sa is present in the list of ids in sb.

Besides if it is possible to get all matching and non matching ids then that would be nice. For this it doesn't have to be one operation. Multiple operations is fine.(like executing match twice or more).

  1. What i am trying to do is i am creating subscribers and they a开发者_StackOverflowre assigned to groups.
  2. I create newsletters and will assign to groups.
  3. If i try to assign a newsletter to the same group then i want the group id so that i can exempt that group and assign that newsletter to the rest.
  4. so if group 15,16,17 are already assigned with a newsletter and the next time i am trying to assign group 15,20,21 i want 15 to be exempted and i want the newsletter to be assigned to 20,21.

And... If i could get a mysql example too then that could be nice.

Any type of answer if it could help the please post it. THX


first of all, this is not a problem you would want to solve with regex. At.all.

Second, you shouldn't have a list of Ids as values in your database, especially if you need to look up on them. It's inefficient and bad database design.

If you only require to link subscribers to newletters these would be the tables you need, one table per entity and a junction table for joining. I have left out the foreign key constraints.

CREATE TABLE Subscribers
(subscriber_id bigint,
first_name varchar(50),
... )

CREATE TABLE Newsletter
(news_letter_id bigint,
name varchar(50),
... )

CREATE TABLE NewslettersSubscribers [or just "Subscriptions"]
(news_letter_id bigint,
subscriber_id bigint,
payment_type smallint,
...[other variables that are specific to this subscription]
)

If you would rather have your subscribers in a group and each subscriber can be in many groups, it would look like this.

CREATE TABLE Subscribers
(subscriber_id bigint,
first_name varchar(50)
... )


CREATE TABLE Group
(group_id bigint,
group_name varchar(50),
... )

CREATE TABLE SubscribersGroups --[or just "Membership"]
(subscriber_id bigint,
group_id bigint,
payment_type smallint,
--...[other variables that are specific to this membership]
)

CREATE TABLE Newsletter
(news_letter_id bigint,
name varchar(50),
... )

CREATE TABLE NewslettersGroups --[or just "SubscriptionGroups"]
(news_letter_id bigint,
group_id bigint
--...[possibly variables that are specific to this subscription group]
)

Now your actions are rather simple. In your example we have newsletter 1, and we have groups 15, 16, 17, 20 and 21 and possibly other groups. We also have these values in NewslettersGroups

| news_letter_id | group_id |
|              1 | 15       |
|              1 | 16       |
|              1 | 17       |

Now you want to connect newsletter 1 to 20 and 21 (only you think you need to do 15 as well). So just insert where it's needed (I'm not 100% sure if this syntax works, I don't use MySQL, but see this reference)

INSERT INTO NewslettersGroups VALUES (1,15),(1,20), (1,21)
ON DUPLICATE KEY UPDATE;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜