来自另一个表的MYSQL子查询

时间:2014-11-10 10:25:33

标签: mysql

我有这个MYSQL声明:

SELECT r.id AS id, r.validSince AS validSince, r.validUntil AS validUntil, o.updatedTime as updatedTime, o.Owner AS Owner, fd.sid, fd.baseData
FROM #__sobipro_relations AS r 
INNER JOIN #__sobipro_field_data AS fd ON fd.fid="71" 
INNER JOIN #__sobipro_object AS o ON r.id=o.id 
WHERE r.oType="entry" 
AND o.approved=1 
AND o.state=1 
AND ((r.validSince<="2014-11-10T02:57:51-07:00") AND(r.validUntil>="2014-11-10T02:57:51-07:00" OR r.validUntil="0000-00-00T00:00:00")) 
GROUP BY r.id

我现在想让查询只返回符合此条件的结果:#__ sobipro_field_data.baseData = 1002(或其他一些数字)

我试图像这样添加 AND fd.baseData = 1002

SELECT r.id AS id, r.validSince AS validSince, r.validUntil AS validUntil, o.updatedTime as updatedTime, o.Owner AS Owner, fd.sid, fd.baseData
FROM #__sobipro_relations AS r 
INNER JOIN #__sobipro_field_data AS fd ON fd.fid="71" 
INNER JOIN #__sobipro_object AS o ON r.id=o.id 
WHERE r.oType="entry" 
AND o.approved=1 
AND o.state=1 
AND fd.baseData=1002 
AND ((r.validSince<="2014-11-10T02:57:51-07:00") AND(r.validUntil>="2014-11-10T02:57:51-07:00" OR r.validUntil="0000-00-00T00:00:00")) 
GROUP BY r.id

但它不起作用,它会返回相同的结果

任何想法如何实现这一目标? 感谢

好吧,所以我可能以错误的方式接近这个,我需要某种子查询来解决它。这工作

SELECT r.id AS id, r.validSince AS validSince, r.validUntil AS validUntil, o.updatedTime as updatedTime, o.Owner AS Owner
FROM j7v3x_sobipro_relations AS r 
INNER JOIN #__sobipro_object AS o ON r.id=o.id 
WHERE r.oType="entry" 
AND r.id IN (SELECT fd.sid FROM #__sobipro_field_data as fd WHERE fd.sid = r.id AND fd.baseData = 1002 )
AND o.approved=1 
AND o.state=1 
AND ((r.validSince<="2014-11-10T02:57:51-07:00") AND(r.validUntil>="2014-11-10T02:57:51-07:00" OR r.validUntil="0000-00-00T00:00:00")) 
GROUP BY r.id

感谢您的帮助

0 个答案:

没有答案