我有一个看起来像这样的表:
+---------+----------+
| post_id | path |
+---------+----------+
| 1 | 1/ |
| 2 | 1/2/ |
| 3 | 1/2/3/ |
| 4 | 1/2/3/4/ |
| 5 | 1/2/5/ |
+---------+----------+
然后,我有一个查询,看起来像以下内容:
SELECT post_id AS column_1,
post_path
FROM posts
WHERE post_path REGEXP concat(?, '/[0-9]+/$') LIMIT 5
UNION
SELECT post_id AS column_2,
post_path
FROM posts
WHERE post_path REGEXP concat(?, '/', column_1, '/[0-9]+/$') LIMIT 5
在最后一行,您将看到我正在使用第一个查询column_1
来引用post_id
,但是,这是行不通的。
如何进行这项工作,以便可以使用post_id
第二个查询中column_1
当前位置的第一个查询中的?
谢谢。
用@variable_name:=
用你所选列的当前数据,这里是代码
SELECT @col:=post_id AS column_1,
post_path
FROM posts
WHERE post_path REGEXP concat(?, '/[0-9]+/$') LIMIT 5
UNION
SELECT post_id AS column_2,
post_path
FROM posts
WHERE post_path REGEXP concat(?, '/', @col, '/[0-9]+/$') LIMIT 5
@col
如您所见,@col
是您的变量,有关更多定义,@col
请访问此处的其他答案#1054-“字段列表”中的未知列
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句