• <legend id='6HcMh'><style id='6HcMh'><dir id='6HcMh'><q id='6HcMh'></q></dir></style></legend>
  • <tfoot id='6HcMh'></tfoot>
    1. <small id='6HcMh'></small><noframes id='6HcMh'>

      • <bdo id='6HcMh'></bdo><ul id='6HcMh'></ul>
      <i id='6HcMh'><tr id='6HcMh'><dt id='6HcMh'><q id='6HcMh'><span id='6HcMh'><b id='6HcMh'><form id='6HcMh'><ins id='6HcMh'></ins><ul id='6HcMh'></ul><sub id='6HcMh'></sub></form><legend id='6HcMh'></legend><bdo id='6HcMh'><pre id='6HcMh'><center id='6HcMh'></center></pre></bdo></b><th id='6HcMh'></th></span></q></dt></tr></i><div id='6HcMh'><tfoot id='6HcMh'></tfoot><dl id='6HcMh'><fieldset id='6HcMh'></fieldset></dl></div>

      1. 带有 CASE 或 IF ELSEIF 的 MySQL 选择语句?不知道如何得到结果

        时间:2023-05-24

          <tfoot id='obKL3'></tfoot>

        1. <small id='obKL3'></small><noframes id='obKL3'>

            <bdo id='obKL3'></bdo><ul id='obKL3'></ul>

              <tbody id='obKL3'></tbody>

              • <i id='obKL3'><tr id='obKL3'><dt id='obKL3'><q id='obKL3'><span id='obKL3'><b id='obKL3'><form id='obKL3'><ins id='obKL3'></ins><ul id='obKL3'></ul><sub id='obKL3'></sub></form><legend id='obKL3'></legend><bdo id='obKL3'><pre id='obKL3'><center id='obKL3'></center></pre></bdo></b><th id='obKL3'></th></span></q></dt></tr></i><div id='obKL3'><tfoot id='obKL3'></tfoot><dl id='obKL3'><fieldset id='obKL3'></fieldset></dl></div>
              • <legend id='obKL3'><style id='obKL3'><dir id='obKL3'><q id='obKL3'></q></dir></style></legend>
                  本文介绍了带有 CASE 或 IF ELSEIF 的 MySQL 选择语句?不知道如何得到结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

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

                  I have a two tables. One has manufacturer information and includes the regions where they can sell. The other has their products for sale. We have to limit visibility of the product based on the regions. This is like Netflix have videos in their system that can only be viewed Everywhere (1), only in Canada (2), only in USA (3).

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

                  I am trying to make a query that tells me where the product can be viewed based on the settings in the manufacturer table.

                  例如,在制造商表中,有两个字段,称为expose_new 和expose_used,每个字段的值都为1,2 或3,以限制可以看到他们的新视频或使用过的视频的位置.

                  For example, in the manufacturer table, there are two fields called expose_new and expose_used each of which will have a value of 1,2 or 3 to limit where their new or used videos can seen.

                  当视频被添加时,它们没有被分配一个expose"值,这是在将它们添加到我们的索引时根据当前制造商的 Exposure_new 或 Exposure_used 值即时完成的.

                  When the videos are added, they are not assigned an 'expose' value and this is meant to be done on the fly when adding them to our index depending on the current manufacturer's expose_new or expose_used values.

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

                  What I am trying to get is the item details and the computed value for where it can be seen based on whether it is new or used and the rule/value assigned to the manufacturer for all their new or used products. I need this single digit on a per-product basis to conditionally display it in a list.

                  以下内容不起作用,但您会了解我正在尝试做什么.我已经用 CASE 语句和以下 WRONG IF/ELSEIF 语句尝试过这个.

                  The following does not work, but you will get the idea of what I am trying to do. I have tried this with CASE statements and the following WRONG IF/ELSEIF statement.

                  任何帮助调试并为我指明正确方向的帮助将不胜感激.

                  Any help to debugger this and point me in the right direction would be appreciated.

                  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),它总是产生第一个值.我不确定是否可以在每个 WHEN 语句中添加另一个带有 AND 的测试,但它不会给出错误,只会给出错误的结果.

                  Here is a CASE version that actually seems to execute but always results in the first value no matter what happens to be true (in this case 1). I am not sure that I can have the addition of another test with the AND in each WHEN statement but it does not give an error, only the wrong result.

                  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
                  

                  推荐答案

                  试试这个查询 -

                  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
                  

                  这篇关于带有 CASE 或 IF ELSEIF 的 MySQL 选择语句?不知道如何得到结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:phpMyAdmin - 无法连接 - 无效设置 - 自从我添加了 root 密码 - 被锁定 下一篇:mySQL 子查询限制

                  相关文章

                1. <legend id='jv7cA'><style id='jv7cA'><dir id='jv7cA'><q id='jv7cA'></q></dir></style></legend>
                    • <bdo id='jv7cA'></bdo><ul id='jv7cA'></ul>
                  1. <i id='jv7cA'><tr id='jv7cA'><dt id='jv7cA'><q id='jv7cA'><span id='jv7cA'><b id='jv7cA'><form id='jv7cA'><ins id='jv7cA'></ins><ul id='jv7cA'></ul><sub id='jv7cA'></sub></form><legend id='jv7cA'></legend><bdo id='jv7cA'><pre id='jv7cA'><center id='jv7cA'></center></pre></bdo></b><th id='jv7cA'></th></span></q></dt></tr></i><div id='jv7cA'><tfoot id='jv7cA'></tfoot><dl id='jv7cA'><fieldset id='jv7cA'></fieldset></dl></div>
                    <tfoot id='jv7cA'></tfoot>

                      <small id='jv7cA'></small><noframes id='jv7cA'>