使用 CASE 或 IF ELSEIF 的 MySQL 选择语句? 不知道如何得到结果

我有两张桌子。其中一个有制造商信息,包括他们可以销售的地区。另一家有他们的产品出售。我们必须根据地区限制产品的可见性。这就像 Netflix 的系统里有只能在任何地方观看的视频(1) ,只能在加拿大(2) ,只能在美国(3)。

我试图做一个查询,告诉我在哪里的产品可以查看基于制造商表中的设置。

例如,在制造商表中,有两个字段,分别称为 exu _ new 和 exu _ used,每个字段的值都为1、2或3,以限制其新的或使用过的视频可以在哪里看到。

当视频被添加时,它们没有被分配一个“暴露”值,这意味着在将它们添加到我们的索引时,根据当前制造商的 expo _ new 或者 expo _ used 值,要动态地进行。

我试图得到的是物品的详细信息和计算出的价值,这些价值是根据物品是新的还是旧的,以及分配给制造商的所有新产品或旧产品的规则/价值计算出来的。我需要在每个产品的基础上有条件地在列表中显示这个单个数字。

下面的内容不起作用,但是您将了解我正在尝试做的事情。我用 CASE 语句和下面的 WRONGIF/ELSEIF 语句尝试了这种方法。

如果您能帮我调试一下这个程序并为我指明正确的方向,我将不胜感激。

SELECT
t2.company_name,
t2.expose_new,  // 1,2 or 3
t2.expose_used, // 1,2 or 3
t1.title,
t1.seller,
t1.status,  //can be new or used
(SELECT
IF(status ='New',
(select expose_new from manufacturers where id = t1.seller),1
)
ELSEIF(t1.status ='Used',
(select expose_used from manufacturers where id = t1.seller),1
)
END IF
) as 'expose'
FROM `products` t1
join manufacturers t2 on t2.id = t1.seller
where t1.seller = 4238

这里有一个 CASE 版本,实际上看起来是执行的,但是不管发生什么事情都会产生第一个值(在本例中为1)。我不确定我是否可以在每个 WHERE 语句中使用 AND 添加另一个测试,但它没有给出错误,只是给出了错误的结果。

SELECT
t2.company_name,
t2.expose_new,
t2.expose_used,
t1.title,
t1.status,
CASE status
when 'New' and t2.expose_new = 1 then 1
when 'New' and t2.expose_new = 2 then 2
when 'New' and t2.expose_new = 3 then 3
when 'Used' and t2.expose_used = 1 then 1
when 'Used' and t2.expose_used = 2 then 2
when 'Used' and t2.expose_used = 3 then 3
END as expose
FROM `products` t1
join manufacturers t2 on t2.id = t1.seller
where t1.seller = 4238
398575 次浏览

Try this query -

SELECT
t2.company_name,
t2.expose_new,
t2.expose_used,
t1.title,
t1.seller,
t1.status,
CASE status
WHEN 'New' THEN t2.expose_new
WHEN 'Used' THEN t2.expose_used
ELSE NULL
END as 'expose'
FROM
`products` t1
JOIN manufacturers t2
ON
t2.id = t1.seller
WHERE
t1.seller = 4238

Syntax:

CASE value WHEN [compare_value] THEN result
[WHEN [compare_value] THEN result ...]
[ELSE result]
END

Alternative: CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...]

mysql> SELECT CASE  WHEN 2>3 THEN 'this is true' ELSE 'this is false' END;
+-------------------------------------------------------------+
| CASE  WHEN 2>3 THEN 'this is true' ELSE 'this is false' END |
+-------------------------------------------------------------+
| this is false                                               |
+-------------------------------------------------------------+

I am use:

SELECT  act.*,
CASE
WHEN (lises.session_date IS NOT NULL AND ses.session_date IS NULL) THEN lises.location_id
WHEN (lises.session_date IS NULL AND ses.session_date IS NOT NULL) THEN ses.location_id
WHEN (lises.session_date IS NOT NULL AND ses.session_date IS NOT NULL AND lises.session_date>ses.session_date) THEN ses.location_id
WHEN (lises.session_date IS NOT NULL AND ses.session_date IS NOT NULL AND lises.session_date<ses.session_date) THEN lises.location_id
END AS location_id
FROM activity AS act
LEFT JOIN li_sessions AS lises ON lises.activity_id = act.id AND  lises.session_date >= now()
LEFT JOIN session AS ses ON  ses.activity_id = act.id AND  ses.session_date >= now()
WHERE act.id

Another way of doing this is using nested IF statements. Suppose you have companies table and you want to count number of records in it. A sample query would be something like this

SELECT IF(
count(*) > 15,
'good',
IF(
count(*) > 10,
'average',
'poor'
)
) as data_count
FROM companies

Here second IF condition works when the first IF condition fails. So Sample Syntax of the IF statement would be IF ( CONDITION, THEN, ELSE). Hope it helps someone.