Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4007026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:35:02+00:00 2026-05-20T08:35:02+00:00

I have a very particular problem and after looking at many ressources, i’m unable

  • 0

I have a very particular problem and after looking at many ressources, i’m unable to find a solution to my problem.

The MySQL version i’m running is MySQL 5.0.91

Given the following tables definition :

DROP TABLE IF EXISTS `item`;
CREATE TABLE `item` (
  `id` int(11) NOT NULL default '0',
  `code` varchar(4096) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `item` VALUES ('1', 'pizza|large|pepp');
INSERT INTO `item` VALUES ('3', 'pizza|medium|pepp');
INSERT INTO `item` VALUES ('2', 'pizza|small|pepp');
INSERT INTO `item` VALUES ('4', 'appetizer|fries|large');
INSERT INTO `item` VALUES ('5', 'beverage|2_liter|pepsi');
INSERT INTO `item` VALUES ('6', 'pizza|small|cheese');

DROP TABLE IF EXISTS `item_regexp`;
CREATE TABLE `item_regexp` (
  `id` int(11) NOT NULL default '0',
  `regexp` varchar(4096) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `item_regexp` VALUES ('1', '((pizza)\\\\|)((large|medium)\\\\|)');
INSERT INTO `item_regexp` VALUES ('2', '((pizza)\\\\|)((.*)\\\\|)((alldressed))');
INSERT INTO `item_regexp` VALUES ('3', '((beverage)\\\\|)((2_liter)\\\\|)');
INSERT INTO `item_regexp` VALUES ('4', '((pizza)\\\\|)((.*)\\\\|)((pepp))');

In summary, table item represents items on an invoice. In my example, I have 5 items. The code is an internal representation of that specific Item.

Then, the item_regexp table are used to specify possible product. This can be used for example to capture all possible product that satisfy a given code pattern to apply a discount, etc.

I would like to load all the item_regexp entries with the number of item each can capture from the list of items on an invoice.

Doing subquery to count the number of entries in table item that are captured by my regexp, gives me a proper result :

################
# QUERY #1     #
################
SELECT 
(SELECT
count(*)
FROM
item
where 
`item`.`code` REGEXP '((pizza)\\|)((large|medium)\\|)') as "regexp1 count"
,
(SELECT
count(*)
FROM
item
where 
`item`.`code` REGEXP '((pizza)\\|)((.*)\\|)((alldressed))') as "regexp2 count"
,
(SELECT
count(*)
FROM
item
where 
`item`.`code` REGEXP '((beverage)\\|)((2_liter)\\|)') as "regexp3 count" 
,
(SELECT
count(*)
FROM
item
where 
`item`.`code` REGEXP '((pizza)\\|)((.*)\\|)((pepp))') as "regexp4 count" ;
+---------------+---------------+---------------+---------------+
| regexp1 count | regexp2 count | regexp3 count | regexp4 count |
+---------------+---------------+---------------+---------------+
|             2 |             0 |             1 |             3 |
+---------------+---------------+---------------+---------------+
1 row in set

However, running this as a subquery within a more general query seemed to always give 0 as a count. This is as if the regexp was not working or not taken into account.

################
# QUERY #2     #
################
SELECT 
`item_regexp`.`regexp`
, 
(
SELECT
count(*)
FROM
item
where 
`item`.`code` REGEXP `item_regexp`.`regexp`
) as "regexp_count"
FROM 
item_regexp ;
+-------------------------------------+--------------+
| regexp                              | regexp_count |
+-------------------------------------+--------------+
| ((pizza)\\|)((large|medium)\\|)     |            0 |
| ((pizza)\\|)((.*)\\|)((alldressed)) |            0 |
| ((beverage)\\|)((2_liter)\\|)       |            0 |
| ((pizza)\\|)((.*)\\|)((pepp))       |            0 |
+-------------------------------------+--------------+
4 rows in set

Is there something I’ve missed it the process so that QUERY #2 yield the same count value as QUERY #1 ?

Thanks for your help.

Mike

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:35:03+00:00Added an answer on May 20, 2026 at 8:35 am

    That is because when you define a literal

    ((beverage)\\|)((2_liter)\\|)
    

    REGEXP sees the double \ as a single .
    When you put it in a column, they are double \s, so it is equivalent to the literal

    ((beverage)\\\\|)((2_liter)\\\\|)
    

    Which makes them different. You had it right with the literals, but your insert into item_regexp is wrong. Try the below instead

    delete from `item_regexp`;
    INSERT INTO `item_regexp` VALUES ('1', '((pizza)\\|)((large|medium)\\|)');
    INSERT INTO `item_regexp` VALUES ('2', '((pizza)\\|)((.*)\\|)((alldressed))');
    INSERT INTO `item_regexp` VALUES ('3', '((beverage)\\|)((2_liter)\\|)');
    INSERT INTO `item_regexp` VALUES ('4', '((pizza)\\|)((.*)\\|)((pepp))');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After many cases of trial and error, I have concluded that the only solution
I have a very simple question. I want to test whether a particular port
I have a particular application that needs to calculate something very specific, and while
We have very strange problem, one of our applications is continually querying server by
i have very simple problem. I need to create model, that represent element of
I have a problem with a single C# project in a solution of 21
I want someone to help me, I have a case that seems very particular
I have a very weird problem. I have a webpage that displays some graphs
So I have a very weird problem. I am writing some code in VB.Net
Is it particularly bad to have a very, very large SQL query with lots

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.