如何按字母顺序排序此SELECT查询。下拉式菜单。订购

时间:2014-10-16 14:16:15

标签: sql

我有这个查询

  `$optnamear = tep_db_query("SELECT `products_options_values_name` from `products_options_values` WHERE `products_options_values_id`='$id[products_options_values_id]' ") ;`  

我希望它按字母顺序对下拉菜单中的products_options_values_name进行排序。我该怎么做?

我一直在尝试使用“order by”,但我对此毫无头绪,任何帮助表示赞赏

如果需要,这是所有代码吗?就像我说的,无能为力....

   $mainhtml = ""; //the var to hold all of the html

foreach ( $opts as $opt => $name) {

    unset($html);

    $html = "<tr><td width='125px'>
                $name 
            </td></tr>
            <tr><td>
            <SELECT name='$opt' onchange='document.m_srch.submit();'>
            <OPTION value='not'>---</OPTION>";//print the name of the box and start the drop down

    $sql = "SELECT `products_options_values_id` from `products_options_values_to_products_options` WHERE `products_options_id`='$opt'"; 
    $res = tep_db_query($sql);// get the values of all the options for that catagory
    while($id = tep_db_fetch_array($res)){

        $optnamear = tep_db_query("SELECT `products_options_values_name` from `products_options_values` WHERE `products_options_values_id`='$id[products_options_values_id]' ") ;

        $optname = tep_db_fetch_array($optnamear);

        //create the dropdown

        $html .= "<OPTION value='$id[products_options_values_id]' ";

        if($_GET[$opt] == $id['products_options_values_id']){
            $html .= "selected='selected'"; // if the product has already been selected keep it selected!
            } 

        $html .= ">$optname[products_options_values_name]</OPTION>";


        }

        $mainhtml .= $html."</SELECT></td></tr>";



    } 


echo "<tr><td>
<table border='0' width='100%' cellspacing='0' cellpadding='0'>
  <tr>
    <td height='14' class='infoBoxHeading'><img src='images/infobox/corner_left.gif' border='0' alt='' width='11' height='14'></td>
    <td  width='100%' height='14' class='infoBoxHeading'>$heading</td>
    </tr>
    </table>
    <table border='0' width='100%' cellspacing='0' cellpadding='1' class='infoBox'>
            <tr><td>
            <table class='infoBoxContents'>
            <FORM name='m_srch' action='advanced_search_result.php' method='get'>
            <INPUT type='hidden' value='1' name='m_op'> <INPUT type='hidden' value='1' name='keywords'> \n
            $mainhtml
            </table>
            </td></tr>
            </FORM>
        </table>
    </td></tr>";  

?> 

1 个答案:

答案 0 :(得分:0)

在查询中使用联接。

SELECT products_options_values_id, products_options_values_name 
FROM products_options_values_to_products_options
  INNER JOIN products_options_values
  ON products_options_values.products_options_values_id = products_options_values_to_products_options.products_options_values_id
WHERE products_options_values_to_products_options.products_options_id='$opt'
ORDER BY products_options_values.products_options_values_name

并对此进行迭代。

而不是:

SELECT products_options_values_id 
from products_options_values_to_products_options 
WHERE products_options_id='$opt'`

然后迭代

SELECT products_options_values_name 
from products_options_values 
WHERE products_options_values_id='$id[products_options_values_id]