2013-02-19 51 views
0

连接表我有这个疑问在MySQL查询,而不是多个查询的

$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,类别名称的数组,categoryLink

可能有人请帮我优化这个查询,并可能加入他们的行列,以节省做这么多的疑问。

在此先感谢。

林现在使用此查询

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

FROM 物品i 内上i.locationID = l.locationID 内部联接类别C于i.categoryID = c.categoryID WHERE itemStatus加入位置升= '1' AND itemAdded> '$时间戳'“)或死亡(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 
+0

你可以发布你的表结构吗?看起来你只需要使用JOINS。 – sgeddes 2013-02-19 16:00:52

回答

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' 
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); 

请让我知道如果返回预期的结果集。希望这会使用categoryParentID帮助

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' 

更新查询 - 我不是说是有效的,但你可以测试,并根据需要优化。

一种选择是更新上面的查询 - 不知道,将工作 - 使用和大型结果集或效率不高:

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 - 将不得不强制转换为字符串/字符。有几个选项可以让你编写这个查询 - 如果你发布一个结构表和一些虚拟数据,我肯定有人会创建一个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)) 
+0

我认为问题在于查询类别为父类和类别的同一张表。 – 2013-02-19 16:40:56

+0

,你想要两个结果集?即gell所有项为categories.categoryID = items.categoryID和categories.categoryID = $ categoryParentID? – 2013-02-19 16:49:29

+0

是的,如果可以的话,谢谢你的帮助。 – 2013-02-19 16:52:04

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' 

我希望对您有用。 干杯!

+0

感谢您的答复,大加赞赏,但是,我现在遇到的问题是categoryParentID是overwritter通过的categoryID结果阵列中,因为它们都返回相同的领域。 – 2013-02-19 16:21:41

+0

阵列 ( [ITEMID] => 81300 [locationID] => 17 [categoryParentID] => 21 [的categoryID] => 183 [ITEMNAME] =>嗒嗒 [itemDetails] =>嗒嗒 [LOCATIONNAME] =>辉煌它拉在预期的位置。 [locationLink] =>嗒嗒 [类别名称] =>辉煌它拉在类别如预期。 [categoryLink] =>嗒嗒 ) [类别名称] =>//这些缺少categoryParentID [categoryLink] => //这些对于categoryParentID缺少 – 2013-02-19 16:27:27