MySQL内部连接查询:输出不正确

时间:2016-04-25 03:52:18

标签: mysql

我有一个mysql查询,我在查询中使用内部联接。由于一些未知的原因,它没有像我预期的那样工作。我不确定查询有什么问题。任何人都可以告诉我我做错了什么。

这是查询。

curosr.hasNext()

如果我运行此查询,那么我想我会得到SELECT f.uid, a.uid, b.uid, c.uid, d.uid, e.uid, f.date, a.email, a.fname, a.lname, a.mobile, a.pic, a.address FROM users a inner join friends b inner join exp c inner join skill d inner join personaldetails e inner join jobs f on a.uid = b.uid =c.uid = d.uid=e.uid=f.uid where f.job_id= 22 and f.ignored=0 and a.fname like '%Rah%' order by f.date DESC

但我得到了a.uid =1, b.uid =1, c.uid =1, d.uid=1, f.uid =1

3 个答案:

答案 0 :(得分:1)

你试过拆分吗?

select f.uid,a.uid,b.uid,c.uid,d.uid,e.uid,f.date,a.email,a.fname,a.lname,a.mobile,a.pic,a.address
from users a 
    inner join friends b on (a.uid = b.uid)
    inner join exp c on (b.uid = c.uid)
    inner join skill d on (c.uid = d.uid)
    inner join personaldetails e on (d.uid = e.uid)
    inner join jobs f on (e.uid = f.uid)
where f.job_id= 22 and f.ignored=0 and a.fname like '%Rah%' order by f.date DESC

答案 1 :(得分:1)

请检查其中一个表格中是否有重复项。我确信是这样的。如果以下查询返回一条记录,那么你将有罪魁祸首。

SELECT f.uid, a.uid, b.uid, c.uid,
       d.uid, e.uid,
       f.date, a.email, a.fname, a.lname,
       a.mobile, a.pic, a.address
FROM   users a 
inner join friends b 
inner join exp c 
inner join skill d 
inner join personaldetails e 
inner join jobs f 
    on a.uid = b.uid =c.uid = d.uid=e.uid=f.uid 
where f.job_id= 22 
    and f.ignored=0 
    and a.fname like '%Rah%' 
group by f.uid, a.uid, b.uid, c.uid,
       d.uid, e.uid
having count(*) > 1
order by f.date DESC;

答案 2 :(得分:1)

这似乎是一个奇怪的问题,我刚刚尝试过以下内容。

select 1=10=10=1=1=1 from dual;

结果是 0

select 10=10=1=1=1=1 from dual;

结果是 1 ,也许on a.uid = b.uid =c.uid = d.uid=e.uid=f.uid此查询条件将第二个作为其计算结果。我不确定mysql是否会进行某种优化步骤​​以在排除时导致此问题。

我认为当mysql计算a.uid = b.uid时,如果a.uid和b.uid都是1,则出现值1意味着true,所以只有当c.uid为1时才计算继续关于d.uid,e.uid,f.uid。

无论如何,试试这个,可以帮到你;)

select f.uid, a.uid, b.uid, c.uid,
   d.uid, e.uid,
   f.date, a.email, a.fname, a.lname,
   a.mobile, a.pic, a.address
from users a, friends b ,exp c, skill d, personaldetails e, jobs f  
where f.job_id= 22 
and f.ignored=0 
and a.fname like '%Rah%'
and a.uid = b.uid
and b.uid = c.uid
and c.uid = e.uid
and e.uid = f.uid 
order by f.date desc

如果您已找到其他解释,请与我分享PLZ;)