我有下一个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运行不正常。对于行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
中的值具有相同的形式。
我所想做的就是显示一个部分树和每个部分的一些项目。
+
不能用于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'
。