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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:45:17+00:00 2026-05-13T14:45:17+00:00

The examples here show a very simplified version of a VIEW that I am

  • 0

The examples here show a very simplified version of a VIEW that I am constructing in MySQL. The goal here is to create a view from which I can select a single set of results to be populated into a HTML table.

The purpose of me using a view is, in part, to show calculations over this data as columns in the view. I can do this easily by giving aliases to expressions, such as: SELECT a, b, a + b AS c will give me a resultset with three columns, of which the values in column c are the addition of values in columns a and b

Example 1

This is how I am currently constructing a VIEW over my fictional “product stock” table:

SELECT
  description,
  unit_cost,
  current_stock,
  required_stock,

  (required_stock - current_stock)                     AS stock_shortfall,
  (required_stock - current_stock) * unit_cost         AS stock_shortfall_cost
FROM product_stock

It is important to me to be able to show both the stock shortfall, and the cost of replacing that stock in the table. You can see that this isn’t fully optimised, as part of the stock shortfall calculation is used again to calculate the cost.

This also means that if something were to change in how the calculation of the stock shortfall was made, the same change would need to be made to the stock shortfall cost calculation, increasing the difficulty of maintaining the code.

Example 2 (Desirable Solution)

I can create a SELECT statement like this which will allow me to reference past expressions using user variables:

SELECT
  description,
  unit_cost,
  current_stock,
  required_stock,

  @stock_shortfall := (required_stock - current_stock)  AS stock_shortfall,
  @stock_shortfall * unit_cost                          AS stock_shortfall_cost
FROM product_stock

I can also achieve a similar effect by using sub-queries on derived tables by referring to the alias of the expression in further calculations.

Unfortunately, MySQL will not allow me to use user variables or derived table sub-queries within a VIEW.

Another solution could be to create a view in place of a derived table, and for each reused expression, create another view “layer” to select the derived value from. This works, but after so many layers, it also becomes unmaintainable.

I am currently performing a mixture of “layered” views and duplicate calculations like in Example #1. Please understand that the actual VIEW I wish to construct is a JOIN across 8 tables, with a lot of calculated values being reused in further expressions throughout. It is not very manageable.

Thanks for reading my wall of text. Here are my questions:

Is it possible to create a VIEW using derived expressions in a similar way to example #2?
If not, how would you solve this problem?

What is the optimum solution? Should I be using a server-side script to calculate the values instead of using the database? Is there another database technology which does support this feature? What would you do?

  • 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-05-13T14:45:18+00:00Added an answer on May 13, 2026 at 2:45 pm

    In the end, I wrote my SELECT statement similar to how I wrote it in my second example (the Desirable Solution) which could not be used to construct a VIEW.

    I then wrote a small compiler to create from that statement a more complex query which could be used to construct the VIEW.

    It is now much easier to maintain.

    Thanks to Rob Van Dam for inspiring me to compile the query.

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

Sidebar

Related Questions

Anybody have good examples of usability disasters? Here's an example. Hector is a manager
Here is an example of what I've got going on: CREATE TABLE Parent (id
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
Here is a specific example which is not CLS-complaint according to VS.NET 2005. Public
Here's a PHP example of mine. Can anyone find a shorter/easier way to do
I am trying create a new frame in wxPython that is a child of
OK so now I can understand that SO's search system is primarily based around
OK, probably best to give an example here of what I mean. Imagine a
ex: <a><strike>example data in here</strike></a> I want everything inside the a tag, to the
Here's a perfect example of the problem: Classifier gem breaks Rails . ** Original

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.