简体   繁体   English

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

[英]Php, MySql join tables, limit records per category

I have two tables: 我有两个表:

Items 物品

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

Brands 品牌品牌

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

And following query: 和以下查询:

  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;

The above produces working array: 上面产生了工作数组:

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

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

        )

        ...
)

Than I use custom function to loop through array and end up with 比我使用自定义函数遍历数组并最终得到

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
                )
            ...
        )    
)

Now I can use foreach loop and limit each Brand to show x number of items, but the idea is to do it within database, so instead of pulling all records, I would like to be able to limit to 5 items per each brand. 现在,我可以使用foreach循环并限制每个品牌显示x个项目,但是这个想法是在数据库中完成,因此,除了提取所有记录,我希望每个品牌可以限制5个项目。

Note: I did not list the rest of the table fields, such as created, which is used to sort records. 注意:我未列出用于对记录进行排序的其余表字段,例如创建的。

  1. get brands: 获得品牌:

$q = 'SELECT Model_ID AS id FROM brands ORDER BY title ASC'; $ q ='从品牌ORDER BY标题ASC中选择Model_ID AS ID';
$r = mysql_query($q); $ r = mysql_query($ q);
$model_ids = array(); $ model_ids = array();
while($brand = mysql_fetch_assoc($r)) { $model_ids[] = $brand['id']; while($ brand = mysql_fetch_assoc($ r)){$ model_ids [] = $ brand ['id']; } }

  1. then make generate select query like this using Model_ID-s collected from 1st step or somewhere else (ex: from search form): 然后使用从第一步或其他位置(例如:从搜索表单中)收集到的Model_ID-进行如下生成选择查询:

 $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); } } 

as result we'll get records of resulting query: 结果,我们将获得结果查询的记录:

(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=1 AND i.status = 1 ORDER BY i.created DESC LIMIT 5) (从项目中选择“ b.name”,“ i.title”作为“ i i.INNER JOIN”品牌作为“ b ON”。b.Model_ID= i.Model_ID,b.Model_ID = 1并且i.status = 1由i。创建的DESC LIMIT 5)

UNION ALL 全联盟

(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=2 AND i.status = 1 ORDER BY i.created DESC LIMIT 5) (从项目中选择b.name,i.title作为AS i INNER JOIN品牌作为b.b.Model_ID = i.Model_ID WHERE b.Model_ID = 2 AND i.status = 1按i。创建的DESC LIMIT 5排序)

UNION ALL 全联盟

(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=3 AND i.status = 1 ORDER BY i.created DESC LIMIT 5) (从项目中选择“ b.name”,“ i.title”作为“ i i.INNER JOIN”品牌作为“ b ON”。b.Model_ID= i.Model_ID WHERE b.Model_ID = 3并且i.status = 1由i。创建的DESC LIMIT 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM