Solved

MySQL select, join and math HELP

Posted on 2009-04-07
3
208 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
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 142

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

867 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

21 Experts available now in Live!

Get 1:1 Help Now