OK this is a bit tricky but basically in my SQL statement im comparing prices between fields in each row to get the lowest value returned, now the issue is that each value has a link.
Would there be a way to return the correct link depending on the lowest value in an SQL statement?
below is my SQL statement.
SELECT
title,
my_value,
my_ink,
LEAST(site_a_value, site_b_value, site_c_value) AS lowest_value,
site_a_link,
site_b_link,
site_c_link
FROM STOCK
so for example if site_b had the lowest value I would only want site_b_link to be returned.
Try
SELECT title,
my_value,
my_ink,
LEAST(site_a_value, site_b_value, site_c_value) AS lowest_value,
case LEAST(site_a_value, site_b_value, site_c_value)
when site_a_value then site_a_link
when site_b_value then site_b_link
when site_c_value then site_c_link end AS lowest_value_link,
FROM STOCK
Try this:
SELECT
title, my_value, my_ink,
LEAST(site_a_value, site_b_value, site_c_value) AS lowest_value,
(
CASE site_a_value = LEAST(site_a_value, site_b_value, site_c_value)
THEN site_a_link
ELSE CASE site_b_value = LEAST(site_a_value, site_b_value, site_c_value)
THEN site_b_link
ELSE
site_c_link
) AS site_link
FROM STOCK