2016-07-12 20 views
0

これまで同様のスタックオーバーフローに関する質問がありましたが、これを把握することはできませんでした。条件付きのWHERE句が複数ある場合

    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), '') 

私は最後の二つの行が参加しました表2に存在することを条件に基づいている5つのWHERE句でクエリを実行しようとしています。条件がfalseの場合は、空の文字列 ''をスローします。

私は上記のコードのバリエーションを試しましたが、構文が正しくありません。私がしようとしていることは可能ですか?

IFをWHERE節のように使用できますか?

EDIT(ここでは、完全なクエリがあります...):

 $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"; 
+0

それは存在しないのですが、その構文がどこかで使用されていたことを知っていましたか、まったく推測していましたか? [EXISTSのMySQLドキュメント](http://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html) – Uueerdo

+0

Table2にはどのフィールドが必要ですか?あなたは完全な質問を投稿できますか? – Yuri

+0

..そして「空の文字列を投げて」何のために?これらはブール条件であり、フィールド選択ではありません。 – Uueerdo

答えて

1

あなたが求めている正確に何を非常に明確ではありませんが、私はこの状態があなたが何であるかであるかもしれないのようなものを推測していますあなたはそれがすべてでは明白ではないよう意図はSYから何であったか、あなたは条件が何をしたいのか平易な言葉で説明するのあなたの質問を編集した場合、それが最善だろう:

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

編集:探しあなたが最初に提示したntax。

+0

これはおそらく正しいですが、それは私のためには機能しませんでした。私は私の全体の質問をやり直す必要があると思います。 – LXXIII

関連する問題