Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 正在获取N条记录(不包括合并的记录)?_Mysql_Join - Fatal编程技术网

Mysql 正在获取N条记录(不包括合并的记录)?

Mysql 正在获取N条记录(不包括合并的记录)?,mysql,join,Mysql,Join,为了简洁起见,我省略了三个表的详细信息: create table products ( id, name ) create table tags ( id, name ) create table product_tags ( product_id, tag_id ) 这些表的填充方式如下: -------- products -------- +----+------+ | id | name | +----+------+ | 1

为了简洁起见,我省略了三个表的详细信息:

create table products (
    id,
    name 
)

create table tags (
    id,
    name
)

create table product_tags (
    product_id,
    tag_id
)
这些表的填充方式如下:

--------
products
--------
+----+------+
| id | name |
+----+------+
| 1  | Rice |
| 2  | Bean |
| 3  | Milk |
+----+------+

----
tags
----
+----+-------+
| id | name  |
+----+-------+
| 1  | Eat   |
| 2  | Drink |
| 3  | Seeds |
| 4  | Cow   |
+----+-------+
获取产品时,我希望输出的格式为:

[{
    id: 1,
    name: 'Rice',
    tags: [
        {
            id: 1,
            name: 'Eat'
        },
        {
            id: 3,
            name: 'Seeds'
        },
    ]
},
{
    id: 2,
    name: 'Bean',
    tags: [
        {
            id: 1,
            name: 'Eat'
        },
        {
            id: 3,
            name: 'Seeds'
        },
    ]
},
{
    id: 3,
    name: 'Milk',
    tags: [
        {
            id: 2,
            name: 'Drink'
        },
        {
            id: 4,
            name: 'Cow'
        },
    ]
}]
为了实现这一点,我正在做的是:

select 
  products.*,
  tags.id as tag_id, tags.name as tag_name
from products
left join product_tags map on map.product_id = products.id
left join tags on map.tag_id = tags.id
其输出为:

[{
    id: 1,
    name: 'Rice',
    tag_id: 1,
    tag_name: 'Eat',
},{
    id: 1,
    name: 'Rice',
    tag_id: 3,
    tag_name: 'Seeds',
},{
    id: 2,
    name: 'Bean',
    tag_id: 1,
    tag_name: 'Eat',
},{
    id: 2,
    name: 'Bean',
    tag_id: 3,
    tag_name: 'Seeds',
},{
    id: 3,
    name: 'Milk',
    tag_id: 2,
    tag_name: 'Drink',
},{
    id: 3,
    name: 'Milk',
    tag_id: 4,
    tag_name: 'Cow',
}]
我手工解析它,并将每个产品实例与它关联的零个或多个标记对象数组聚合

问题 执行上述选择时,输出为6行。然而,只有3种产品。是否可以使用仅适用于产品的限制条款

例如,如果Product.id=>1有10个标记与之关联,则执行限制5只会选择其中的5个标记。我想做的是选择5个产品以及与之相关的所有标签

我能想到的实现这一点的唯一方法是只选择产品,然后只使用上一个查询中的产品ID进行无限制选择

奖金问题
是否有一种更有效的方法来进行此连接,以便按上述方式聚合输出?

使用子查询选择5种产品,然后使用标记进行连接:

SELECT p.name as product, t.name AS tag
FROM (
    SELECT id, name
    FROM products
    ORDER by name
    LIMIT 5) AS p
JOIN product_tags AS pt ON pt.product_id = p.id
JOIN tags AS t ON pt.tag_id = t.id

SQL中唯一的结果是二维表。如果要将结果重新组织为更复杂的数据,必须使用外部编程语言(如PHP或Python)进行。有关如何使用Javascript(针对PHP)进行此操作,请参阅。