aero
aero

Reputation: 69

Running a Sub Query in a Mysql Statment

I got the following query but it is not working. Any help appreciated.

mysql_query("SELECT * FROM 
      (SELECT * FROM Names WHERE suffix='1' AND word!='$main_suffix' ORDER BY votes DESC LIMIT 15)

     ORDER BY Rand() LIMIT 3");

Upvotes: 0

Views: 24

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133360

You must add the alias eg:as t at the end of subselect

mysql_query("SELECT * FROM 
                (SELECT * 
                FROM Names 
                WHERE suffix='1' 
                AND word!='$main_suffix' 
                ORDER BY votes DESC LIMIT 15) as t
            ORDER BY Rand() LIMIT 3");

Upvotes: 0

Bart Friederichs
Bart Friederichs

Reputation: 33511

All subselects need an alias:

mysql_query("SELECT * FROM (
       SELECT * FROM Names 
       WHERE suffix='1' AND word!='$main_suffix' 
       ORDER BY votes DESC LIMIT 15) 
     ) YourAliasName
     ORDER BY Rand() LIMIT 3");

Also, don't use mysql_query, but use MySQLi or PDO instead.

Upvotes: 1

Related Questions