带有CASE或IF ELSEIF的MySQL select语句?不知道如何获得结果

use*_*858 69 mysql select case

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

我正在尝试进行查询,告诉我可以根据制造商表中的设置查看产品的位置.

例如,在制造商表中,有两个名为expose_new和expose_used的字段,每个字段的值为1,2或3,以限制新视频或已用视频的显示位置.

添加视频时,不会为它们分配"公开"值,这是根据当前制造商的expose_new或expose_used值将它们添加到索引时动态完成的.

我想要得到的是项目详细信息以及根据它是新的还是使用的以及为所有新产品或旧产品分配给制造商的规则/值的计算值. 我需要基于每个产品的这个单个数字来有条件地将其显示在列表中.

以下不起作用,但您将了解我正在尝试做什么.我已尝试使用CASE语句和以下错误的IF/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
Run Code Online (Sandbox Code Playgroud)

这是一个CASE版本,实际上似乎执行但总是导致第一个值,无论发生什么情况都是真的(在这种情况下为1).我不确定在每个WHEN语句中我是否可以使用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
Run Code Online (Sandbox Code Playgroud)

Dev*_*art 124

试试这个查询 -

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
Run Code Online (Sandbox Code Playgroud)

  • 如何在where条件下使用“expose”? (3认同)

Lim*_*isa 17

句法:

CASE value WHEN [compare_value] THEN result 
[WHEN [compare_value] THEN result ...] 
[ELSE result] 
END
Run Code Online (Sandbox Code Playgroud)

替代方案: CASE WHEN [条件]然后结果[WHEN [条件]那么结果......]

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                                               | 
+-------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

我用的是:

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
Run Code Online (Sandbox Code Playgroud)