我有两个表类别和子类别。在类别表中,我有一列cat_url。在子类别表中,我有一列sub_cat_url

我要获取的是按字母顺序排序的类别表和子类别表的所有URL,并显示在菜单下拉菜单中

我在做什么是:

$A = "SELECT cat_url FROM category
WHERE cat_url like 'a%'

UNION ALL

SELECT sub_cat_url FROM subcategory
WHERE sub_cat_url like 'a%'
ORDER BY cat_url, sub_cat_url";

$rA = mysqli_query($dbc,$A) or die(mysqli_error($dbc));

while ($rsA = mysqli_fetch_assoc($rA)){

    <a href=".$rsA['cat_url'].'.php'.">

    echo $rsA['cat_url'];

    // i do not want to use $rsA['cat_url']; What i need is to combine both cat_url and sub_cat_url in one result set and the echo each url

}

最佳答案

您可以尝试像这样(未测试)加入子类别表:

SELECT cat.cat_url as cat_url, subcat.sub_cat_url as sub_cat_url FROM category as cat
left join subcategory as subcat on subcat.sub_cat_url like 'a%'
WHERE cat.cat_url like 'a%'
ORDER BY cat.cat_url, subcat.sub_cat_url

10-02 19:14