Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rezamk's avatar

Strange Error with Laravel DB query

I really hope somebody on here could help me with this very strange issue.

I have an Laravel DB query which runs absolutely fine on my development host, however I get an error when I deploy it to my UAT host.

[EDIT - simplified example]

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'c.xxx' in 'order clause' (SQL: SELECT c.* 
FROM (SELECT 0 AS xxx, u.* FROM users u) c
ORDER BY c.xxx DESC)

This would be absolutely fine if the SQL had an issue, however, it seems the SQL itself is absolutely fine too when I copy / paste it into workbench, but the PHP gives the above error.

I have reduced the query down to the minimum to demonstrate the issue.

[EDIT - simplified example]

return DB::select(" SELECT c.* 
                    FROM   (SELECT 0 AS xxx, u.* FROM users u) c
                    ORDER  BY c.xxx DESC");

Here is the generated query. Replacing 'users' with any table really should run so I don't think its anything on the database level. The users table is no different to the one shipped with laravel auth.

SELECT c.* 
FROM   (SELECT 0 AS xxx, 
               u.* 
        FROM   users u) c 
ORDER  BY c.xxx DESC 

Any help much appreciated as I am stumped on this one...

Any ideas why the database query could possibly behave differently to the query run via Laravel / php? Any idea why one machine would behave fine and there other display the above error?

0 likes
10 replies
ahmeddabak's avatar

Did you run php artisan migrate on your production server, this can happens if you added the column later in the development, and it was not migrated in the development server, please check in your mysql client (phpmyadmin , sequelpro...) if the column actually exists in the database, before we explore other options

petrit's avatar

I looks like you are missing a column called is_featured in your table called contents.

rezamk's avatar

Thanks for both responses. I wish that was the case but when I run the query in the error message on the DB directly it works without any issues.

(I have now added more details to the question - sorry for the confusion).

The "is_featured" column is generated via DB::raw (I have just included the php code). It is generated on the fly, but for the sake of simplicity I have changed it to just always return 0 here.

Any idea why the SQL would work on workbench but not when generated and run by Laravel?

ahmeddabak's avatar

Answer copied from stackoverflow

As is the case with most relational database systems, the SELECT clause is processed after the GROUP BY clause. This means you cannot use columns aliased in the SELECT (such as ts in this example) in your GROUP BY.

There are essentially two ways around this. Both are correct, but some people have preference for one over the other for various reasons.

First, you could group by the original expression, rather than the alias. This results in duplicate code, as you will have the exact same expression in both your SELECT and GROUP BY clause.

rezamk's avatar

Hi Ahmed - I thought the exact same thing initially (when the query was more complicated and actually did have a group by).

Now in its simplest form, all the SQL is doing is adding a column to the inner query, then sorting by it in the outer query. There is no grouping at all.

Also, if this were the issue, surely workbench would also complain (as would my development environment)?

Thanks!

ahmeddabak's avatar

@REZAMK - The example contained grouping, but it also applies to sort by, try the two solutions provided on stackoverflow, for your sort by.

rezamk's avatar

Happy to accept if I have made a mistake somewhere, but it looks to me like I am already doing exactly what is suggested in the 2nd option. I have an internal query, called c, and I am sorting by a column in here?

How would you suggest the query is changed? Simplified even further...

SELECT `c`.* 
FROM   (SELECT 0 AS `is_featured`, `contents`.* FROM   `contents`) AS `c` 
ORDER  BY `c`.`is_featured` DESC 

Again, this query works fine in workbench but not when run on my UAT box in php. It also runs fine on the development machine... which may provide a clue.

rezamk's avatar

To avoid any doubt, I tried this:

return DB::select(" SELECT `c`.* 
                    FROM   (SELECT 0 AS `xxx`, u.* FROM   `users` u) AS `c` 
                    ORDER  BY `c`.`xxx` DESC");

The users table is unchanged from the one shipped with laravel auth so nothing special here.

I have also removed all eloquent references.

And the error:

(3/3) QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'c.xxx' in 'order clause' (SQL: SELECT `c`.* 
FROM (SELECT 0 AS `xxx`, 
u.* 
FROM `users` u) AS `c` 
ORDER BY `c`.`xxx` DESC)

Edited the question to use this example instead.

rezamk's avatar

Any help would be appreciated.

Another finding... When I run the below code on my UAT environment, it works fine. Suggesting Laravel is doing something internally?

// Create connection  
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT c.* FROM (SELECT 0 AS xxx, u.* FROM users u)  c ORDER BY c.xxx DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>ID</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["id"]."</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
heikokrebs's avatar

Hey rezamk,

have you found any solution to this problem?

Please or to participate in this conversation.