具有条件的多个WHERE子句

时间:2016-07-12 22:54:02

标签: mysql sql

我已经查看过一堆类似的stackoverflow问题,但我还没有能够解决这个问题。

                    WHERE `Table1`.`Column1` = 'criteria1'
                    AND `Table1`.`Column2` = 'criteria2'
                    AND `Table1`.`Column3` LIKE 'criteria3'
                    AND IF(EXISTS IN `Table2`, (`Table2`.`Delete` <> 2, `Table2`.`SectionId` IS NOT NULL), '')

我尝试使用五个WHERE子句执行查询,其中最后两个子句基于连接的Table2中存在该行的条件。如果条件为假,则抛出空字符串&#39;

我尝试过上述代码的变体,但语法不正确。我试图做的是什么?

我可以在WHERE子句中使用IF吗?

编辑(这里是完整的查询...):

        $sql_query = "SELECT
                    `Courses`.`Id` AS CourseId,
                    IFNULL(`Courses`.`CourseName`, '') AS CourseName,
                    IF(`Courses`.`CourseNumber` > '', `Courses`.`CourseNumber`, -1) AS CourseNumber,
                    IFNULL(`Courses`.`CourseDepartment`, '') AS CourseDepartment,
                    IFNULL(`Courses`.`Notes`, '') AS CourseNotes,
                    IFNULL(`Courses`.`Year`, '') AS CourseYear,
                    IFNULL(`Courses`.`CourseType`, '') AS CourseType,
                    `Sections`.`Id` AS SectionId,
                    IFNULL(`Sections`.`SectionNumber`, '') AS SectionNumber,
                    IFNULL(`Sections`.`SectionName`, '') AS SectionName,

                    `StudentsCourses`.`CustomerId` AS CustomerId,

                    CONCAT(`Courses`.`Id`, '-', `Sections`.`Id`, '-', `StudentsCourses`.`FirstName`, `StudentsCourses`.`LastName`, '_', `StudentsCourses`.`TeacherEmail`) AS TempCustomerId,
                    IFNULL(`StudentsCourses`.`FirstName`, '') AS StudentFirstName,
                    IFNULL(`StudentsCourses`.`LastName`, '') AS StudentLastName,
                    IFNULL(`Customers`.`Email`, IFNULL(`StudentsCourses`.`StudentEmail`, '')) AS StudentEmail,
                    IFNULL(`StudentsCourses`.`TeacherFirstName`, '') AS TeacherFirstName,
                    IFNULL(`StudentsCourses`.`TeacherLastName`, '') AS TeacherLastName,
                    IF(`StudentsCourses`.`CustomerId` IS NOT NULL, `Customers`.`CustomerName`, CONCAT(`StudentsCourses`.`FirstName`, ' ', `StudentsCourses`.`LastName`)) AS FullName,
                    IF(`StudentsCourses`.`CustomerId` IS NOT NULL, CONCAT(REPLACE(`Customers`.`CustomerName`, ' ', ''), '_', `Customers`.`Email`), CONCAT(`StudentsCourses`.`FirstName`, `StudentsCourses`.`LastName`, '_', `StudentsCourses`.`TeacherEmail`)) AS NameKey,
                    `Customers`.`UserRoleId` AS UserRoleId,
                    `Customers`.`MagentoId` AS MagentoId,
                    `StudentsCourses`.`StudentId` AS StudentId,
                    `SiteProfile`.`StudentIdField` AS StudentIdField,
                    `SiteProfile`.`SchoolEmailDomain` AS SchoolEmailDomain,
                    `StudentsCourses`.`Id` AS StudentsCoursesId,
                    IFNULL(`Orders`.`Id`, '') AS OrderId
                FROM `Courses`
                    LEFT JOIN `StudentsCourses` ON `Courses`.`Id` = `StudentsCourses`.`CourseId`
                    LEFT JOIN `BooksCourses` ON `Courses`.`Id` = `BooksCourses`.`CourseId`
                    LEFT JOIN `Products` ON `BooksCourses`.`ISBN` = `Products`.`ISBN`
                    LEFT JOIN `EbookVendors` ON `Products`.`EbookVendorId` = `EbookVendors`.`Id`
                    LEFT JOIN `Sections` ON `Sections`.`Id` = `StudentsCourses`.`SectionId`
                    LEFT JOIN `Customers` ON `StudentsCourses`.`CustomerId` = `Customers`.`Id`
                    LEFT JOIN `SiteProfile` ON `Courses`.`SchoolCode` = `SiteProfile`.`SchoolCode`
                    LEFT JOIN `Orders` ON `Customers`.`Id` = `Orders`.`CustomerId`

                    WHERE `Courses`.`SchoolCode` = '{$criteria["school_code"]}'
                    AND `Courses`.`Year` = {$criteria["year"]}
                    AND `Courses`.`CourseType` LIKE '{$criteria["term"]}'
                    AND `StudentsCourses`.`Delete` <> 2
                    AND `StudentsCourses`.`SectionId` IS NOT NULL";

1 个答案:

答案 0 :(得分:1)

目前还不是很清楚你在问什么,但我猜是喜欢这个条件可能正是你要找的东西:

AND (StudentCourses.CourseId IS NULL 
     OR (`StudentsCourses`.`Delete` <> 2
         AND `StudentsCourses`.`SectionId` IS NOT NULL
        )
    )

编辑:如果你编辑你的问题以便用简单的语言解释你想要条件做什么,那将是最好的,因为根本不清楚你最初提出的语法是什么意思。