LIMIT (Subquery)

Hello,
i didn't find it in the help, i think it is not possible, but i try to
question anyway:
Is the MySQL command "LIMIT" possible with a subquery in MySQL 4.0.27?

e.g.: SELECT * FROM tab1 LIMIT (SELECT COUNT(*) from tab2)

this doesn't work.
MySQL 5.1 writes:
User variables may be used in contexts where expressions are allowed.
This does not currently include contexts that explicitly require a
literal value, such as in the LIMIT clause of a SELECT statement, or
the IGNORE N LINES clause of a LOAD DATA statement...

Is there a workaround?
franc
francwalter [ Mo, 11 Dezember 2006 17:18 ] [ ID #1564122 ]

Re: LIMIT (Subquery)

I don't think its good idea at all.
Anyway take a look at prepared statements (support added in 5.0.7)

SELECT COUNT(*) into [at] cnt from tab2;
PREPARE STMT FROM 'SELECT * FROM tab1 LIMIT ?';
EXECUTE STMT USING [at] cnt;

Franc Walter wrote:
> Hello,
> i didn't find it in the help, i think it is not possible, but i try to
> question anyway:
> Is the MySQL command "LIMIT" possible with a subquery in MySQL 4.0.27?
>
> e.g.: SELECT * FROM tab1 LIMIT (SELECT COUNT(*) from tab2)
>
> this doesn't work.
> MySQL 5.1 writes:
> User variables may be used in contexts where expressions are allowed.
> This does not currently include contexts that explicitly require a
> literal value, such as in the LIMIT clause of a SELECT statement, or
> the IGNORE N LINES clause of a LOAD DATA statement...
>
> Is there a workaround?
> franc
que [ Di, 12 Dezember 2006 13:38 ] [ ID #1565340 ]
Datenbanken » mailing.database.mysql » LIMIT (Subquery)

Vorheriges Thema: constructing a sql query
Nächstes Thema: Deadlock found when trying to get lock (MySQL 5.0 with innodb)