SELECT
j.* ,
month(j.create_date) as month
FROM tbl_jobs AS j
LEFT JOIN tbl_well_data AS wd
on j.id = wd.job_id
LEFT JOIN tbl_well_depth AS wdp
on j.id = wdp.job_id
LEFT JOIN tbl_well_formation AS wf
on j.id = wf.job_id
LEFT JOIN tbl_well_tubing_size AS wts
on j.id = wts.job_id
LEFT JOIN tbl_pumping AS p
on j.id = p.job_id
LEFT JOIN tbl_pumping_puid AS pf
on j.id = pf.job_id
WHERE j.create_date >= '2013-04-01'
AND j.create_date <= '2013-04-25'
AND ( pf.puid_id = 109 AND pf.volume BETWEEN 1 AND 2000 AND pf.corrosive = 1 AND pf.bh = 0 AND pf.ct = 1 )
AND ( pf.puid_id = 112 AND pf.volume BETWEEN 1 AND 2000 AND pf.corrosive = 1 AND pf.bh = 0 AND pf.ct = 1 )
AND FIND_IN_SET('1',j.services)
使用多个AND
结果时查询的问题在我使用此结果时显示为空但是我想要pf.puid_id = 112 AND pf.puid_id = 109
,并且这些puid_id会在我的数据库表中显示,那么为什么会出现这个问题呢?
当我使用OR时,结果仅在pf.puid_id = 112
或pf.puid_id = 109
上显示
答案 0 :(得分:0)
WHERE j.create_date >= '2013-04-01'
AND j.create_date <= '2013-04-25'
AND (( pf.puid_id = 109 OR pf.puid_id = 112)
AND pf.volume BETWEEN 1 AND 2000
AND pf.corrosive = 1 AND pf.bh = 0 AND pf.ct = 1 )
AND FIND_IN_SET('1',j.services)
是。请使用OR。
答案 1 :(得分:0)
当您对两个puid_id使用AND时,您希望同时满足两个条件的数学逻辑状态。这意味着您的查询将仅匹配同时遵循这两个条件的行。
所以你应该问自己:一件事怎么能同时等同于两件不同的事情?这就是你想用AND做的事情。
使用OR将给出与至少一个给定条件匹配的行(在这种情况下,两个条件都不可能满足)。
在继续之前,请考虑阅读更多基本概念。它将提高您的软件质量。
希望我能帮到你。希望我帮助过。
答案 2 :(得分:0)
问题在于:
AND ( pf.puid_id = 109 AND pf.volume BETWEEN 1 AND 2000 AND
pf.corrosive = 1 AND pf.bh = 0 AND pf.ct = 1 )
AND ( pf.puid_id = 112 AND pf.volume BETWEEN 1 AND 2000 AND
pf.corrosive = 1 AND pf.bh = 0 AND pf.ct = 1 )
您说您希望pf.puid_id
同时等于两者 109和112。我相信你的确意味着它应该 两者中的一个,而不是同时具有两个值。
将这两行替换为:
AND ( pf.puid_id IN (109,112) AND pf.volume BETWEEN 1 AND 2000 AND
pf.corrosive = 1 AND pf.bh = 0 AND pf.ct = 1 )
当您尝试测试某个值是否等于所需值之一时,IN关键字是您的朋友。它使查询更短,更易读。
一个例子 - 这个条件:
WHERE id = 5 OR id = 8 OR id = 13 OR id = 21 OR id = 34
与:
相同WHERE id IN (5,8,13,21,34)
另一个建议。替换:
j.create_date >= '2013-04-01'
AND j.create_date <= '2013-04-25'
使用:
j.create_date BETWEEN '2013-04-01' AND '2013-04-25'
两者都相同且同样正确,但第二个更短且更易读。