4

我有这个名为“类别”的表,其中包含以下三个字段

cat     | order | id
--------|-------|----
News    |  3    | 23
Hi-Tech |  2    | 15
Biz     |  5    | 8
Health  |  1    | 3

另外,我还有另一个名为“链接”的表,如下所示

link    | order | cat-id
--------|-------|-------
link1   |  2    | 23
link2   |  8    | 15
link3   |  5    | 8
link4   |  6    | 15
link5   |  2    | 15
link6   |  4    | 23
link7   |  1    | 3
link8   |  1    | 8

我想要实现的是对类别进行排序并在每个类别下方对该类别cat-id / id的链接进行排序,如下所示:

健康
链接7

高科技
链接
5 链接4 链接
2

新闻
链接1
链接6

商务
链接
8 链接3

我成功地显示了分类的类别,但在它之后我失去了它。

我的目标是在页面上显示这个,所以我想我必须使用 PHP 和 mySQL。

4

10 回答 10

16

查询很简单:

SELECT c.cat, l.link, c.id
FROM links l INNER JOIN categories c
ON l.cat-id = c.id
ORDER BY c.`order`, l.`order`

它在这里起作用。

为了在页面上显示它,它看起来像这样:

mysql_connect(<host>, <username>, <password>);

$q = mysql_query("<query from above>");
$last_cat = 0;
while($row = mysql_fetch_assoc($q)) {
    if($row['id'] != $last_cat) {
        print '<h3>' . $row['cat'] . '</h3>';
        $last_cat = $row['id'];
    }
    print $row['link'] . '<br>';
}
于 2012-04-29T00:31:30.330 回答
5

我在脑海中写了这个,所以没有测试它,但它有点像这样:

$q = mysql_query("select * from categories c 
      left join links l 
      on l.cat-id=c.id 
      order by c.order,l.order");

$last_cat = -1;

while($row=mysql_fetch_assoc($q)){
    if($last_cat!=$row["cat"]){
        echo "<br><b>".$row["cat"]."</b><br>";
        $last_cat=$row["cat"];
    }
    echo $row["link"]."<br>";
}
于 2012-04-29T00:46:37.757 回答
2

This will print exactly as you have described in the example:

<?php
$categories = mysql_query("SELECT * FROM categories order by 'order'");
$links = mysql_query("SELECT * FROM links order by 'order'"); 
?>

<?php foreach ($categories as $key => $category): ?>
 <b><?php echo $category['cat'];?>"</b>
    <?php foreach ($links as $key => $link): ?>
      <?php if($category['id'] === $link['cat-id']): ?>
         <?php echo $link['link'];?>
      <?php endif;?>
    <?php endforeach;?>
   <br />
<?php endforeach;?>
于 2012-05-05T11:37:11.440 回答
2
<?php
// This query gets all the needed data with one shot. No need to do multiple
// queries. You can run this query in phpMyAdmin to see the result.
$query = mysql_query("
    SELECT links.link as link, categories.cat as cat FROM links
    LEFT JOIN categories ON categories.id = links.cat_id
    ORDER BY categories.order, links.order ASC");

// With this we are getting all links with correct order to an array.
$categories = array();
while ($row = mysql_fetch_array($query)) {
    $categories[$row['cat']][] = $row['link'];
}

// To see how we stored the links in the array uncomment the below line
// var_dump($categories);

// To print out the data:
foreach ($categories as $category => $links) {
    echo "<strong>".$category."</strong><br />\n";

    // implode is a built-in function that makes a string from an array.
    // You can put something between the array elements with the first parameter.
    // If you want to make some changes or print the links, use a foreach loop.
    echo implode("<br />\n", $links)."<br /><br />\n";
}
?>

This should write the links exactly what you want.

于 2012-05-05T13:40:26.230 回答
1
ORDER BY categories.order ASC, links.order ASC
于 2012-04-29T00:32:36.653 回答
1
select cat, link
from categories, links
where link.cat-id = categories.id
order by cat asc, link asc;
于 2012-04-29T00:36:09.647 回答
1

根据您的查询,如果我们需要考虑优化,我们将考虑一些情况

案例 1: 正如您所提到的,您将拥有 60(链接)* 5(类别),这些从 mysql 端本身很容易处理。我们可以很好地使用我们的 SQL 技术本身并从中获得优势。

您总是可以混合使用 php 处理 + SQL。PHP的处理速度比mysql快。所以,抓住它。

Your query can also be solved by SQl alone and the query mentioned by paolo can be used simply.

case 2: There are situation where either or both one of the table participating tables have too high number of records. when there is a join between them it results into high number of combinations i.e join size eg: links(100000) * categories(10) = 1 million In these cases, u can try to avoid joins.

You can take advantage of indexes. Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows.

There are techniques like flat files i.e schema free databases like MongoDB which dont use concept of joins but if you plan on it running it on a huge site (huge, meaning that you need many many servers to host it)

MySQl scales well and you have to think something out of it when you are in a huuuuuuuge environment.

Here,I have solved your query without using joins and any dependent query. I have just fetch results and just used array mapping techniques only which is faster.

 $query = mysql_query("SELECT * FROM category order by `order` asc");
 while($row = mysql_fetch_assoc($query) ) {
     $categories[$row['id']]['cat']  = $row['cat'];
     $categories[$row['id']]['order'] = $row['order'];

 } 

 $query = mysql_query("SELECT * FROM links ORDER BY `order` ASC ");
 $count =0; 
 while($row = mysql_fetch_assoc($query) ) {
     $links[$row['cat-id']][$count]['link']  = $row['link'];
     $links[$row['cat-id']][$count]['order']  = $row['order'];
     $links[$row['cat-id']][$count]['cat-id']  = $row['cat-id'];
     $count++; 
 } 

foreach ($categories as $id=$value) {
     foreach ($links[$id] as $link_info) {

         if ($last_cat != $link_info['cat-id'])
             print '<h3' . $categories[$link_info['cat-id']]['cat'] . '</h3';

         print $link_info['link'] . '<br';
         $last_cat = $link_info['cat-id'];
     } }
于 2012-05-02T21:12:01.870 回答
0

1st the query as a lot of people says its:

SELECT c.cat,c.order as catorder,c.id,l.* FROM categories as c 
INNER JOIN links as l ON (c.id = l.id-cat )
ORDER BY c.order, l.order 

so u have a result ordered like this:

cat     | catorder | id  |  link   | order | cat-id
--------|----------|-----|---------|-------|-------
Health  |  1       | 3   |  link7  |  1    |  3
Hi-Tech |  2       | 15  |  link5  |  2    |  15
Hi-Tech |  2       | 15  |  link4  |  6    |  15
Hi-Tech |  2       | 15  |  link2  |  8    |  15
News    |  3       | 23  |  link1  |  2    |  23
News    |  3       | 23  |  link6  |  4    |  23
Biz     |  5       | 8   |  link8  |  1    |  8
Biz     |  5       | 8   |  link3  |  1    |  8  

So .. you need to make a "cutting control"

$max = sizeof($arrayOfResults)
$i = 0;
while ($i < $max) {
    $current = $arrayOfResults[$i]["id"];
    print_r($arrayOfResults[$current]["cat"]);
    while ($i < $max && $arrayOfResults[$i]["id"] == $current) {
        print_r($arrayOfResults[$current]["link"]);
        $i++;
    }
}

If u copy and paste this code u going to have the output that u r searching.

If u need a explanation ask me.

于 2012-05-03T15:08:43.307 回答
-1

代码可能是这样的。

<?php
$result_catg = mysql_query("select * from categories order by order asc");
?>
<ul>
<?php
while($row_catg = mysql_fetch_array($result_catg))
{
$catg_id = $row_catg['id'];
$result_link = mysql_query("select * from links where cat_id = '$catg_id' order by order asc");
while($row_link = mysql_fetch_array($result_link))
{
?>
<li class="down"><?php echo $row_link['link']; ?></li>
<?php
}
}
?>
</ul>

只需在li down的 CSS 属性中使用“float:none”

希望这可以帮助。

于 2012-04-29T14:23:09.390 回答
-1

This will give you the required result.

$ds = mysql_query("select cat, id from categories order by  'order' asc");
while($rs = mysql_fetch_row($ds))
{
    echo"<b>".$rs[0]."<b><br/>";
    $dsLinks = mysql_query("select link from links where cat_id = '".$rs[1]."' order by 'order' asc");
    while($rsLink = mysql_fetch_row($ds))
    {
        echo"<b>".$rsLink [0]."<b><br/>";
    }
    echo"<br/>";
}
于 2012-05-03T05:57:26.377 回答