Jump to content

[SOLVED] Using variable variables in mysql


artacus

Recommended Posts

My coworker built a non-normalized database and now I'm trying to do reports w/o having to do a ton of queries.

 

The main table I'll be using is category_map. Its a normalized table that gives me the level, category & sub-category. Now I've got about 5 non-normalized tables that are related. So I added a `field_name` field to category_map table. I store the table_name.field_name that each category is mapped to.

 

So a row in my category_map might look like this:

cat_id     description                    category        level     field_name                                             
---------  -----------------------------  --------------  --------  ------------------------------------------------------ 
40         Secretary                      Classified      JH        tbl_class_allocations_jh.secretary       
16         Office/Attendance Assistant    Classified      EL        tbl_class_allocations_el.office_attendance_assistant   

 

So my question is, "Is there any way to treat that field_name like you would a "variable variable" in PHP. So when the query runs I could get back the value stored in tbl_class_allocations_jh.secretary and not the string "tbl_class_allocations_jh.secretary"???

Link to comment
Share on other sites

Yeah, I wanted to use a @var as a field name. But it seems you can use variables for field and table names.

 

I couldn't use a prepared statement because the value of the variable would change for each row.

 

I ended up with two choices, normalizing the tables and recreating the old ones as views (I didn't want to recode the application); or writing a stored procedure to pull the value. I ended up normalizing the tables and creating views.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.