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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:08:40+00:00 2026-06-09T23:08:40+00:00

I have a database representing something like a bookstore. There’s a table containing the

  • 0

I have a database representing something like a bookstore. There’s a table containing the categories that books can be in. Some categories are defined simply using another table that contains the category-item relationships. But there are also some categories that can be defined programmatically — a category for a specific author can be defined using a query (SELECT item_id FROM items WHERE author = “John Smith”). So my categories table has a “query” column; if it’s not null, I use this to get the items in the category, otherwise I use the category_items table.

Currently, I have the application (PHP code) make this decision, but this means lots of separate queries when we iterate over all the categories. Is there some way to incorporate this dynamic SQL into a join? Something like:

SELECT c.category, IF(c.query IS NULL, count(i.items), count(EXECUTE c.query)
FROM categories c
LEFT OUTER JOIN category_items i
ON c.category = i.category

EXECUTE requires a prepared statement, but I need to prepare a different statement for each row. Also, EXECUTE can’t be used in expressions, it’s just a toplevel statement. Suggestions?

  • 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-09T23:08:42+00:00Added an answer on June 9, 2026 at 11:08 pm

    Here’s how I finally ended up solving this in the PHP client.

    I decided to just keep the membership in the category_items table, and use the dynamic queries during submission to update this table.

    This is the function in my script that’s called to update an item’s categories during submission or updating. It takes a list of user-selected categories (which can only be chosen from categories that don’t have dynamic queries), and using this and the dynamic queries it figures out the difference between the categories that an item is currently in and the ones it should be in, and inserts/deletes as necessary to get them in sync. (Note that the actual table names in my DB are not the same as in my question, I was using somewhat generic terms.)

    function update_item_categories($dbh, $id, $requested_cats) {
        $data = mysql_check($dbh, mysqli_query($dbh, "select id, query from t_ld_categories where query is not null"), 'getting dynamic categories');
        $clauses = array();
        while ($row = mysqli_fetch_object($data))
            $clauses[] = sprintf('select %d cat_id, (%d in (%s)) should_be_in',
                $row->id, $id, $row->query);
        if (!$requested_cats) $requested_cats[] = -1; // Dummy entry that never matches cat_id
        $requested_cat_string = implode(', ', $requested_cats);
        $clauses[] = "select c.id cat_id, (c.id in ($requested_cat_string)) should_be_in
                      from t_ld_categories c
                      where member_type = 'lessons' and query is null";
        $subquery = implode("\nunion all\n", $clauses);
        $query = "select c.cat_id cat_id, should_be_in, (member_id is not null) is_in
                  from ($subquery) c
                  left outer join t_ld_cat_members m
                  on c.cat_id = m.cat_id
                  and m.member_id = $id";
        // printf("<pre>$query</pre>");
        $data = mysql_check($dbh, mysqli_query($dbh, $query), 'getting current category membership');
        $adds = array();
        $deletes = array();
        while ($row = mysqli_fetch_object($data)) {
            if ($row->should_be_in && !$row->is_in) $adds[] = "({$row->cat_id}, $id)";
            elseif (!$row->should_be_in && $row->is_in) $deletes[] = "(cat_id = {$row->cat_id} and member_id = $id)";
        }
        if ($deletes) {
            $delete_string = implode(' or ', $deletes);
            mysql_check($dbh, mysqli_query($dbh, "delete from t_ld_cat_members where $delete_string"), 'deleting old categories');
        }
        if ($adds) {
            $add_string = implode(', ', $adds);
            mysql_check($dbh, mysqli_query($dbh, "insert into t_ld_cat_members (cat_id, member_id) values $add_string"),
                "adding new categories");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a table in my database representing releases of software; the relevant columns
Suppose I have a database containing 500,000 records, each representing, say, an animal. What
I have a form representing a survey that is dynamically generated based on some
I Have a legacy database with a field containing an integer representing a datetime
I have a single database table representing a file tree structure. Files - Id
If I have a class representing access to one table in my database in
I have an Access database of 4M rows, each representing an individual customer order.
I have Database with date field. I see the time like: 1900-01-01 13:38:00.000 How
i have DataBase function that calculate distance by coordinates CREATE OR REPLACE FUNCTION distance(lat1

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.