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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:53:30+00:00 2026-06-09T01:53:30+00:00

So, I have a confusing MySQL issue. I feel like I need to use

  • 0

So, I have a confusing MySQL issue. I feel like I need to use some IF statements, but I’m really not sure how to implement them into this situation! First, consider the following query. It’s simple:

SELECT *
FROM flow
INNER JOIN flow_strings
    USING(node_id)
WHERE
    (
        flow.parent = 0
        OR flow.parent = :user_flow
    )
    AND flow.source = 0
    AND :input LIKE flow_strings.sql_regex

However, I need to expand it, and that’s where I’m stuck. Thinking through this, I’m not really sure how to explain it, so following are the table structures, and then some examples.

TABLE flow

+---------+--------+--------+
| node_id | parent | source |
+---------+--------+--------+
|    1    |   0    |    0   |
+---------+--------+--------+
|    2    |   0    |    0   |
+---------+--------+--------+
|    3    |   1    |    1   |
+---------+--------+--------+
|    4    |   3    |    0   |
+---------+--------+--------+

TABLE flow_strings

+----------------+---------+-----------+
| flow_string_id | node_id | sql_regex |
+----------------+---------+-----------+
|        1       |    1    |   fish    |
+----------------+---------+-----------+
|        2       |    1    |   wish    |
+----------------+---------+-----------+
|        3       |    1    |   *yes*   |
+----------------+---------+-----------+
|        4       |    2    |   *no*    |
+----------------+---------+-----------+
|        5       |    2    |   nay     |
+----------------+---------+-----------+
|        6       |    3    |   *herp*  |
+----------------+---------+-----------+

[ ... ]

TABLE placeholder_variables

+-------------+--------+------+-------+
| variable_id | source | name | value |
+-------------+--------+------+-------+
|      1      |    0   | yes  | sure  |
+-------------+--------+------+-------+
|      2      |    0   | yes  | yeah  |
+-------------+--------+------+-------+
|      3      |    0   | no   | nope  |
+-------------+--------+------+-------+
|      4      |    1   | herp | derp  |
+-------------+--------+------+-------+

NOW, here’s what I need to happen based on :input.


"fish", "wish", "sure", or "yeah" —SELECT flow.node_id 1

  • This is because "fish", "wish", and "*yes*" are all associated with flow.node_id 1. Note that *yes* is surrounded by asterisks, so instead of "yes" being interpreted literally, it instead draws the values from placeholder_variables.

"nope" or "nay" —SELECT flow.node_id 2

  • This is because "*no*" and "nay" are associated with flow.node_id 2. Again, because of the asterisks, "no" is not interpreted literally, but "nope" matches because "no" is in the placeholder_variables table, even though "nope" is not in the flow_strings table.

"no" and "*no*" —NO MATCH

Even though *no* is in flow_strings, it should not match because it has asterisks around it (and a corresponding placeholder_variable) which means it should not be interpreted literally, and so can only be evaluated by its corresponding placeholder variable’s value(s).


"baby" —NO MATCH

  • Even though "baby" does not have asterisks around it, it corresponds to flow.node_id 3, and that node’s flow.source is 1.

"derp" —NO MATCH

  • This is because placeholder_variables.source is 1 for *herp*, even though it is in the flow_strings table.

"*herp*" —SELECT flow.node_id 4

  • Even though there are asterisks around *herp* in the flow_strings table, the corresponding placeholder_variable.source is 1.

** TO SUM UP **

  1. No source = 1
  2. Interpret placeholder_variables if the sql_regex is surrounded by asterisks, but only if the corresponding placeholder_variable’s source is 0.
  3. If source is 0, and no asterisks are present, interpret sql_regex literally.

I know that I can use MySQL’s SUBSTRING() to work with the asterisks. I also know that, (as I am using PHP) I could theoretically split this into two queries, and then dynamically generate the second query by looping through the first. However, this would be both A) memory intensive, and B) sloppy.

So my question is this: Is it possible to do this using MySQL alone? If yes, how would you recommend I format it? You don’t need to write the query for me, but if you could help me with some of the logic, I’d be very grateful! I have absolutely no idea what to try besides what I have already outlined, and I definitely don’t want to do that.

Thanks!

  • 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-09T01:53:32+00:00Added an answer on June 9, 2026 at 1:53 am

    You can use this solution:

    SELECT    a.*,
              COALESCE(c.value, b.sql_regex) AS string #-- If there was a successful JOIN (sql_regex was a variable), then display the value of the "value" column in placeholder_variables, otherwise if the JOIN did not succeed (sql_regex was a literal value), just display sql_regex instead.
    FROM      flow a
    JOIN      flow_strings b ON a.node_id = b.node_id
    LEFT JOIN placeholder_variables c #-- LEFT JOIN placeholder_variables on these conditions:
           ON b.sql_regex LIKE '*%*' AND -- That sql_regex is a variable
              REPLACE(b.sql_regex, '*', '') = c.name AND -- Match sql_regex up with the "name" column in placeholder_variables. We must replace the asterisks in sql_regex so that the values can match ("name" column do not contain asterisks)
              c.source = 0
    WHERE     a.source = 0 AND
              COALESCE(c.value, b.sql_regex) = :input -- If the string was interpreted as a placeholder variable, make the condition on the interpreted value in the placeholder_variables table ("name" column), otherwise, just make the condition on the sql_regex column.
    

    SQLFiddle Demo

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

Sidebar

Related Questions

I have an SQL question which may be basic to some but is confusing
I have been starting to write some reasonably large and or confusing MySQL queries
I have a MySQL stored routine where I'd like to use a temporary data
The title may have been confusing, but please let me explain: Currently when I
I know this sounds confusing: I have just built some basic prev/next pagination for
One thing I have continually found very confusing about using an object database like
I have a setup, something confusing like this: <ListView> <ItemTemplate> <div id=Content> <asp: TextBox
This might be a confusing question but I have written below a Directory crawler,
The question might seem a bit confusing but I have an if statement that
I'm terribly bad at keeping MySQL queries straight, but that aside I have one

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.