9

Say I have the following table:

TABLE: product
===============================================================================
| product_id | language_id | name           | description                     |
===============================================================================
| 1          | 1           | Widget 1       | Really nice widget. Buy it now! |
-------------------------------------------------------------------------------
| 1          | 2           | Lorem  1       |                                 |
-------------------------------------------------------------------------------

How do I query this such that it tries to give me the name and description where language_id = 2, but fall back to language_id = 1 if the column contains a NULL?

In the above example, I should get Lorem 1 for name and Really nice widget. Buy it now! for description.

4

5 回答 5

8

这个怎么样?

SET @pid := 1, @lid := 2;
SELECT 
    COALESCE(name,(
        SELECT name
        FROM product
        WHERE product_id = @pid AND description IS NOT NULL
        LIMIT 1
    )) name, 
    COALESCE(description,(
        SELECT description
        FROM product
        WHERE product_id = @pid AND description IS NOT NULL
        LIMIT 1
    )) description
FROM product
WHERE product_id = @pid 
    AND (language_id = @lid 
    OR language_id = 1)
ORDER BY language_id DESC
LIMIT 1;

在哪里:

  • @pid: 当前产品编号
  • @lid: 当前语言 ID
  • name和/或的值description可能为空
  • language_id= 2 项无法存在
于 2011-04-10T09:06:16.590 回答
1
select name, description from product
where product_id = @pid
  and name is not null
  and description is not null
  and (language_id = @lang or language_id = 1)
order by language_id desc

其中@pid是当前产品 ID,@lang是当前语言 ID。

返回的第一行将包含当前名称和描述。

这假定该行将language_id = 1不包含NULL名称或描述。

于 2011-04-09T23:59:07.320 回答
0
select p2.product_id
      ,coalesce(p2.name, p1.name, 'No name') as name
      ,coalesce(p2.description, p1.description, 'No description') as description
  from product p2
  left join product p1 on(
       p1.product_id = p2.product_id
   and p1.language_id = 1
  )
 where p2.product_id  = 1
   and p2.language_id = 2;

Edit1:
上面的查询假定存在 language=2 行但名称/描述可能为空。

编辑 2.
我只记得最近有人问过类似的问题。然后我发现是。您需要将产品与翻译分开。这就是使这个查询难以编写的原因。托马斯的回答让你很容易做你想做的事。

于 2011-04-09T23:59:17.147 回答
0

下面是一个使用 SQL 更新的例子:

它类似于 Oracle 的 NVL。您可以使用参数在准备好的语句中使用它,如下所示

UPDATE
    tbl_cccustomerinfo
SET
    customerAddress = COALESCE(?,customerAddress),
    customerName =  COALESCE(?,customerName),
    description =  COALESCE(?,description)
WHERE
    contactNumber=?
于 2013-09-29T20:06:03.560 回答
0

假设:每个产品都有一个条目,language = 1 下面的代码只是简单的 sql 来检索你想要的东西。

另一个主题是如果您想要您请求的行为.. 因为您可以在name和之间使用混合语言description。我会以不同的方式设计它,如果两个字段之一为空,我将默认返回主要语言 (1)。

select p.product_id,
  coalesce(pl.name, p.name, 'No name') as name,
  coalesce(pl.description, p.description, 'No description') as description
from product p
  left join product pl on (pl.product_id = p.product_id and pl.language_id = :language_id)
where p.product_id = :product_id and p.language_id = 1
于 2013-02-02T09:02:49.650 回答