2

我有一个下一个 mysql 查询:

SELECT 
    shop_sections.title, 
    shop_sections.id, 
    shop_sections.parent_id, 
    count(shop_items.id) AS items
FROM 
    shop_sections 
LEFT JOIN 
    shop_items 
ON 
    shop_items.section_id 
        REGEXP "^" + shop_sections.parent_id + "([[.full-stop.]].*|$)" 
GROUP BY 
    shop_sections.id

REGEXP 执行不正确。对于 row shop_items.section_id= 1,它获取所有字段shop_items.section_id以“1”开头的行(即 13.14 或 13.15,但它必须获取 1 或 1.*(1.1、1.2、1.2.3 等))。

我注意到如果改变

REGEXP "^" + shop_sections.parent_id + "([[.full-stop.]].*|$)"

REGEXP "^1([[.full-stop.]].*|$)"

比它适用于 1 (如果手动将其插入查询也适用于任何值)。但我想使用一个查询来获取所有项目。

可能是什么问题?

UPD。

shop_items.section_id包含下一种形式的值:parent_section.child_section_1.child_section_2.child_section_3 等。我需要在项目页面上进行分页。

中的值shop_sections.parent_id具有相同的形式。

我想要做的就是显示一个部分的树和每个部分的一些项目。

4

1 回答 1

2

+不能用于 MySQL 中的连接。你的测试应该是:

ON 
  shop_items.section_id 
    REGEXP CONCAT('^', shop_sections.parent_id, '([[.full-stop.]].*|$)')

您得到的奇怪结果是由于断开的连接执行了加法。假设shop_sections.parent_id为 1,则:

'^' + shop_sections.parent_id + '([[.full-stop.]].*|$)'

... 确实被评估0 + 1 + 0(转换为整数的字符串被评估为 0,除非它们以数字序列开头),用作正则表达式等同于无用的'1'.

于 2012-08-24T10:11:24.053 回答