I'm very new in comlex SQL queries. So, I'm trying to debug SQL statement generated by Magento:
SELECT `e`.*,
`cat_index`.`position` AS
`cat_index_position`,
price_index.price AS
`indexed_price`,
`price_index`.`price`,
`price_index`.`final_price`,
IF(`price_index`.`tier_price`, Least(`price_index`.`min_price`,
`price_index`.`tier_price`),
`price_index`.`min_price`) AS
`minimal_price`,
`price_index`.`min_price`,
`price_index`.`max_price`,
`price_index`.`tier_price`,
GROUP_CONCAT(CONVERT(catalog_product_relation.child_id, CHAR(8))) AS
`children`,
`sfoi`.`price` AS
`confprice`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_category_product_index` AS `cat_index`
ON cat_index.product_id = e.entity_id
AND cat_index.store_id = 1
AND cat_index.visibility IN( 2, 4 )
AND cat_index.category_id = '3'
INNER JOIN `catalog_product_index_price` AS `price_index`
ON price_index.entity_id = e.entity_id
AND price_index.website_id = '1'
AND price_index.customer_group_id = 0
INNER JOIN `catalog_product_index_eav` AS `attributeA`
ON attributeA.entity_id = e.entity_id
AND attributeA.attribute_id = '184'
AND attributeA.store_id = '1'
AND attributeA.value IN ( 50 )
INNER JOIN `catalog_product_index_eav` AS `attributeB`
ON attributeB.entity_id = e.entity_id
AND attributeB.attribute_id = '185'
AND attributeB.store_id = '1'
AND attributeB.value IN ( 95 )
LEFT JOIN `catalog_product_relation`
ON e.entity_id = catalog_product_relation.parent_id
LEFT JOIN `catalog_product_flat_1` AS `sfoi`
ON sfoi.entity_id = `children`
GROUP BY `e`.`entity_id`
ORDER BY `confprice` DESC
LIMIT 9
Everything work fine until:
LEFT JOIN `catalog_product_flat_1` AS `sfoi`
ON sfoi.entity_id = `children`
I get following error:
#1054 - Unknown column 'children' in 'on clause'
I've seen smilar posts, but I can't seem to figure it out by myself. Please, help me.
EDIT:
PHP code that generates this query:
$this->_collection->getSelect()->
joinLeft(
'catalog_product_relation',
'e.entity_id = catalog_product_relation.parent_id',
'GROUP_CONCAT(CONVERT(catalog_product_relation.child_id, CHAR(8))) as children'
);
$this->_collection->getSelect()->joinLeft('catalog_product_flat_1 AS sfoi',
'sfoi.entity_id = children',
'sfoi.price AS confprice'
)->order('confprice desc');
Actually, I'm trying to join two tables (catalog_product_relation and catalog_product_flat_1). But I can't get access to the "children" column after joining first table.
Add the corresponding column to children table in the ON condition.
ON sfoi.entity_id = catalog_product_relation.child_id
Related
symfony version: 5.1
There is a sql statement and work in command:
SELECT
p.sn as pid,
p.title as title,
pc.title as categoryName
GROUP_CONCAT(CONCAT_WS('=', pa.`meta_key`, pa.`meta_value`) order by a.`sort_by` asc) as filters
FROM `product` as p
LEFT JOIN `product_category` as pc on pc.id = p.`category_id`
LEFT JOIN `product_model` as pm on p.`model_id` = pm.id
LEFT JOIN `brand` as b on b.id = p.brand_id
LEFT JOIN `product_attribute` as pa on pa.`product_id` = p.id
LEFT JOIN `attribute` as a on pa.`attribute_id` = a.id
WHERE p.status = 1 and a.`search_enable` = 1 and pa.`attribute_id` is not null
and FIND_IN_SET('name=test', (
SELECT
GROUP_CONCAT(CONCAT_WS('=', pa2.`meta_key`, pa2.`meta_value`))
FROM `product_attribute` as pa2
LEFT JOIN `attribute` as a2 on a2.`id` = pa2.`attribute_id`
WHERE pa2.`product_id` = p.`id` and a2.`search_enable` = 1
GROUP BY pa2.`product_id`
)) and FIND_IN_SET('age=18', (
SELECT
GROUP_CONCAT(CONCAT_WS('=', pa2.`meta_key`, pa2.`meta_value`))
FROM `product_attribute` as pa2
LEFT JOIN `attribute` as a2 on a2.`id` = pa2.`attribute_id`
WHERE pa2.`product_id` = p.`id` and a2.`search_enable` = 1
GROUP BY pa2.`product_id`
))
GROUP BY p.`id`;
But the function of FIND_IN_SET is not working in doctrine query.
$query->andWhere("FIND_IN_SET('age=18', '(SELECT GROUP_CONCAT(CONCAT_WS('=', pa2.metaKey, pa2.metaKey)) FROM ProductAttribute as pa2 LEFT JOIN Attribute as a2 on a2.id = pa2.attribute WHERE pa2.product = p.id and a2.searchEnable = 1 GROUP BY pa2.product)')");
I get error message:
PHP message: [critical] Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Syntax Error] line 0, col 657: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '='"
If I change the find_in_set statement remove the single quotes wit select statement:
$query->andWhere("FIND_IN_SET('age=18', (SELECT GROUP_CONCAT(CONCAT_WS('=', pa2.metaKey, pa2.metaKey)) FROM ProductAttribute as pa2 LEFT JOIN Attribute as a2 on a2.id = pa2.attribute WHERE pa2.product = p.id and a2.searchEnable = 1 GROUP BY pa2.product)");
I got this error message:
PHP message: [critical] Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Syntax Error] line 0, col 625: Error: Expected Literal, got 'SELECT'"
How to use the FIND_IN_SET with select statement?
About the relationship of table:
1, product and product_category are manyToOne;
2, product and product_model are manyToOne;
3, product and brand are manyToOne;
4, product and product_attribute are oneToMany;
5, product_attribute and attribute are ManyToOne;
The product has many attributes that a part of it will be filtered when user search product with keywords.
So, I want to a filter function same as amazon that user can click the option of filter to get a new list of products from search result.
I have the following code which I'm using to get prices from a Magento database:
SELECT
`e`.`sku`,
`price_index`.`price` AS `RRP`,
`price_index`.`final_price` AS `Dealer Price`
FROM
`catalog_product_entity` AS `e`
INNER JOIN
`catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND
price_index.website_id = '1' AND price_index.customer_group_id = 7
This works fine, however I need to get an additional price from "final_price" but with customer group 8 instead of 7 for example.
The results from my current script display:
SKU, RRP, Dealer Price
The Results I would like:
SKU, RRP, Dealer Price, Trader Price
Thanks
You just need left joins instead of inner joins. Note in the tested example below that I'm renaming the alias of price_index to price_index2 in the second join on the same table.
SELECT
`e`.`sku`,
`price_index`.`price` AS `RRP`,
`price_index`.`final_price` AS `Dealer Price`,
`price_index2`.`final_price` AS `Trader Price`
FROM
`catalog_product_entity` AS `e`
LEFT JOIN
`catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND
price_index.website_id = '1' AND price_index.customer_group_id = 7
LEFT JOIN
`catalog_product_index_price` AS `price_index2` ON price_index2.entity_id = e.entity_id AND
price_index2.website_id = '1' AND price_index2.customer_group_id = 8
This is my simplified query:
SELECT `student`.`id`, `student`.`firstname`, `student`.`lastname`,
IFNULL(`review`.`score`, '0') AS `adminreview_score`,
`reviewcolor`.`color` AS adminreview_color FROM (`student`)
LEFT JOIN `review` ON `review`.`student_id` = `student`.`id` AND
review.reviewtype_id = 13
LEFT JOIN `reviewcolor` ON `reviewcolor`.`score` = `adminreview_score`
WHERE `student`.`id` > '0'
And this is the error I get:
Error Number: 1054
Unknown column 'adminreview_score' in 'on clause'
Note that there may be no row in review table with the situation:
`review`.`student_id` = `student`.`id` AND review.reviewtype_id = 13
In this situation, I want adminreview_score to be set as 0, and I hope reviewcolor.color be NULL or empry ()
Thank you
Try this: (I replaced the alias with the actual expression. Note that aliases from the SELECT clause can't be used in the rest of the SQL expression.)
SELECT `student`.`id`, `student`.`firstname`, `student`.`lastname`,
IFNULL(`review`.`score`, '0') AS `adminreview_score`,
`reviewcolor`.`color` AS adminreview_color
FROM (`student`)
LEFT JOIN `review` ON `review`.`student_id` = `student`.`id` AND review.reviewtype_id = 13
LEFT JOIN `reviewcolor` ON `reviewcolor`.`score` = IFNULL(`review`.`score`, '0')
WHERE `student`.`id` > '0'
You can use a variable, try This
set #a:='';
SELECT `student`.`id`, `student`.`firstname`, `student`.`lastname`,
IFNULL(#a:=`review`.`score`, #a:=0),
`reviewcolor`.`color` AS adminreview_color FROM (`student`)
LEFT JOIN `review` ON `review`.`student_id` = `student`.`id` AND
review.reviewtype_id = 13
LEFT JOIN `reviewcolor` ON `reviewcolor`.`score` = #a
WHERE `student`.`id` > '0'
The SQL statement below used to work a few years ago, now it doesn't and I get this error message:
1054 - Unknown column 'Promotion_Card_details.Card_id' in 'on clause'
This is my SQL statement:
SELECT DISTINCT Promotion_Card_details.Card_id,
Promotion_Merchant_details.Merchant_id,
Promotion_details.Promotion_id,
Issuer_details.IssuerName,
Card_details.CCType,
PaymentType_details.PaymentTypeName,
Merchant_details.MerchantName,
PromotionText,
PromotionEndDate
FROM Promotion_details
INNER JOIN Card_details ON (Card_details.Card_id=Promotion_Card_details.Card_id)
INNER JOIN Issuer_details ON (Issuer_details.Issuer_id=Card_details.Issuer_id)
INNER JOIN PaymentType_details ON (PaymentType_details.PaymentType_id=Card_details.PaymentType_id)
INNER JOIN Merchant_details ON (Merchant_details.Merchant_id=Promotion_Merchant_details.Merchant_id)
INNER JOIN Promotion_Merchant_details ON (Promotion_Merchant_details.Promotion_id=Promotion_details.Promotion_id)
INNER JOIN Promotion_Card_details ON (Promotion_details.Promotion_id=Promotion_Card_details.Promotion_id)
WHERE ((Promotion_details.Promotion_id = '13' OR Promotion_details.Promotion_id = '14'
OR
Promotion_details.Promotion_id = '15' OR Promotion_details.Promotion_id = '16' OR
Promotion_details.Promotion_id = '17' OR Promotion_details.Promotion_id = '18' OR
Promotion_details.Promotion_id = '19' OR Promotion_details.Promotion_id = '20' OR
Promotion_details.Promotion_id = '21' OR Promotion_details.Promotion_id = '22' OR
Promotion_details.Promotion_id = '23' OR Promotion_details.Promotion_id = '24' OR
Promotion_details.Promotion_id = '25' OR Promotion_details.Promotion_id = '361'
OR Promotion_details.Promotion_id = '364' OR Promotion_details.Promotion_id = '382') )
Any help would be greatly appreciated as I have tried parentheses around certain parts, but it still hasn't fixed it.
JOINs are left-associative, so a given ON clause can only refer to tables that have already been mentioned. So, reorder these:
FROM Promotion_details
INNER JOIN Card_details ON (Card_details.Card_id=Promotion_Card_details.Card_id)
INNER JOIN Issuer_details ON (Issuer_details.Issuer_id=Card_details.Issuer_id)
INNER JOIN PaymentType_details ON (PaymentType_details.PaymentType_id=Card_details.PaymentType_id)
INNER JOIN Merchant_details ON (Merchant_details.Merchant_id=Promotion_Merchant_details.Merchant_id)
INNER JOIN Promotion_Merchant_details ON (Promotion_Merchant_details.Promotion_id=Promotion_details.Promotion_id)
INNER JOIN Promotion_Card_details ON (Promotion_details.Promotion_id=Promotion_Card_details.Promotion_id)
into this order:
FROM Promotion_details
INNER JOIN Promotion_Card_details ON (Promotion_details.Promotion_id=Promotion_Card_details.Promotion_id)
INNER JOIN Card_details ON (Card_details.Card_id=Promotion_Card_details.Card_id)
INNER JOIN Issuer_details ON (Issuer_details.Issuer_id=Card_details.Issuer_id)
INNER JOIN PaymentType_details ON (PaymentType_details.PaymentType_id=Card_details.PaymentType_id)
INNER JOIN Promotion_Merchant_details ON (Promotion_Merchant_details.Promotion_id=Promotion_details.Promotion_id)
INNER JOIN Merchant_details ON (Merchant_details.Merchant_id=Promotion_Merchant_details.Merchant_id)
I get this error in my Magento store when I turn on flat catalogs:
SELECT COUNT(_table_views.event_id) AS `views`, `e`.*, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`category_ids`, `e`.`created_at`, `e`.`enable_googlecheckout`, `e`.`has_options`, `e`.`image_label`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`weight`, `e`.`weight_type`, `e`.`display_price_group_0`, `e`.`display_price_group_1`, `e`.`display_price_group_2`, `e`.`display_price_group_3`, `e`.`display_price_group_4`, `cat_index`.`position` AS `cat_index_position` FROM `report_event` AS `_table_views`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = _table_views.object_id AND e.entity_type_id = 10
INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.category_id='10' WHERE (_table_views.event_type_id = '1') AND (logged_at >= '2010-01-06 17:03:57') AND (logged_at <= '2010-01-13 17:03:57') GROUP BY `e`.`entity_id` HAVING (views > 0) ORDER BY `views` desc LIMIT 10
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'e.enable_googlecheckout' in 'field list'
Does anyone know how I would go about fixing this?
Describe the table catalog_product_entity using
describe catalog_product_entity;
and see if enable_googlecheckout is present in the table as a column. If not present you can try taking it out of the query as:
SELECT COUNT(_table_views.event_id) AS `views`, `e`.*, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`category_ids`, `e`.`created_at`, `e`.`has_options`, `e`.`image_label`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`weight`, `e`.`weight_type`, `e`.`display_price_group_0`, `e`.`display_price_group_1`, `e`.`display_price_group_2`, `e`.`display_price_group_3`, `e`.`display_price_group_4`, `cat_index`.`position` AS `cat_index_position` FROM `report_event` AS `_table_views`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = _table_views.object_id AND e.entity_type_id = 10
INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.category_id='10' WHERE (_table_views.event_type_id = '1') AND (logged_at >= '2010-01-06 17:03:57') AND (logged_at <= '2010-01-13 17:03:57') GROUP BY `e`.`entity_id` HAVING (views > 0) ORDER BY `views` desc LIMIT 10