1. <tfoot id='9nVDg'></tfoot><legend id='9nVDg'><style id='9nVDg'><dir id='9nVDg'><q id='9nVDg'></q></dir></style></legend>

    1. <small id='9nVDg'></small><noframes id='9nVDg'>

        <bdo id='9nVDg'></bdo><ul id='9nVDg'></ul>
      <i id='9nVDg'><tr id='9nVDg'><dt id='9nVDg'><q id='9nVDg'><span id='9nVDg'><b id='9nVDg'><form id='9nVDg'><ins id='9nVDg'></ins><ul id='9nVDg'></ul><sub id='9nVDg'></sub></form><legend id='9nVDg'></legend><bdo id='9nVDg'><pre id='9nVDg'><center id='9nVDg'></center></pre></bdo></b><th id='9nVDg'></th></span></q></dt></tr></i><div id='9nVDg'><tfoot id='9nVDg'></tfoot><dl id='9nVDg'><fieldset id='9nVDg'></fieldset></dl></div>
    2. 在 MySQL SELECT 查询中使用 IF 语句

      时间:2023-05-31

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

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

                本文介绍了在 MySQL SELECT 查询中使用 IF 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试在 MySQL 选择查询中使用 IF 语句.

                I am trying to use an IF statement in a MySQL select query.

                AND 语句之后出现错误,其中第一个 IF.

                I am getting an error after the AND statement where the first IF.

                SELECT J.JOB_ID,E.COMPANY_NAME,J.JOB_DESC,JT.JOBTYPE_NAME,J.COMPENSATION,ST.STATE_NAME,MC.METRO_CITY_NAME,I.INDUSTRY_NAME,
                 J.JOB_CONTACT_PERSON,J.DT_INSRT,J.JOB_TITLE,J.JOB_EXP_DATE,J.SKILLS
                 FROM JOBS J 
                 JOIN EMPLOYER E ON J.COMPANY_ID=E.COMPANY_ID 
                 JOIN LOOKUP_JOBTYPE JT ON J.JOB_TYPE=JT.JOBTYPE_ID
                 JOIN LOOKUP_STATE ST ON J.STATE_ID=ST.STATE_ID
                 JOIN JOBS_LOCATION JL ON J.JOB_ID=JL.JOB_ID
                 JOIN LOOKUP_METRO_CITY MC ON JL.METRO_CITY_ID=MC.METRO_CITY_ID
                 JOIN LOOKUP_INDUSTRY I ON J.INDUSTRY_ID=I.INDUSTRY_ID 
                 JOIN JOBS_QUALIFICATION JQ ON J.JOB_ID=JQ.JOB_ID 
                 JOIN LOOKUP_DEGREE_QUALIFICATION LDQ ON LDQ.QUALIFICATION_ID = JQ.QUALIFICATION_ID
                 WHERE  J.ACTIVE='Y' AND J.DT_INSRT > COALESCE(pEmailSntDt,DATE_SUB(SYSDATE(),INTERVAL 4 DAY))  
                AND
                IF(JQ.COURSE_ID=0) 
                THEN
                IF(JQ.DEGREE_ID=0)
                THEN J.SKILLS LIKE CONCAT('%', pSkills,'%')
                ELSE
                JQ.DEGREE_ID=pDegreeId OR J.SKILLS LIKE CONCAT('%', pSkills,'%')
                END IF
                ELSE
                JQ.COURSE_ID=pCourseId OR IF(JQ.DEGREE_ID=0)
                                      THEN
                                      J.SKILLS LIKE CONCAT('%', pSkills,'%')
                                      ELSE
                                      JQ.DEGREE_ID=pDegreeId OR J.SKILLS LIKE CONCAT('%', pSkills,'%')
                                      END IF
                END IF                           
                GROUP BY J.JOB_ID ORDER BY J.DT_INSRT DESC;
                

                为什么这不起作用?在 MySQL 查询中执行 IF 语句的正确方法是什么?

                Why doesn't this work and what is the proper way to do an IF statement in a MySQL query?

                推荐答案

                您使用的 IF/THEN/ELSE 构造仅在存储过程和函数中有效.您的查询将需要重新构造,因为您不能使用 IF() 函数来控制 WHERE 子句的流程,例如这样.

                The IF/THEN/ELSE construct you are using is only valid in stored procedures and functions. Your query will need to be restructured because you can't use the IF() function to control the flow of the WHERE clause like this.

                可以在查询中使用的 IF() 函数主要用于查询的 SELECT 部分,用于根据某些条件选择不同的数据,而不是用于查询的 WHERE 部分:

                The IF() function that can be used in queries is primarily meant to be used in the SELECT portion of the query for selecting different data based on certain conditions, not so much to be used in the WHERE portion of the query:

                SELECT IF(JQ.COURSE_ID=0, 'Some Result If True', 'Some Result If False'), OTHER_COLUMNS
                FROM ...
                WHERE ...
                

                这篇关于在 MySQL SELECT 查询中使用 IF 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:像 MySQL 中区分大小写 下一篇:如何在 MySQL 中将自动增量格式设置为 0001?

                相关文章

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

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

                    <tfoot id='T89fJ'></tfoot>

                    <legend id='T89fJ'><style id='T89fJ'><dir id='T89fJ'><q id='T89fJ'></q></dir></style></legend>