I have a select query like below:
SELECT * FROM A
LEFT JOIN B ON B.x = A.y
LEFT JOIN C ...
WHERE ....
GROUP BY ...
ORDER BY ...;
All tables have id column and query works well. Result have many id columns without error and driver handles ambiguous issues. But i need to have LIMIT on results so i wrap it with another select query like this:
SELECT * FROM (
SELECT * FROM A
LEFT JOIN B ON B.x = A.y
LEFT JOIN C ...
WHERE ....
GROUP BY ...
ORDER BY ...
) AS x WHERE 1 LIMIT 1000;
And now i get Duplicate column name 'id' error!
PS: The full query is complicated and i need to use * (listing column names is not possible), and i can't use limit in main query because of joins, group by order by and etc.
limit 1000to your first query?SELECT *, and using it in production code is never a good idea, and in almost all cases is just laziness. I'm betting in the 42 minutes since you asked this question (with no answers as I comment) that you could have written out all the columns you need. The benefit of this is not just removing the duplicate column error, it also future proofs your column positions against DDL changes such as new columns being added, or columns being removed, it will also reduce I/O on the server and network traffic.