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

  • Home
  • SEARCH
  • 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 555075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:46:39+00:00 2026-05-13T11:46:39+00:00

I have a rather complex logical query I’m trying to execute. Essentially there are

  • 0

I have a rather complex logical query I’m trying to execute. Essentially there are a series of related tables. I am having difficulty figuring out a good way to approach it.

Tables:

Transaction –
T_id

Discount –
D_id
item_id
Type (percentage or basic value)
value

Transaction_Discount –
T_id
D_id

Item –
item_id
price

EDIT: additional table
Purchase
T_id
item_id

I am attempting to pull the price adjusted after the discount is applied. I would like to avoid adding another column with the adjusted price to the database… as it is possible to calculate the adjusted price for each transaction from the data already entered (so it’d be redundant to store this data).

This is the basic logic:

if(this transaction had a discount applied)  
  //apply the discount and return the adjusted price  
else(no discount applied)  
  //pull price

It is possible to do this in several separate queries using PHP logic.

//1st step - create an array of bool's: true = discount_used/false = no_discount
//2nd step - if(discount_used) return price adjusted to discount
//3rd step - if(no_discount) return price
//4th step - combine the two different arrays

this is clearly very bulky. It seems like there HAS to be a better way to do this that is likely much more efficient.

It is my understanding that you can perform queries that contain logic in mysql, would that help here?

  • 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-13T11:46:39+00:00Added an answer on May 13, 2026 at 11:46 am

    Lets say that you have an item_id on the Transactions Table, and that Percentage is represented by ‘P’, you could try something like

    SELECT  *,
            CASE 
                WHEN td.T_id IS NULL
                    THEN it.price
                WHEN td.T_id IS NOT NULL AND d.Type = 'P'
                    THEN id.price - (id.price * d.value)
                ELSE id.price - d.value
            END PriceDiscounted
    
    FROM    Transaction t LEFT JOIN
            Purchase p ON t.T_id = p.T_id LEFT JOIN
            Item it ON p.item_id = it.item_id LEFT JOIN
            Transaction_Discount td ON t.T_id = td.T_id LEFT JOIN
            Discount d ON td.D_id = d.D_id LEFT JOIN
            Item id ON d.item_id = id.item_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 309k
  • Answers 309k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Because you're returning from the first method call, the second… May 13, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer Solved. I did this: Ego.objects.all().select_related.order_by("auth_user.first_name") May 13, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer The simplest way to do it would be this: const… May 13, 2026 at 9:52 pm

Related Questions

I am in the early stages of developing a sports statistics website (ultimate frisbee)
I have to buil a rather complex WPF UI (a hughe Grid) in code
I'm developing a sim game based on Theme Hospital, which is quite an old
I have a rather complex decorator written by someone else. What I want to
I have a rather complex custom control - the custom control has a couple

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.