MySQL:加入SELECT语句问题

时间:2014-08-30 13:23:59

标签: mysql stored-procedures

我创建了以下存储过程:

CREATE PROCEDURE `sp_DBR_Subbie_Cert_Input`(inPlot_ID varchar(25), inSubbie varchar(25), inCertDate date, inCertDetails varchar(50), inCertGross float ) BEGIN


SELECT inPlot_ID as 'Plot[Unit]',   inSubbie as 'Subcontractor[inSub_ID]',  inCertDate as 'Date[inC_Date]',     inCertDetails as 'Details[inC_Det]',    inCertGross as 'Gross[inC_Gross]',  a.tblCert_Number as 'Prev. Cert#',  a.tblCert_Gross as 'Prev Gross[inC_Prev]'

FROM hilmark.tblcertificates_j a

JOIN

(SELECT     max(tblcertificates_j.tblCert_ID) as MaxCertID,
tblcertificates_j.tblCert_XID456 as MaxSubbie,  tblcertificates_j.tblCert_XIDJob as MaxPlot
FROM    hilmark.tblcertificates_j
GROUP BY    tblcertificates_j.tblCert_XIDJob) x
WHERE x.MaxCertID=a.tblCert_ID AND x.MaxPlot=inPlot_ID AND x.MaxSubbie=inSubbie;


END $$

我想要实现的是允许用户为网站的供应商输入新发票。该查询检索该供应商和该站点的最后一张发票。这很有效,除非这是该网站上该供应商的第一张发票 - 没有返回任何内容。我真正需要的是左外连接,但在我的代码中,如果我用左连接替换连接,我会得到语法错误。

我在哪里错了?

2 个答案:

答案 0 :(得分:0)

尝试将查询写为:

SELECT inPlot_ID as `Plot[Unit]`, inSubbie as `Subcontractor[inSub_ID]`,
       inCertDate as `Date[inC_Date]`, inCertDetails as `Details[inC_Det]`,
       inCertGross as `Gross[inC_Gross]`, c.tblCert_Number as `Prev. Cert#`,
       c.tblCert_Gross as `Prev Gross[inC_Prev]`
FROM hilmark.tblcertificates_j c LEFT JOIN
     (SELECT max(c.tblCert_ID) as MaxCertID, c.tblCert_XID456 as MaxSubbie,
             c.tblCert_XIDJob as MaxPlot
      FROM hilmark.tblcertificates_j c
      GROUP BY c.tblCert_XIDJob
     ) cmax
WHERE cmax.MaxCertID = c.tblCert_ID AND cmax.MaxPlot = c.inPlot_ID AND cmax.MaxSubbie = c.inSubbie;

一些注意事项:

  1. 我怀疑这确实是你想要的,因为MaxSubbie具有不确定的价值。但您保存查询工作。你为什么需要两个比较?
  2. 使用表别名时,请将它们作为表的缩写,以便查询更容易阅读。
  3. 仅对字符串和日期常量使用单引号。

答案 1 :(得分:0)

我声明了一个名为Max_CerT_ID的变量,并将其值设置为表格中的Cert_ID,如果它是一个全新的供应商,则设置为零。然后我在case语句中使用这个变量。

DROP PROCEDURE IF EXISTS sp_DBR_Subbie_Cert_Input
$$
CREATE PROCEDURE `sp_DBR_Subbie_Cert_Input`(inPlot_ID varchar(25), inSubbie varchar(25), inCertDate date, inCertDetails varchar(50), inCertGross float )
BEGIN
declare Max_Cert_Id int(11);
set Max_Cert_Id=0;
select max(y.tblCert_ID) into Max_Cert_ID from 
    hilmark.tblcertificates_j y
where y.tblCert_XIDJob=inPlot_ID and y.tblCert_XID456=inSubbie
group by y.tblCert_XIDJob, y.tblCert_XID456 ;
select 'dbr.colstyle',6,'%0.0F';
select 'dbr.colstyle',7,'%0.0F';
select 'dbr.colstyle',8,'%0.0F';
select  inPlot_ID as 'Plot[Unit]',
    inSubbie as 'Subcontractor[inSub_ID]',
    inCertDate as 'Date[inC_Date]',
    case
    when Max_Cert_Id=0 then 1
    else (select a.tblCert_Number from hilmark.tblcertificates_j a where a.tblCert_ID=Max_Cert_ID)+1
    end as 'Cert#',
    upper(inCertDetails) as 'Details[inC_Det]',
    inCertGross as 'Gross[inC_Gross]',
    case
    when Max_Cert_Id=0 then 0
    else (select a.tblCert_Gross from hilmark.tblcertificates_j a where a.tblCert_ID=Max_Cert_ID)
    end as 'Prev. Gross',
    case
    when Max_Cert_Id=0 then inCertGross
    else inCertGross-(select a.tblCert_Gross from hilmark.tblcertificates_j a where a.tblCert_ID=Max_Cert_ID)
    end as 'Change in WIP';

END $$