如何通过其他sql表对sql进行排序?

时间:2011-05-18 09:13:24

标签: php mysql sql wordpress

我有两张桌子。

wp_term_relationships
     term_taxonomy_id
     ...

wp_posts
     ID
     ...
     menu_order

我正在做的是从wp_term_relationships

获取所有值
$results = $wpdb->get_results("SELECT `object_id` 
                               FROM `wp_term_relationships` 
                               WHERE `term_taxonomy_id`='".$cat_id."'");

然后考虑我需要的每个值

foreach($results as $result){

   $postarrayStringINT[] = $result->object_id;

    $results2 = $wpdb->get_results("SELECT * FROM `wp_posts` 
                                    WHERE `ID`='".$result->object_id."' ");

但我需要它们的顺序正确,那就是wp_posts中的menu_order表,该怎么做?

我尝试过类似的东西:

"SELECT `object_id` FROM `wp_term_relationships` 
 WHERE `term_taxonomy_id`='".$cat_id."' JOIN (SELECT * FROM 'wp_posts') 
 ORDER BY 'menu_order'"

怎么做?如何从表中获取一个值并按其他表值排序/ 排序?

1 个答案:

答案 0 :(得分:0)

你应该尝试类似的东西:

SELECT field1, field2, field3, etc... 
FROM wp_posts
INNER JOIN wp_term_relationships on wp_posts.id = wp_term.object_id
WHERE term_taxonomy_id=".$cat_id."
ORDER BY menu_order