mysqlsqlsubqueryleft-joinhaving-clause

how to get mysql values based on left join condition


job table

1.id
2.status = 'active'
3.name

repair table

1.repair id
2.job_id
3.process = 'yes|no'
4.status  = '1|2'

job table

id   name  status
1    test  active
2    check active

repair table

repair_id     job_id  process  status
1                1      no        2
2                1      no        1
3                1      yes       2
4                2      no        1
5                2      no        2

here i need to show data which ( process != 'yes' and repair_status != 2 ) group by job_id

i need result after query

---------------------------------------------
job_id    name( job.name ) status( job.status )
------------------------------------------------
2            check           active

Solution

  • In order to get the results that you specify, you mean that process is not yes for any row for the job_id. And then that at least one row has a status <> 2. That would be:

    select j.job_id, j.name, j.status
    from repair r join
         job j
         on r.job_id = r.id
    group by j.job_id, j.name, j.status
    having max(process) = 'no' and
           min(repair_status) = 1;