在MySQL Query中加入表而不是多个查询

时间:2013-02-19 15:57:15

标签: mysql join

我有这个查询

$query = "SELECT * FROM items WHERE itemStatus = '1' AND itemAdded > '$timestamp'";

一旦这个查询返回结果,我就会遍历结果

结果数组是itemID,itemLocationID,categoryParentID,categoryID,itemName,itemDetails

在循环过程中,我通过调用同一个类中的函数来运行其他三个查询

$locationName = $this->getLocationByName($locationID);
$categoryParentName = $this->getCategoryByName($categoryParentID);
$categoryName = $this->getCategoryByName($categoryID);

函数getLocationByName执行此查询;

$q = mysql_query("SELECT * FROM locations WHERE locationID = '$locationID'");

这将返回locationID,locationName,locationLink

的数组

函数getCategoryByName执行此查询;

$q = mysql_query("SELECT * FROM categories WHERE categoryID = '$categoryID'");

返回categoryID,categoryName,categoryLink

的数组

有人可以帮我优化这个查询,也许可以加入他们来保存这么多查询。

提前感谢。

我现在正在使用此查询

$q = mysql_query("SELECT 
i.itemID, 
i.locationID,
i.categoryParentID,
i.categoryID, 
i.itemName,
i.itemDetails,
l.*,
c.*        

这     物品我     i.locationID = l.locationID上的内连接位置l     i.categoryID = c.categoryID上的内部联接类别c 哪里     itemStatus ='1'     AND itemAdded> '$ timestamp'“)或死(mysql_error());

,结果是

Array

(     [itemID] => 81300     [locationID] => 17     [categoryParentID] => 21     [categoryID] => 183     [itemName] =>胡说     [itemDetails] =>胡说     [locationName] =>辉煌它按预期拉到了位置。     [locationLink] =>胡说     [categoryName] =>辉煌它按预期进入类别。     [categoryLink] =>胡说 )

[categoryName] => //these are missing for categoryParentID
[categoryLink] => //these are missing for categoryParentID

3 个答案:

答案 0 :(得分:1)

我认为应该是类似下面的查询。我没看到你在哪里使用$ categoryParentName。

使用您的查询和数据:

SELECT * FROM items WHERE itemStatus = '1' AND itemAdded > '$timestamp'
SELECT * FROM locations WHERE locationID = '$locationID'
SELECT * FROM categories WHERE categoryID = '$categoryID'

$locationName = $this->getLocationByName($locationID);
$categoryParentName = $this->getCategoryByName($categoryParentID);
$categoryName = $this->getCategoryByName($categoryID);

如果这返回了预期的结果集,请告诉我。希望这有帮助

SELECT 
    it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
    l.locationID, l.locationName, l.locationLink, 
    c.categoryID, c.categoryName, c.categoryLink
FROM items it 
LEFT JOIN locations l ON l.locationID = it.itemLocationID 
LEFT JOIN categories c ON c.categoryID = it.categoryID 
WHERE 
    it.itemStatus = '1' 
    AND it.itemAdded > '$timestamp'

使用categoryParentID更新查询 - 我不是说效率很高,但您可以根据需要进行测试和优化。

一个选项是更新上面的查询 - 不确定是否有效 - 对于使用OR的大型结果集效率不高:

LEFT JOIN categories c ON (c.categoryID = it.categoryID OR c.categoryID = it.categoryParentID)

我看到的另一个选项是获得2个结果集(见下文) - 一个用于CategId = categId,第二个用于categId = categParentId,并将结果集合并在一个大结果集中。

SELECT 
    t.itemID, t.itemLocationID, t.categoryParentID, t.categoryID, t.itemName, t.itemDetails,
    l.locationID, l.locationName, l.locationLink, 
    t.categoryID, t.categoryName, t.categoryLink 
FROM 
(
SELECT 
    it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
    c.categoryID, c.categoryName, c.categoryLink
FROM items it 
INNER JOIN categories c ON c.categoryID = it.categoryID 
WHERE 
    it.itemStatus = '1' 
    AND it.itemAdded > '$timestamp'

UNION -- [ALL]

SELECT 
    it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
    c.categoryID, c.categoryName, c.categoryLink
FROM items it 
INNER JOIN categories c ON c.categoryID = it.categoryParentID 
WHERE 
    it.itemStatus = '1' AND 
    it.itemAdded > '$timestamp'
) AS t 
LEFT JOIN locations l ON l.locationID = t.itemLocationID 

其他想法 - 未经测试并假设id为int - 将必须转换为字符串/ char。如何编写此查询有几个选项 - 如果您发布结构表和一些虚拟数据,我相信有人会创建一个demo / sqlfiddle。

SELECT 
    it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
    l.locationID, l.locationName, l.locationLink, 
    c.categoryID, c.categoryName, c.categoryLink
FROM items it 
LEFT JOIN locations l ON l.locationID = it.itemLocationID 
WHERE 
    it.itemStatus = '1' 
    AND it.itemAdded > '$timestamp' 
    AND c.category ID IN ( SELECT GROUP_CONCAT(categs) FROM (SELECT CONCAT(categoryID, ",", categoryParentID) AS categs FROM items ))

答案 1 :(得分:1)

我不会在select语句中使用* 带有连接的查询可以是

SELECT 
    i.itemID, 
    i.itemLocationID,
    i.categoryParentID,
    i.categoryID, 
    i.itemName,
    i.itemDetails,
    l.*,
    c.*        
FROM 
    items i
    inner join locations l on i.itemLocationID = l.locationID
    inner join categories c on i.categoryID = c.categoryID
WHERE
    itemStatus = '1'
    AND itemAdded > '$timestamp'

我希望它对你有用。 干杯!

答案 2 :(得分:0)

除非我遗漏了一些明显的东西,否则我可能会建议这样的事情作为第一个开始:

select *
  from items i
  join locations l 
    on i.location_id=l.location_id
  join categories c
    on i.category_id=c.category_id
 where item_status='1'
   and itemAdded > '$timestamp'