如何将这两个SQL查询合并为一个

时间:2011-08-23 09:25:11

标签: php mysql 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只返回一个值。)

由于

3 个答案:

答案 0 :(得分: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}'

答案 1 :(得分:1)

尝试类似

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

答案 2 :(得分: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}'");