I need to use subqueries to name columns in a query. I have written a statement with subqueries in the SELECT section, but I am getting the following error message (returned from Codeigniter):
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘(SELECT
ps_url_nameFROMproduct_selectsWHEREps_id= ‘1’),psc_opt_2A’ at line 1
SELECT
`psc_opt_1` AS (SELECT `ps_url_name` FROM `product_selects` WHERE `ps_id` = '1'),
`psc_opt_2` AS (SELECT `ps_url_name` FROM `product_selects` WHERE `ps_id` = '2'),
`psc_opt_3` AS (SELECT `ps_url_name` FROM `product_selects` WHERE `ps_id` = '3'),
`psc_opt_4` AS (SELECT `ps_url_name` FROM `product_selects` WHERE `ps_id` = '4'),
`psc_opt_5` AS (SELECT `ps_url_name` FROM `product_selects` WHERE `ps_id` = '5')
FROM (`product_stock_control`)
WHERE `psc_prod_id` = '5'
Filename: models/products_model.php
Line Number: 602
Any ideas what is going wrong?
ASis a column alias operator, meaning you are selecting these variables as an identifier. You cannot use the operator to identify a variable with a query.Reference: http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html
Instead, you’ll need to reverse how you use the alias operator like so:
Ideally, you should avoid using subqueries (especially in this case) to avoid table-locking overhead.