Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mysql中连接两个表时,我得到的错误是;未知列';m、 机器编号';在';关于第'条&引用;?_Mysql_Sql_Join - Fatal编程技术网

在mysql中连接两个表时,我得到的错误是;未知列';m、 机器编号';在';关于第'条&引用;?

在mysql中连接两个表时,我得到的错误是;未知列';m、 机器编号';在';关于第'条&引用;?,mysql,sql,join,Mysql,Sql,Join,连接两个表时,我执行了一个查询,但它没有正确显示第二个表数据,而是显示了垃圾值 我使用的查询是 select p.machinenumber, max(case when (p.shift)=1 then xxxx end) s1_xxxx, max(case when (p.shift)=2 then xxxx end) s2_xxxx, avg(case when (p.shift)=1 then yyyy end) s1_yyyy , avg(case when (p.shift)=2 t

连接两个表时,我执行了一个查询,但它没有正确显示第二个表数据,而是显示了垃圾值

我使用的查询是

select p.machinenumber,
max(case when (p.shift)=1 then xxxx end) s1_xxxx,
max(case when (p.shift)=2 then xxxx end) s2_xxxx,
avg(case when (p.shift)=1 then yyyy end) s1_yyyy ,
avg(case when (p.shift)=2 then yyyy end) s2_yyyy 
from pdata p
inner  join (select  count(case when (shift)=1 and (ename)=1  then ename end) s1_wa,
count(case when (shift)=1 and (ename)=2 then errorname end) s1_we,
count(case when (shift)=2 and (ename)=1 then errorname end) s2_wa,
count(case when (shift)=2 and (ename)=2 then ename end) s2_we,
count(case when (shift)=1 and (ename)=0 then ename end) s1_merror,
count(case when (shift)=2 and (ename)=0 then ename end) s2_merror  from mstop)m
on p.mnumber=m.mnumber
where date(p.proddate)='2013-02-25'; 

请解决此问题???

如果垃圾来自第二个表,请尝试
左联接
而不是
内部联接
您还需要为
m.mnumber
返回
mnumber
。在mstop表的内部联接中,您还需要返回
mnumber
,以便根据
mnumber
进行联接

inner  join (select  count(case when (shift)=1 and (ename)=1  then ename end) s1_wa,
count(case when (shift)=1 and (ename)=2 then errorname end) s1_we,
count(case when (shift)=2 and (ename)=1 then errorname end) s2_wa,
count(case when (shift)=2 and (ename)=2 then ename end) s2_we,
count(case when (shift)=1 and (ename)=0 then ename end) s1_merror,
count(case when (shift)=2 and (ename)=0 then ename end) s2_merror,
     mnumber  from mstop)m
on p.mnumber=m.mnumber
这是因为您没有在子查询上投影列
mnumber
。您需要
选择子查询中的列名,以便在联接中可见。不要忘记在查询中添加
groupby
子句

select  p.machinenumber,
        max(case when (p.shift)=1 then xxxx end) s1_xxxx,
        max(case when (p.shift)=2 then xxxx end) s2_xxxx,
        avg(case when (p.shift)=1 then yyyy end) s1_yyyy ,
        avg(case when (p.shift)=2 then yyyy end) s2_yyyy 
from    pdata p
        inner  join 
        (
            select  mnumber,
                    count(case when (shift)=1 and (ename)=1  then ename end) s1_wa,
                    count(case when (shift)=1 and (ename)=2 then errorname end) s1_we,
                    count(case when (shift)=2 and (ename)=1 then errorname end) s2_wa,
                    count(case when (shift)=2 and (ename)=2 then ename end) s2_we,
                    count(case when (shift)=1 and (ename)=0 then ename end) s1_merror,
                    count(case when (shift)=2 and (ename)=0 then ename end) s2_merror  
            from    mstop
            GROUP   BY mnumber
        ) m on p.mnumber=m.mnumber
where   date(p.proddate)='2013-02-25'
GROUP   BY  p.machinenumber

老兄,这次我打败了你……对不起,每次我试着写答案时,你都在我面前回答,所以我后来删除了我的答案,但现在我打败了你。@DipeshParmar我对此没有问题(对我来说没什么大不了),但我想知道为什么每次我回答一个问题,你最终都会删除你的答案?我不是要你删除它。我不会在这里与任何其他用户竞争。我想问一下,即使您在子查询中添加了
mnumber
,您是否确定该查询会给出正确的结果<代码>:D
不,决不..我决不删除任何答案,也决不否决任何没有极端条件的人…你可以在我的个人资料中看到,否决投票率要低得多..谢谢你。。我现在没有收到错误,前5列执行正常,但剩余6列未显示..给我们一些可以播放的记录,以便我们可以帮助您解决问题
:D
谢谢您。。我现在没有收到错误,前5列执行正常,但其余6列未显示。。