NCP
NCP

Reputation: 81

Showing invalid syntax

its a database column companies under the database company_directory

SELECT a.company_name FROM 
         (SELECT companies.company_name 
        FROM companies WHERE  companies.profile_progress 
        BETWEEN 80 AND 100) a ORDER BY  RAND() DESC LIMIT 9 u ORDER BY profile_progress

facing issue with this error Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'u ORDER BY profile_progress' at line 4 in C:\xampp\htdocs\company-directory\home.php:20 Stack trace: #0 C:\xampp\htdocs\company-directory\home.php(20): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\company-directory\home.php on line 20

Upvotes: 1

Views: 31

Answers (1)

Jim Macaulay
Jim Macaulay

Reputation: 5141

No need to use sub query, you can directly use ORDER BY clause and also not sure what is the purpose you use RAND() function in the ORDER BY clause, it is a function that generates random numbers and there is no point in sorting using the function. You can sort with respective column and fetch the result.

SELECT companies.company_name 
FROM companies WHERE  companies.profile_progress 
BETWEEN 80 AND 100 ORDER BY profile_progress LIMIT 9; 

Upvotes: 1

Related Questions