Forum Moderators: open

Message Too Old, No Replies

MySQL alias issue

AS not working with REGEXP

         

rob7591

4:30 pm on May 26, 2008 (gmt 0)

10+ Year Member



Hi,

I have this query for a search


$query = "SELECT
id,
title,
5 * (title REGEXP '$words') AS r1,
description REGEXP '$words' AS r2,
10 * (title REGEXP '$q') AS r3

FROM items
WHERE
(title REGEXP '$words') OR
(description REGEXP '$words') OR
(title REGEXP '$q')
ORDER BY
(r1 + r2 + r3) DESC";

I get the error unknown column r1, but I already used it as an alias.

I tried title as t1 .. ORDER BY t1 and that worked fine, so it's just an issue with the REGEXP I think.

Anyone know what's wrong?

rob7591

8:08 pm on May 26, 2008 (gmt 0)

10+ Year Member



I have it working now just by doing the same REGEXPs in the order:
              $query = "SELECT
id,
title,
5 * (title REGEXP '$words') AS r1,
description REGEXP '$words' AS r2,
10 * (title REGEXP '$q') AS r3

FROM items
WHERE
(title REGEXP '$words') OR
(description REGEXP '$words') OR
(title REGEXP '$q')
ORDER BY
(5 * (title REGEXP '$words')) + (description REGEXP '$words') + (10 * (title REGEXP '$q')) DESC, title";

But I'd like to know why it doesn't know why it doesn't see those r1 r2 r3, and also will it slow it down doing all these regexps?