2011-08-23 153 views
0

我在計算如何組合這兩個查詢時遇到了一些問題。如何將這2個SQL查詢合併爲一個

$get_id = $psk->goqueryvalue("SELECT `categoryid` FROM `products` WHERE `seo_title` = '{$url_title}'"); 

$get_category = $psk->goqueryvalue("SELECT `title` FROM `categories` WHERE `id` = '{$get_id}'"); 

($ psk-> goqueryvalue只返回一個值。)

感謝

+1

是有你的表'products'和'categories'之間的關係(外鍵參考)? – diEcho

回答

3
SELECT c.title # also c.id if you need it 
FROM categories c, products p 
WHERE c.id = p.categoryid 
AND p.seo_title = '{$url_title}' 
+0

擊敗了我;) – ChrisK

+0

非常感謝。我認爲c.id = p.categoryid有點困惑,因爲我早期嘗試了類似的查詢,但並沒有想到它會起作用。 –

1

試着這麼做

SELECT `categoryid`, `title` FROM `products` 
    LEFT JOIN `categories` ON `products`.`categoryid` = `categories`.`id` 
    WHERE `products`.`seo_title` = '{$url_title}' 
1

試着這麼做

$get_category = $psk->goqueryvalue("SELECT `title` FROM `categories` WHERE `id` = (SELECT `categoryid` FROM `products` WHERE `seo_title` = '{$url_title}'"); 

OR

$get_category = $psk->goqueryvalue("SELECT `title` FROM `categories` INNER JOIN `products` ON 
`categories.id` = `products.categoryid` WHERE `seo_title` = '{$url_title}'"); 
相關問題