SQLite:条件Where子句取决于行存在

时间:2016-06-15 10:46:32

标签: sqlite select where-clause

我有一个相当复杂的SQLite查询:

  

选择TDELIVER.ROWID,TDELIVER.DELSERN,COMSERNO,DELDATASRC,DELDATE,(选择总和(CASE WHEN(DELEORDUNI =='CASE'或PRODCOMP == 1)和DELEACTVOL> 0和PRODPALLET = 0然后DELEACTVOL / PRODSIZE WHEN(DELEORDUNI =='CASE'或PRODCOMP == 1)和DELEACTVOL> 0那么DELEACTVOL ELSE 0 END)来自TDELENTR内部加入TPRODUCT在TDELENTR.PRODSERN = TPRODUCT.PRODSERN其中DELSERN = TDELIVER.DELSERN)NumberOfPalletsDelivered,(选择总和) (CASE WHEN(DELEORDUNI =='CASE'或PRODCOMP == 1)和DELEACTVOL< 0那么DELEACTVOL ELSE 0 END)来自TDELENTR内部加入TPRODUCT在TDELENTR.PRODSERN = TPRODUCT.PRODSERN其中DELSERN = TDELIVER.DELSERN)NumberOfPalletsCompensated,(选择总和(CASE WHEN(DELEORDUNI!='CASE'和PRODCOMP!= 1)和DELEACTVOL> 0 THEN ROUND(DELEACTVOL / PRODSIZE,0)ELSE 0 END)来自TDELENTR内部联接TPRODUCT在TDELENTR.PRODSERN = TPRODUCT.PRODSERN DELSERN = TDELIVER.DELSERN)NumberOfCasesDelivered,(选择总和(CASE WHEN(DELEORDUNI!='CASE'和PRODCOMP!= 1)a和DELEACTVOL< 0那么DELEACTVOL / PRODSIZE ELSE 0 END)来自TDELENTR内部联接TPRODUCT在TDELENTR.PRODSERN = TPRODUCT.PRODSERN其中DELSERN = TDELIVER.DELSERN)NumberOfCasesCompensated,(从TDELENTR内部联接选择总和(DELEACTVOL * DELEPRICE)TPRODUCT在TDELENTR.PRODSERN = TPRODUCT .PRODSERN DELSERN = TDELIVER.DELSERN)TotalPriceOfDelivery,

<?php
$message = 'sevenflash';

$public_key = openssl_get_publickey(file_get_contents('pubkey.pem'));

$encrypted = $e = NULL;
openssl_seal($message, $encrypted, $e, array($public_key));

$sealed_data = base64_encode($encrypted);
$envelope = base64_encode($e[0]);

$unsealed = null;
openssl_open(base64_decode($sealed_data), $unsealed, base64_decode($envelope), file_get_contents('privatekey.pem'));
echo $unsealed; // This is sevenflash

$embed = "<script>var encrypted = '$sealed_data'; var envelope = '$envelope'; </script>";
?>
<html>
<head>
    <script type="text/javascript" src="rc4.js"></script>
    <script type="text/javascript" src="enc-base64.js"></script>
    <script type="text/javascript" src="jsencrypt/bin/jsencrypt.js"></script>
    <?php echo $embed; ?>
    <script>
        var private_key = `...`;

        var crypt = new JSEncrypt();
        crypt.setPrivateKey(private_key);
        var theKey = crypt.decrypt(envelope);

        var decrypted = CryptoJS.RC4.decrypt(encrypted, theKey);
        console.log(decrypted.toString()); // This must be sevenflash, but this is 696a2b0440220d69e8c3
    </script>
</head>
<body>

</body>
</html>

除其他外,还会返回品牌列表。但是,有时,replace(group_concat(distinct TPGROUPS.PGRPVAL), ',', ', ') as Brands from TDELIVER inner join TDELENTR on TDELENTR.DELSERN=TDELIVER.DELSERN inner join LPROPGR on LPROPGR.PRODSERN=TDELENTR.PRODSERN inner join TPGROUPS on TPGROUPS.PGRSERN= LPROPGR.PGRSERN where COMSERNO='501000000002118' and PGRPNAME COLLATE NOCASE in (select SETVALUE from TSETTING where SETKEY = 'ProductGroup') GROUP BY TDELIVER.ROWID 不会返回1,在这种情况下,我想运行一个更简单的查询:

  

选择TDELIVER.ROWID,TDELIVER.DELSERN,COMSERNO,DELDATASRC,DELDATE,(选择总和(CASE WHEN(DELEORDUNI =='CASE'或PRODCOMP == 1)和DELEACTVOL&gt; 0和PRODPALLET = 0然后DELEACTVOL / PRODSIZE WHEN(DELEORDUNI =='CASE'或PRODCOMP == 1)和DELEACTVOL&gt; 0那么DELEACTVOL ELSE 0 END)来自TDELENTR内部加入TPRODUCT在TDELENTR.PRODSERN = TPRODUCT.PRODSERN其中DELSERN = TDELIVER.DELSERN)NumberOfPalletsDelivered,(选择总和) (CASE WHEN(DELEORDUNI =='CASE'或PRODCOMP == 1)和DELEACTVOL&lt; 0那么DELEACTVOL ELSE 0 END)来自TDELENTR内部加入TPRODUCT在TDELENTR.PRODSERN = TPRODUCT.PRODSERN其中DELSERN = TDELIVER.DELSERN)NumberOfPalletsCompensated,(选择总和(CASE WHEN(DELEORDUNI!='CASE'和PRODCOMP!= 1)和DELEACTVOL&gt; 0 THEN ROUND(DELEACTVOL / PRODSIZE,0)ELSE 0 END)来自TDELENTR内部联接TPRODUCT在TDELENTR.PRODSERN = TPRODUCT.PRODSERN DELSERN = TDELIVER.DELSERN)NumberOfCasesDelivered,(选择总和(CASE WHEN(DELEORDUNI!='CASE'和PRODCOMP!= 1)a和DELEACTVOL&lt; 0那么DELEACTVOL / PRODSIZE ELSE 0 END)来自TDELENTR内部联接TPRODUCT在TDELENTR.PRODSERN = TPRODUCT.PRODSERN其中DELSERN = TDELIVER.DELSERN)NumberOfCasesCompensated,(从TDELENTR内部联接选择总和(DELEACTVOL * DELEPRICE)TPRODUCT在TDELENTR.PRODSERN = TPRODUCT .PRODSERN DELSERN = TDELIVER.DELSERN)TotalPriceOfDelivery

SELECT EXISTS(SELECT 1 FROM TSETTING WHERE SETKEY="ProductGroup" LIMIT 1);

(我使用blockquotes来显示相同的代码,除了第一个块末尾的逗号外)

我的问题是,我如何才能运行一个查询并拥有一个列'品牌',如果from TDELIVER where COMSERNO='501000000002118' 为假,则返回时没有数据或行中有空字符串,如果是真的则返回品牌列表?

一些屏幕截图有助于说明问题:

简单案例:

Simple Case

复杂案例:

Complex Case

1 个答案:

答案 0 :(得分:1)

要根据条件执行两个不同的查询,请使用compound query

SELECT * FROM (SELECT ...)
WHERE x
UNION ALL
SELECT * FROM (SELECT ...)
WHERE NOT x;