Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL select, join and math HELP

Posted on 2009-04-07
3
Medium Priority
?
223 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 1980 total points
ID: 24091897
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
ID: 24091907
Perfect!  Thanks for the timely response!
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24091912
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this article, we’ll look at how to deploy ProxySQL.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

783 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