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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:31:20+00:00 2026-05-27T19:31:20+00:00

I have a problem with tables joining. I have two tables: product_commons and product_prices.

  • 0

I have a problem with tables joining.

I have two tables: product_commons and product_prices. Product_prices has records with standard product prices + pre-generated prices for campaigns (campaign_id). I need a query which returns me list of products with campaign prices (if exists) or standard prices (if they not).

Something like this:

SELECT * FROM product_commons 
   INNER JOIN product_prices ON product_commons.id = product_prices.product_id 
WHERE (campaign_id = 3 OR campaign_id IS NULL) 
GROUP BY product_commons.id

Unfortunnely this query returns me prices only with campaign_id = NULL.

  • 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-27T19:31:21+00:00Added an answer on May 27, 2026 at 7:31 pm

    You need to use a mix of INNER and LEFT (OUTER) JOINs with a COALESCE, with the campaign_id filter in the JOIN conditions. To get 2 prices from different rows into one row, you need 2 JOINs.

    SELECT
       *,
       COALESEC(pstd.price, pc.price)
    FROM 
       product_commons pc
       JOIN
       product_prices pstd ON pc.id = pstd.product_id AND pstd.campaign_id IS NULL;
       LEFT JOIN 
       product_prices pp ON pc.id = pp.product_id AND pp.campaign_id = 3;
    

    You don’t need a GROUP BY either: it doesn’t make sense (and would give errors used like this) in standard SQL or other RDBMS

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

Sidebar

Related Questions

Problem I'm joining two tables on postcode. I only have the data for the
I have a problem with mutual constraints. I want to have two tables each
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
i have problem when joining tables(left join) table1: id1 amt1 1 100 2 200
I have two tables (1) MonthlyTarget {SalesManCode, TargetMonthYear, TargetValue}; this table has 1966177 rows.
Hello i have a problem joining tables when i have null values in a
i have a problem with joining three tables in mysql. lets say we have
I have one problem with one SP, when I am joining some specific tables,
I'm wondering if anyone has faced this issue. Let's assume I have two tables:
Ok, I have two tables that I am joining and doing a select on

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.