为mysql中的两个不同字段获取相同的名称

时间:2015-03-20 12:33:59

标签: mysql

这是我的小调查询,我想两次获取相同的字段名称processNm和 在nxtProcess我想从processmaster获取名称,但我没有得到任何输出

在我的进程表中我有两个相同的字段processMasterId,nextProcessId在这个字段中从processmaster表中插入相同的id我想要获取他的名字。但我不能得到任何输出..

SELECT designMaster.designMasterId,dNo,dWet,nextProcess,itemnm,SUM(moldNo) AS moldNo
                 ,mold,status,processNm AS preProcess,processNm AS nxtProcess,nextProcessId
     FROM   designMaster,item,moldmaster,processmaster,process
     WHERE  item.itemId = designMaster.itemId
     AND   process.processMasterId = process.nextProcessId
     AND   process.designMasterId = designMaster.designMasterId
     AND  moldmaster.designMasterId = designMaster.designMasterId
     AND  processmaster.processMasterId = designMaster.nextProcess
    GROUP BY designMaster.designMasterId
    ORDER BY designMaster.designMasterId

1 个答案:

答案 0 :(得分:1)

您的查询无效。你不正确地使用GROUP BY。除SUM()之外的所有列都必须属于该组。

create table designMaster (
    designMasterId number,
    nextProcess number,
    itemId number,
    dNo number
);

create table item (
    itemId number,
    itemnm number
);

create table moldmaster (
    designMasterId number,
    moldNo number,
    mold number    
);

create table process (
    processId number,
    processMasterId number,
    processDate date,
    workerId number,
    designMasterId number,
    desWet number,
    dWet number,
    rDate date,
    reciveWet number,
    rejWet number,
    nextProcessId number,
    status number
);

create table processmaster (
    processMasterId number,
    processNm number,
    sequence number
);

SELECT designMaster.designMasterId,dNo,dWet,nextProcess,itemnm,SUM(moldNo) AS moldNo
        ,mold,status,processNm AS preProcess,processNm AS nxtProcess,nextProcessId
 FROM   designMaster,item,moldmaster,processmaster,process
 WHERE  item.itemId = designMaster.itemId
 AND   process.processMasterId = process.nextProcessId
 AND   process.designMasterId = designMaster.designMasterId
 AND  moldmaster.designMasterId = designMaster.designMasterId
 AND  processmaster.processMasterId = designMaster.nextProcess
GROUP BY designMaster.designMasterId,dNo,dWet,nextProcess,itemnm,mold,status,processNm,nextProcessId
ORDER BY designMaster.designMasterId;