Solved

MySQL select, join and math HELP

Posted on 2009-04-07
3
207 Views
Last Modified: 2013-12-12
I have 4 tables: 'devices', 'inventory', 'discounts', and 'device_discounts_xref'

I want to select all the devices and order them by discounted price.  Some phones have discounts some do not.

The query i have is kind of working, but the devices that don't have discounts have a price of NULL.  I want the price to be the actual price of the device.
SELECT

	`devices`.*,

	`inventory`.`quantity`,

	ROUND(`devices`.`price` - `discounts`.`amount`,2) AS `sort_price`

FROM

	`devices`

LEFT JOIN `inventory` ON `inventory`.`sku`=`devices`.`sku`

LEFT JOIN `device_discounts_xref` ON `device_discounts_xref`.`device_id`=`devices`.`id`

LEFT JOIN `discounts` ON `discounts`.`id`=`device_discounts_xref`.`discount_id`

WHERE

	`devices`.`active`='1'

ORDER BY `sort_price`

Open in new window

0
Comment
Question by:fastball1945
3 Comments
 
LVL 15

Accepted Solution

by:
oobayly earned 495 total points
Comment Utility
Use IFNULL for the discounts. Basically, if the discounts.amount == null, use 0 instead:
SELECT

        `devices`.*,

        `inventory`.`quantity`,

        ROUND(`devices`.`price` - IFNULL(`discounts`.`amount`, 0),2) AS `sort_price`

FROM

        `devices`

LEFT JOIN `inventory` ON `inventory`.`sku`=`devices`.`sku`

LEFT JOIN `device_discounts_xref` ON `device_discounts_xref`.`device_id`=`devices`.`id`

LEFT JOIN `discounts` ON `discounts`.`id`=`device_discounts_xref`.`discount_id`

WHERE

        `devices`.`active`='1'

ORDER BY `sort_price`

Open in new window

0
 

Author Comment

by:fastball1945
Comment Utility
Perfect!  Thanks for the timely response!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
try this:
SELECT
      `devices`.*,
      `inventory`.`quantity`,
      ROUND(`devices`.`price` - COALESCE(`discounts`.`amount`,0) ,2) AS `sort_price`
FROM
      `devices`
LEFT JOIN `inventory` ON `inventory`.`sku`=`devices`.`sku`
LEFT JOIN `device_discounts_xref` ON `device_discounts_xref`.`device_id`=`devices`.`id`
LEFT JOIN `discounts` ON `discounts`.`id`=`device_discounts_xref`.`discount_id`
WHERE
      `devices`.`active`='1'
ORDER BY `sort_price`

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now