sql >> データベース >  >> RDS >> Mysql

WHEREステートメントなしのINT比較

    これを試してください:

    SELECT
      emp.name AS '## Name',
      (SELECT COUNT(*)     
       FROM project p JOIN employee e ON p.number = e.number
       WHERE e.name = emp.name 
         AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 0
       ) AS 'Day 0'
      ,(SELECT COUNT(*)
        FROM project p JOIN employee e ON p.number = e.number
        WHERE e.name = emp.name 
          AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 1
       ) AS 'Day 1'
      ,(SELECT COUNT(*)
        FROM project p JOIN employee e ON p.number = e.number
        WHERE e.name = emp.name 
          AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 2
       ) AS 'Day 2'
      ,(SELECT COUNT(*)
        FROM project p JOIN employee e ON p.number = e.number
        WHERE e.name = emp.name 
          AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 3
       ) AS 'Day 3'
      ,(SELECT COUNT(*)
        FROM project p JOIN employee e ON p.number = e.number
        WHERE e.name = emp.name 
          AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 4
       ) AS 'Day 4'
      ,(SELECT COUNT(*)
        FROM project p JOIN employee e ON p.number = e.number
        WHERE e.name = emp.name 
          AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) >= 5
       ) AS 'Day 5'
    FROM employee emp
    GROUP BY emp.name
    

    SQLFiddleデモ を参照してください。 (-提供された情報に基づいてデータにいくつかの仮定をしました。)



    1. 複数の変数を持つmysqlテーブル構造

    2. Oracleにテーブルがまだ存在しない場合にテーブルを作成する2つの方法

    3. OracleSQLでタイムスタンプを日付に変換する

    4. SQL Serverエージェントスケジュールの変更(T-SQL)