简体   繁体   English

MySQL-在子查询的别名上使用REGEXP

[英]MySQL - using REGEXP on an alias of a subquery

How can I use REGEXP on an alias of a subquery? 如何在子查询的别名上使用REGEXP?

SELECT 
    colA,
    colB,
    (SELECT colA FROM t2 WHERE t1.colID=t2.colID) as colC
FROM t1
WHERE colC REGEXP '$string'
ORDER BY IF($col = '' OR $col IS NULL,1,0),$col";




It works if I make another select of the complete query, but I see that this costs some time 如果我再次选择完整查询,它会起作用,但是我发现这会花费一些时间

SELECT * FROM (
    SELECT 
        colA,
        colB,
        (SELECT colA FROM t2 WHERE t1.colID=t2.colID) as colC
    FROM t1
) as temp
WHERE colC REGEXP '$string'
ORDER BY IF($col = '' OR $col IS NULL,1,0),$col";




REGEXP works if I use a join and referencing the table field directly, but the order doesn't work with an alias either 如果我使用联接并直接引用表字段,则REGEXP可以使用,但是该命令也不能使用别名

SELECT 
    t1.colA AS colA,
    t1.colB AS colB,
    t2.colA AS colC
FROM t1
LEFT JOIN t2 ON t1.colID=t2.colID
WHERE t2.colA REGEXP '$string'
ORDER BY IF($col = '' OR $col IS NULL,1,0),$col";




So is a select on the complete query the only way to do what I need? 那么,对完整查询进行选择是执行我需要的唯一方法吗?

You can use a HAVING clause (this is not standard SQL - MySQL feature): 您可以使用HAVING子句(这不是标准的SQL-MySQL功能):

SELECT 
    colA,
    colB,
    (SELECT colA FROM t2 WHERE t1.colID=t2.colID) as colC
FROM t1
HAVING colC REGEXP '$string'
ORDER BY IF($col = '' OR $col IS NULL,1,0),$col";

There are performance implications - your join is a better query. 这对性能有影响-您的联接是更好的查询。

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

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