[英]Joining Tables in MySQL Query instead of multiple queries
我有這個問題
$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.*
FROM項目i內部聯接位置l on i.locationID = l.locationID內部聯接類別c on i.categoryID = c.categoryID WHERE itemStatus ='1'AND itemAdded>'$ timestamp'“)或die(mysql_error());
結果是
Array
([itemID] => 81300 [locationID] => 17 [categoryParentID] => 21 [categoryID] => 183 [itemName] => blah [itemDetails] => blah [locationName] =>很棒它按預期拉入位置。[locationLink] => blah [categoryName] =>輝煌它按預期拉入類別。[categoryLink] => blah)
[categoryName] => //these are missing for categoryParentID
[categoryLink] => //these are missing for categoryParentID
我認為應該類似於下面的查詢。 我沒看到你在哪里使用$ 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 ))
我不會在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'
我希望它對你有用。 干杯!
除非我遺漏了一些明顯的東西,否則我可能會建議這樣的事情作為第一個開始:
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'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.