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 8522521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:08:37+00:00 2026-06-11T07:08:37+00:00

I’m having trouble crafting a query to return the correct data, and I’m becoming

  • 0

I’m having trouble crafting a query to return the correct data, and I’m becoming less confident that it’s even possible with a single query.

I have log records stored in a MySQL database in very much in the same way that printf() works, except that I must keep the format strings stored separately from the replacement values. What I’d like to do is return this data in the most efficient manner possible, given a search for certain values.

Here’s the table setup:

CREATE TABLE `log` (
  `log_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `message` varchar(255) NOT NULL,
  `num_variables` int(10) unsigned NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`log_id`)
);

CREATE TABLE `variable` (
  `log_id` int(10) unsigned NOT NULL,
  `order` int(10) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `value_id` int(10) unsigned NOT NULL,
  KEY `log_id` (`log_id`),
  KEY `value_id` (`value_id`)
);

CREATE TABLE `value` (
  `value_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `value` varchar(255) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`value_id`),
  UNIQUE KEY `value` (`value`)
);

And here’s an example usage:

log('user %email% invited %num% new players', 'him@example.com', 2);

which would lead to the following queries:

-- create the log record (resulting PK would be 1)
INSERT INTO log
(message, num_variables)
VALUES
('user %email% invited %num% new players', 'him@example.com', '2');

-- create the first value record (resulting PK would be 1)
INSERT INTO value
(value)
VALUES
('him@example.com');

-- create the first variable record (resulting PK would be 1)
INSERT INTO variable
(log_id, order, name, value_id)
VALUES
(1, 0, 'email', 1);

-- create the second value record (resulting PK would be 2)
INSERT INTO value
(value)
VALUES
('2');

-- create the second variable record (resulting PK would be 2)
INSERT INTO variable
(log_id, order, name, value_id)
VALUES
(1, 1, 'num', 2);

Now I want to be able to pull log records back out of the database, with their associated variables and values. Specifically, I need the log message, and all it’s associated values:

SELECT  log.id, log.message
        variable.order, variable.name
        value.value_id, value.value
FROM log
LEFT JOIN variable ON (log.log_id = variable.log_id)
LEFT JOIN value ON (variable.value_id = value.value_id)

This works fine if I want ALL log records (ignoring the fact that log.log_id and log.message are returned redundantly for any logs with multiple variables). But I want more specificity.

To borrow from the example above, I want to be able to specify that I only want log records containing an “email” of “him@example.com”, let’s say. When I add that into my query…

SELECT  log.log_id, log.message
        variable.order, variable.name
        value.value_id, value.value
FROM log
LEFT JOIN variable ON (log.log_id = variable.log_id)
LEFT JOIN value ON (variable.value_id = value.value_id)
WHERE (variable.name = 'email' AND value.value = 'him@example.com')

It will return that log/variable/value record, but it will NOT return the associated “num = 2” record (which is required to fully reconstruct the log). Additionally, suppose I wanted to specify a second constraint, say, where “action” = “logged out”. I could (incorrectly) alter my WHERE clause to look like this:

-- won't return anything
WHERE (variable.name = 'email' AND value.value = 'him@example.com')
AND (variable.name = 'action' AND value.value = 'logged out')

or this:

-- will also return logs containing only ONE of the given constraints
WHERE (variable.name = 'email' AND value.value = 'him@example.com')
OR (variable.name = 'action' AND value.value = 'logged out')

but in either case, you can see that it misses the mark, and doesn’t return the exact result set I’m looking for.

Are my tables are poorly (or under- or over-) designed? Am I approaching the query the wrong way? Would storing a field of derived data somewhere give me what I need? Is there some JOIN I’ve failed to use that would solve the problem?

UPDATE 1:

variable.order and variable.name are just two different methods for assuring that the values are interpolated back into log.message correctly.

UPDATE 2:

Based on comments, it’s worth noting that these tables are a contrived example to simplify the post – the actual table structure is slightly more complex than presented. I’ve merely reduced that complexity down to the very kernel of the issue. Simple use-a-single-table-and-serialize-the-value techniques won’t work for me. Aside from that, we need to be able to lookup these logs based on values pretty quickly, and such a solution wouldn’t provide us the proper indexing capabilities.

  • 1 1 Answer
  • 0 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-06-11T07:08:39+00:00Added an answer on June 11, 2026 at 7:08 am

    How about:

    ...
    WHERE log.id IN (SELECT l.id 
                     FROM log l 
                     INNER JOIN variable v ON l.log_id = v.log_id
                     INNER JOIN value vv ON v.value_id = vv.value_id
                     WHERE v.name = 'email' and vv.value = 'him@example.com')
    

    Without knowing a bigger sample of data, I can’t really comment on the table design. At this point I do question separating out the variables and values tables unless this is a one-to-many relationship variables->values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

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.