SQL查询如果行计数为空,则执行另一个查询

时间:2018-03-05 10:35:36

标签: sql join left-join not-exists

我正在尝试访问2个表,1个是作业表,另一个是分配给作业表的调用表。

基本上并非所有的工作都有分配给他们的工作表,而且我最初这样做的方式很好,如果已分配工作表但不计算没有工作表的工作单。

这是我的原始代码:

$getQuery = $pdo->prepare("
SELECT 
sheet.id, sheet.jid, sheet.jd,sheet.tos,sheet.toffs,sheet.miles,sheet.mrate,sheet.r1,sheet.r2,sheet.r3,sheet.r4,sheet.r5,sheet.r6,sheet.r7,sheet.r8,job.cid, job.cc,job.ref,job.ac,job.reference,job.status,job.jdesc 
FROM 
jSheets sheet 
LEFT JOIN jobs job ON(sheet.jid = job.id) 
WHERE job.cc=:cc AND jd >= :jd AND jd <= :jd2 
ORDER BY id DESC
");
$getQuery->execute(array(':cc' => $typecostcode, ':jd' => $postDateFrom, ':jd2' => $postDateToo));

我试图改变这一点,以便我的逻辑如果它返回不存在/行计数= 0然后再做另一个查询。

但是我不能让它发挥作用。

我一直在谷歌搜索的其他几个网站上看这个网站,但无法掌握使这个工作所需的查询。

以下是我认为如果没有工作表存在则可以解决问题的代码,然后再进行一次查询:

$getQuery = $pdo->prepare("
SELECT 
job.id,job.cid,job.cc,job.ref,job.ac,job.reference,job.status,job.jdesc,sheet.tos,sheet.toffs,sheet.miles,sheet.mrate,sheet.r1,sheet.r2,sheet.r3,sheet.r4,sheet.r5,sheet.r6,sheet.r7,sheet.r8 
FROM jobs job 
LEFT JOIN jSheets sheet ON(job.id = sheet.jid) 
WHERE job.cc=:cc AND sheet.jd >= :jd AND sheet.jd <= :jd2 
AND NOT EXISTS (
job.id,job.cid,job.cc,job.ref,job.ac,job.reference,job.status,job.jdesc 
FROM jobs 
WHERE job.cc=:cc AND job.jobdate >= :jd AND sheet.jobdate <= :jd2) 
ORDER BY id DESC
");
$getQuery->execute(array(':cc' => $typecostcode, ':jd' => $postDateFrom, ':jd2' => $postDateToo));

我无法让它工作,不确定它的查询或执行绑定是因为我只收到以下错误:

  

警告:PDOStatement :: execute():in   第152行的C:\ wamp \ www \ Applications \ Controllers \ Jobs \ report.php

但我只使用3个变量,即使我做了jd,jd2,jd3,jd4,我仍然会得到同样的错误。

---编辑---

在接受3BS关于使用IF语句的说法之后,我将代码更改为以下内容:

$getQuery = $pdo->prepare("
                IF(
                    SELECT COUNT (*)
                    FROM jsheets sheet 
                    LEFT JOIN jobs job ON(sheet.jid = job.id) 
                    WHERE sheet.jd >= :jd AND sheet.jd <= :jd2 LIMIT 1
                ) > 0
                BEGIN
                    SELECT job.id,job.cid,job.cc,job.ref,job.ac,job.reference,job.status,job.jdesc,sheet.tos,sheet.toffs,sheet.miles,sheet.mrate,sheet.r1,sheet.r2,sheet.r3,sheet.r4,sheet.r5,sheet.r6,sheet.r7,sheet.r8 FROM jobs job LEFT JOIN jsheets sheet ON(job.id = sheet.jid) WHERE sheet.jd >= :jd AND sheet.jd <= :jd2
                END
                ELSE
                BEGIN
                    SELECT id,cid,cc,ref,ac,reference,status,jdesc FROM jobs WHERE jobdate >= :jd AND jobdate <= :jd2) ORDER BY id DESC
                END
            ");
            $getQuery->execute(array(':jd' => $postDateFrom, ':jd2' => $postDateToo));

我仍然收到以下警告,并且我不知道是否是查询或PDO执行问题:

  

警告:PDOStatement :: execute():in   第152行的C:\ wamp \ www \ Applications \ Controllers \ Jobs \ report.php

作业表格列如下:

id,cid,cc,ref,ttotal,wtotal,ac,qble,reference,status,gen,jdesc,noengineer,jobdate

工作表列如下:

id,jid,jd,tos,toffs,miles,mrate,tt,r1,r2,r3,r4,r5,r6,r7,r8,status

---编辑结束---

任何指针都将非常感激:)

由于

2 个答案:

答案 0 :(得分:0)

编辑:在你解释你使用MySQL之后,我的回答是与sql-server相关的。以下是MySQL中的If条件使用,IF(表达式,true,false):

select  IF( (
            SELECT COUNT jd
            FROM jsheets sheet 
            LEFT JOIN jobs job ON(sheet.jid = job.id) 
            WHERE sheet.jd >= :jd AND sheet.jd <= :jd2 LIMIT 1  
            ) > 0 ,
            (
            SELECT job.id,job.cid,job.cc,job.ref,job.ac,job.reference,job.status,job.jdesc,sheet.tos,sheet.toffs,sheet.miles,sheet.mrate,sheet.r1,sheet.r2,sheet.r3,sheet.r4,sheet.r5,sheet.r6,sheet.r7,sheet.r8
            FROM jobs job LEFT JOIN jsheets sheet ON(job.id = sheet.jid)
            WHERE sheet.jd >= :jd AND sheet.jd <= :jd2
            FROM jobs job
            LEFT JOIN jsheets sheet ON(job.id = sheet.jid) WHERE sheet.jd >= :jd AND sheet.jd <= :jd2
            ),
            (
            SELECT id,cid,cc,ref,ac,reference,status,jdesc
            FROM jobs WHERE jobdate >= :jd AND jobdate <= :jd2)
            )
        )
 ORDER BY id DESC

---结束编辑---

这里有SQL语法错误

AND NOT EXISTS (
job.id,job.cid,job.cc,job.ref,job.ac,job.reference,job.status,job.jdesc 

当使用带有子查询的NON EXIST子句时,子查询必须具有逻辑意义,它应该是:

AND NOT EXISTS ( 
SELECT job.id,job.cid,job.cc,job.ref,job.ac,job.reference,job.status,job.jdesc 

答案 1 :(得分:0)

我认为您不需要其他查询。我认为你只需要正确使用@Override public void onMeasure(final int origWidthMeasureSpec, final int origHeightMeasureSpec) { final int specWidthSize = MeasureSpec.getSize(origWidthMeasureSpec); final int specWidthMode = MeasureSpec.getMode(origWidthMeasureSpec); final int maxWidth = getTabMaxWidth(); ... } 。将条件放在left join子句中的 second 表中:

on

如果您想保留所有作业,那么这应该是SELECT s.id, s.jid, s.jd, s.tos, s.toffs, s.miles, s.mrate, s.r1, s.r2, s.r3, s.r4, s.r5, s.r6, s.r7, s.r8, j.cid, j.cc, j.ref, j.ac, j.reference, j.status, j.jdesc FROM jSheets s LEFT JOIN jobs j ON s.jid = j.id AND j.cc = :cc WHERE s.jd >= :jd AND s.jd <= :jd2 ORDER BY s.id DESC; 中的第一个表:

LEFT JOIN