1

好的,这有点棘手,但基本上在我的 SQL 语句中,我比较每行中的字段之间的价格以获得返回的最低值,现在问题是每个值都有一个链接。

有没有办法根据 SQL 语句中的最小值返回正确的链接?

下面是我的 SQL 语句。

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

因此,例如,如果 site_b 具有最低值,我只希望返回 site_b_link。

4

2 回答 2

4

尝试

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 
于 2012-07-25T15:00:07.803 回答
1

尝试这个:

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
于 2012-07-25T15:00:55.793 回答