Php,MySql连接表,限制每个类别的记录

时间:2015-08-25 23:34:18

标签: php mysql

我有两张桌子:

产品

ID      Name                Model_ID
---------------------------------
1       2010 Audi L1        1
2       2014 BMW X2         2
3       2015 Acura L3       3
4       2016 BMW X5         2
5       2012 BMW X3         2
6       2013 BMW X4         2
7       2015 Acura L1       3
8       2011 Acura L2       3
9       2011 Audi L5        1
10      2012 Audi L6        1

品牌

Model_ID        Title
---------------------
1               Audi
2               BMW
3               Acura

以下查询:

  SELECT 
    b.name,
    i.title,
  FROM
    items AS i 
    INNER JOIN brands AS b 
      ON b.Model_ID = i.Model_ID 
  WHERE i.status = 1
  ORDER BY i.created DESC;

以上产生工作数组:

Array
(
    [0] => stdClass Object
        (
            [name] => 2010 Audi L1
            [title] => Audi
        )

    [1] => stdClass Object
        (
            [name] => 2014 MBW X5
            [title] => BMW

        )

        ...
)

我使用自定义函数循环遍历数组并以

结束
Array
(
    [Acura] => Array
        (
            [0] => stdClass Object
                (
                    [name] => 2015 Acura L1
                    [title] => Acura
                )

            ...
        )

    [BWM] => Array
        (
            [0] => stdClass Object
                (
                    [name] => 2016 BMW X5
                    [title] => BWM
                )

            ...
        )
    [Audi] => Array
        (
            [0] => stdClass Object
                (
                    [name] => 2010 Audi L1
                    [title] => Audi
                )
            ...
        )    
)

现在我可以使用foreach循环并限制每个品牌显示x个项目,但想法是在数据库中进行,所以不是拉所有记录,我希望每个限制为5个项目品牌。

注意:我没有列出其余的表字段,例如created,用于对记录进行排序。

1 个答案:

答案 0 :(得分:0)

  1. 获得品牌:
  2.   

    $ q =' SELECT Model_ID AS id FROM brands ORDER BY title ASC&#39 ;;
       $ r = mysql_query($ q);
       $ model_ids = array();
       while($ brand = mysql_fetch_assoc($ r)){$ model_ids [] = $ brand [' id']; }

    1. 然后使用从第1步或其他地方收集的Model_ID-s生成这样的选择查询(例如:从搜索表单):

    2. $q = array(); 
      foreach($mode_ids AS $model_id) {
          $q[] = '(SELECT b.name, i.title FROM items AS i INNER JOIN brands AS b ON b.Model_ID = i.Model_ID WHERE b.Model_ID = '.(int)$model_id.' AND i.status = 1 ORDER BY i.created DESC LIMIT 5)';
      }
      
      if(!empty($q)) {
          $q = implode(' UNION ALL ', $q);
          $r = mysql_query($q);
          while($record = mysql_fetch_object($r)) {
              var_dump($record);
          }
      }
      

      结果我们将得到结果查询的记录:

        

      (SELECT       b.name,       i.title FROM       项目AS i       INNER JOIN品牌AS b         ON b.Model_ID = i.Model_ID WHERE b.Model_ID = 1 AND i.status = 1 ORDER BY i.created DESC LIMIT 5)

           

      UNION ALL

           

      (SELECT       b.name,       i.title FROM       项目AS i       INNER JOIN品牌AS b         ON b.Model_ID = i.Model_ID WHERE b.Model_ID = 2 AND i.status = 1 ORDER BY i.created DESC LIMIT 5)

           

      UNION ALL

           

      (SELECT       b.name,       i.title FROM       项目AS i       INNER JOIN品牌AS b         ON b.Model_ID = i.Model_ID WHERE b.Model_ID = 3 AND i.status = 1 ORDER BY i.created DESC LIMIT 5)