MySQL Select语句---显示所有这些,除非这个---不太确定如何做到这一点

时间:2012-07-30 15:28:21

标签: php mysql

我正在尝试创建一个SQL查询,它将选择显示的所有内容......但我的条件是总小时数!= 0。这有效,但我必须再添加一个条件,排除总小时数!= 0。

我基本上需要查询看起来像:AND project_timecard_task_days.total_hours!= 0 UNLESS task.type =“Fixed Rate”

因此,声明的“除非”部分是问题。任何帮助将不胜感激!

SELECT project_timecard_tasks.task_id, 
       project_timecard_tasks.datetime, 
       project_timecard_tasks.total_hours, 
       project_timecard_tasks.user_id, 
       project_timecard_tasks.project_id, 
       users.user_id, 
       users.firstname, 
       users.lastname, 
       users.billingrate, 
       tasks.id, 
       tasks.taskname, 
       tasks.billingoption, 
       tasks.fixedrate, 
       tasks.rate_schedule_id, 
       hourlyrates.rate_schedule_id, 
       hourlyrates.hourlyrate, 
       project_timecard_tasks.project_task_id, 
       project_timecard_task_days.project_task_id, 
       project_timecard_task_days.project_task_day_id, 
       project_timecard_task_days.total_hours, 
       project_timecard_task_days.comments, 
       project_timecard_task_days.invoiced 
FROM   project_timecard_tasks 
       JOIN users 
         ON project_timecard_tasks.user_id = users.user_id 
       JOIN tasks 
         ON project_timecard_tasks.task_id = tasks.id 
       JOIN project_timecard_task_days 
         ON project_timecard_tasks.project_task_id = 
            project_timecard_task_days.project_task_id 
       JOIN hourlyrates 
         ON project_timecard_tasks.billing_code_id = hourlyrates.hourly_rate_id 
WHERE  project_timecard_tasks.project_id = '$jobNumber' 
       AND project_timecard_task_days.total_hours != 0 
       AND project_timecard_task_days.invoiced != 1 

3 个答案:

答案 0 :(得分:3)

...AND (project_timecard_task_days.total_hours != 0 OR task.type ="Fixed Rate")...

答案 1 :(得分:1)

只需使用OR

WHERE
(
  project_timecard_tasks.project_id = '$jobNumber' 
  AND 
  project_timecard_task_days.total_hours != 0 
  AND 
  project_timecard_task_days.invoiced != 1 
)
OR
  task.type ="Fixed Rate"

或者(取决于你想要的条件):

WHERE
  project_timecard_tasks.project_id = '$jobNumber' 
  AND 
  project_timecard_task_days.invoiced != 1 
  AND 
  (
    project_timecard_task_days.total_hours != 0
    OR
    task.type ="Fixed Rate"
  )

答案 2 :(得分:0)

使用OR声明。

...
AND (project_timecard_task_days.total_hours != 0 OR task.type = "Fixed Rate")
AND...