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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:18:13+00:00 2026-05-20T08:18:13+00:00

I came up with the following query to return product pricing data. This query

  • 0

I came up with the following query to return product pricing data. This query works fine, although I’m wondering if there is a way to simplify/optimize it?

The query pulls info about the product and pricing details for each product. If there is no pricing details (p_p.pl_id = 2) for particular product join another query with pricing details (p_p.pl_id = 1). Finally group all products by id to get distinct rows.

Tables used:
products – some details about the products like name, description etc.
product_pricing (p_p) – contains pricing details for every product. There may be more that one records for one product because product price varies depending on ordered quantity

SELECT * FROM (
(
SELECT `products`.*, 
GROUP_CONCAT(p_p.`qty_range` SEPARATOR '|') AS qty_range, 
GROUP_CONCAT(CAST(p_p.`p_price` AS CHAR) SEPARATOR '|') AS price 
FROM `products`
LEFT JOIN `prod_pricing` AS p_p ON p_p.`prod_id` = `products`.`prod_id` 
WHERE `products`.`account_id` = 2
AND p_p.`pl_id` = 2
GROUP BY `products`.`prod_id`
) 
UNION
(
SELECT `products`.*, 
GROUP_CONCAT(p_p.`qty_range` SEPARATOR '|') AS qty_range, 
GROUP_CONCAT(CAST(p_p.`p_price` AS CHAR) SEPARATOR '|') AS price 
FROM `products`
LEFT JOIN `prod_pricing` AS p_p ON p_p.`prod_id` = `products`.`prod_id` 
WHERE `products`.`account_id` = 2
AND p_p.`pl_id` = 1
GROUP BY `products`.`prod_id`
)) AS list
GROUP BY list.`prod_id`
  • 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-20T08:18:14+00:00Added an answer on May 20, 2026 at 8:18 am

    Ok, I finally figured it out.
    I created two temporary tables t1,t2 that reflect data with pl_id = 1 and 2. Next I joined those tables to the products table and with IF condition returned products with pricing from pl_id = 2 and pl_id =1 only when pricing for pl_id = 2 doesn’t exist.
    Thanks to everyone for help!

    Here’s the query

    SELECT p.*,
    IF(t2.pl_id IS NULL,t1.qty_range,t2.qty_range) AS qty_range,
    IF(t2.pl_id IS NULL,t1.price,t2.price) AS price
    FROM products AS p
    LEFT JOIN
    (
        SELECT p_p.prod_id,
        GROUP_CONCAT(p_p.qty_range SEPARATOR '|') AS qty_range, 
        GROUP_CONCAT(CAST(p_p.p_price AS CHAR) SEPARATOR '|') AS price 
        FROM prod_pricing AS p_p WHERE p_p.pl_id = 1
        GROUP BY p_p.prod_id
    ) AS t1 ON t1.prod_id = p.prod_id
    LEFT JOIN
    (
        SELECT p_p.prod_id,p_p.pl_id,
        GROUP_CONCAT(p_p.qty_range SEPARATOR '|') AS qty_range, 
        GROUP_CONCAT(CAST(p_p.p_price AS CHAR) SEPARATOR '|') AS price 
        FROM prod_pricing AS p_p WHERE p_p.pl_id = 1
        GROUP BY p_p.prod_id
    ) AS t2 ON t2.prod_id = p.prod_id 
    WHERE p.account_id = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
I came across the following http://ckeditor.com/demo , and was wondering if anyone had a
Recently I came across with the following quiz. Imagine we have this table +--------+
While researching Oracle Analytics, I came across this query: select an_id, a_date, min(a_date) over
I came up with following query which is not so challenging to resolve but
I came across the following MySQL query: update 'table' set itemId=:itemId, startDate=:startDate where id=:id
How to find data for the following query Original Source question 89 Table structure:
This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY
I came across following code and don't know what does having from twice mean
I am going through C and came to following example which I could not

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.