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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:58:29+00:00 2026-05-26T22:58:29+00:00

My first table is an item table. (invoice_inventory) My second table is the pricelist

  • 0

My first table is an item table. (invoice_inventory)
My second table is the pricelist (inventory_prices), there I have a default price list (company_id = 0) and several special price lists (but not for every company).

With the following query I try to get all items with their prices for the company_id = 2.
It should use the special price list (company_id = 2 if it exists) else it should use the default price (company_id = 0).

I thought by GROUPING + ORDERING it would work, but I forgot that GROUP BY gets executed before ORDER.

I think I may need a subquery, but I am not sure.

SELECT invoice_inventory.*, invoice_inventory_prices.* 
FROM invoice_inventory
LEFT JOIN invoice_inventory_prices 
   ON  `invoice_inventory`.`inventory_id`
      = `invoice_inventory_prices`.`inventory_id`
WHERE (company_id = 2 OR company_id = 0)
GROUP BY invoice_inventory.inventory_id 
ORDER BY company_id DESC
  • 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-26T22:58:29+00:00Added an answer on May 26, 2026 at 10:58 pm

    I am going to use two LEFT JOINs the first will go get the Default prices dp alias and the second will get the Special prices (company_id=2) sp alias

    The CASE is used to determine if a Special price exists, use it, otherwise use the default.

    You did not give a column list, so I took a guess at the name of the price column. If you require a second (or more) column, you can add additional CASE statements.

    SELECT i.inventory_id
       , CASE
            WHEN sp.Price IS NOT NULL THEN sp.Price
            ELSE dp.Price
         END AS PriceForItem
    FROM invoice_inventory AS i
    LEFT JOIN invoice_inventory_prices AS dp ON  i.inventory_id = dp.inventory_id
       AND dp.company_id = 0
    LEFT JOIN invoice_inventory_prices AS sp ON  i.inventory_id = sp.inventory_id
       AND sp.company_id = 2
    GROUP BY invoice_inventory.inventory_id 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, when I check a item in first table it adds
Imagine I have a table called item that has a column called price. In
I have database with many tables. In the first table, I have a field
I have a performance problem on a query. First table is a Customer table
I have a database framework where I have two tables. The first table has
So I have a database with a few tables. The first table contains the
I have the following style in an external CSS file called first.css table {
There is a table Item like, code,name 01,parent1 02,parent2 0101,child11 0102,child12 0201,child21 0202,child22 Create
I have defined attribute is_first for table items. In model Item, i want to
I am trying to write a query that connects 3 tables. The first table

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.