Doctrine Priority选择取决于字段值

时间:2015-09-15 12:06:50

标签: php sql doctrine-orm doctrine

关于此查询的结果集:

从[My_Table]

中选择*
type WorkflowBuilder() = 
    member x.Bind
        (workflow: Workflow<'Env, 'OldResult>,
         selector: 'OldResult -> Workflow<'Env, 'NewResult>) 
         : Workflow<'Env, 'NewResult> =
         fun env -> <@ %(selector (%(workflow env)) env) @>
    member __.Return(x) = fun Env -> <@ x @>
    member __.ReturnFrom(x: Workflow<_, _>) = x
    member __.Quote(expr: Expr<Workflow<'Env, 'Result>>) = expr
    // This run method fails
    member __.Run(x : Expr<Workflow<'Env, 'Result>>) : Workflow<'Env, 'Result> = fun (env: Expr<'Env>) -> <@ %((%x) env) @>

let workflow = new WorkflowBuilder()

// Env of type int for testing
let getRandomNumber (kernel: Expr<int>) = <@ (new Random()).Next() @> 

let workflow1 = workflow {
    let! randomNumber = getRandomNumber
    let otherValue = 2
    let! randomNumber2 = getRandomNumber
    return randomNumber + otherValue + randomNumber2
}
// This fails due to quotation slicing issue
workflow1 <@ 0 @>

我需要选择满足这些条件的行(不同的Fk_ID):

如果代码与FK_id| name | code | ___________________ 1 | first | 21500 | ___________________ 1 | first | 0 | ___________________ 2 | second | 21500 | ___________________ 4 | four | 21500 | ___________________ 4 | four | 42000 | ___________________ 4 | four | 0 | ___________________ 5 | fifth | 0 | ___________________ 5 | fifth | 42000 | ___________________ 6 | six | 0 | ___________________ 不同,我们会使用我们想要的代码(它是查询的参数) 如果没有等于此参数的行,我们将使用0

这就是我需要的:

如果0

code = 21500

如果Fk_id| name | code | ___________________ 1 | first | 21500 | ___________________ 2 | second | 21500 | ___________________ 4 | four | 21500 | ___________________ 5 | fifth | 0 | ___________________ 6 | six | 0 | ___________________

code = 42000

我需要先拥有SQL查询,不需要拥有doctrine Query构建器。

1 个答案:

答案 0 :(得分:1)

我添加了一个id列来简化PHPMyAdmin的版本,但它在这里没有任何作用。

  

如果code = 21500

SELECT * FROM `test`
WHERE (`code` = 21500 OR `code` = 0)
GROUP BY `name`
ORDER BY `FK_id`

Demo at SQL Fiddle

结果:

| id | FK_id |   name |  code |
|----|-------|--------|-------|
|  1 |     1 |  first | 21500 |
|  3 |     2 | second | 21500 |
|  4 |     4 |   four | 21500 |
|  7 |     5 |  fifth |     0 |
|  9 |     6 |    six |     0 |
  

如果code = 42000

SELECT *, MAX(code) FROM `test`
WHERE (`code` = 42000 OR `code` = 0)
GROUP BY `name`
ORDER BY `FK_id`

Demo at SQL Fiddle

结果:

| id | FK_id |  name |  code | MAX(code) |
|----|-------|-------|-------|-----------|
|  2 |     1 | first |     0 |         0 |
|  5 |     4 |  four | 42000 |     42000 |
|  7 |     5 | fifth |     0 |     42000 |
|  9 |     6 |   six |     0 |         0 |

我使用MAX()来获得第一行结果42000而不是0

在两个查询中,您都可以将GROUP BY `name`替换为GROUP BY `FK_id`