此查询
SELECT
itemID,
locationParentID,
locationID,
categoryParentID,
categoryID,
itemTitle,
itemDetails,
itemAttributes,
itemPictures,
itemFeatured,
itemSpotlight,
itemAdded
FROM items
WHERE items.siteID IN('".$cfg['site']['siteShares']."')
AND EXISTS(SELECT 1 FROM sites_locations sl WHERE items.locationID = sl.locationID AND siteID = '".$cfg['site']['siteID']."' LIMIT 1)
AND EXISTS(SELECT 1 FROM sites_categories sc WHERE items.categoryID = sc.categoryID AND siteID = '".$cfg['site']['siteID']."' LIMIT 1)
AND itemStatus = '1'
AND itemAdded > '".$cfg['timestamp']."'有效,但最多需要6秒
我可以使用JOIN,因为我猜它会更快
下面是我尝试使用JOIN的方法,但它返回所有结果,而不仅仅是在sites_location表中有位置的项。
SELECT
itemID,
locationParentID,
categoryParentID,
categoryID,
itemTitle,
itemDetails,
itemAttributes,
itemPictures,
itemFeatured,
itemSpotlight,
itemAdded
FROM items LEFT JOIN sites_locations
ON items.locationID = sites_locations.locationID
AND sites_locations.siteID = items.siteID
WHERE items.siteID IN('1,2') AND itemStatus = '1'
AND itemAdded > '1356048000' ORDER BY itemID DESC LIMIT 15发布于 2013-02-22 10:33:52
不确定是否在第二个查询中对表sites_categories进行联接。
发布于 2013-02-22 13:47:02
当两个表中至少有一个匹配项时,INNER JOIN关键字将返回行。
LEFT JOIN关键字返回左表(table_name1)中的所有行,即使右表(table_name2)中没有匹配项也是如此。
这就是为什么您无法获取值的原因
Check this link
https://stackoverflow.com/questions/15021956
复制