I have the update query:
UPDATE cash_billings_bills_articles
SET cash_billings_bills_articles.cashbillingbillarticle_cost = (SELECT articles_pricehistory.articlepricehistory_cost
FROM articles_pricehistory
LEFT JOIN cash_billings_bills
ON cash_billings_bills_articles.cashbillingbill_id = cash_billings_bills.cashbillingbill_id
WHERE articles_pricehistory.article_id = cash_billings_bills_articles.article_id AND
articles_pricehistory.articlepricehistory_date <= cash_billings_bills.cashbillingbill_date
ORDER BY articles_pricehistory.articlepricehistory_date DESC
LIMIT 1
);
But i got the error: Error Code: 1054. Unknown column 'cash_billings_bills_articles.cashbillingbill_id' in 'on clauseError Code: 1054. Unknown column 'cash_billings_bills_articles.cashbillingbill_id' in 'on clause' 0.000 sec
UPDATE Tables Structures:
Your join condition seems to be wrong .In your query you try to join tables articles_pricehistory and cash_billings_bills on condition
cash_billings_bills_articles.cashbillingbill_id = cash_billings_bills.cashbillingbill_id
where cash_billings_bills.cashbillingbill_id column should be compared with some column of articles_pricehistory table not cash_billings_bills_articles table.
Your query should be more like
UPDATE cash_billings_bills_articles
SET
cash_billings_bills_articles.cashbillingbillarticle_cost = (SELECT
articles_pricehistory.articlepricehistory_cost
FROM
articles_pricehistory
LEFT JOIN
cash_billings_bills_articles ON cash_billings_bills_articles.article_id = articles_pricehistory.article_id
LEFT JOIN
cash_billings_bills ON cash_billings_bills_articles.cashbillingbill_id = cash_billings_bills.cashbillingbill_id
WHERE
articles_pricehistory.articlepricehistory_date <= cash_billings_bills.cashbillingbill_date
ORDER BY articles_pricehistory.articlepricehistory_date DESC
LIMIT 1)
Is there a limit of subquerys where the referencing outer column is working on?
I tried this query:
SELECT
`userouter`.`id` AS `user_id`
FROM
`users` AS `userouter`
WHERE
123456 = (
SELECT
SUM(`tmp`.`sum`) AS `sum_total`
FROM
(
SELECT
SUM(`invoiceposition`.`number` * `invoiceposition`.`amount`) AS `sum`
FROM
`invoices` AS `invoice` INNER JOIN
`invoicepositions` AS `invoiceposition` ON
`invoice`.`id` = `invoiceposition`.`invoice`
WHERE
`invoice`.`user` = `userouter`.`id`
GROUP BY
`invoice`.`id`
) AS `tmp`
)
GROUP BY
`userouter`.`id`
And i get Error Code: 1054. Unknown column 'userouter.id' in 'where clause'
How can i reference the userouter.id in the sub-sub query?
As it seems in a normal way not possible to resolve the problem (double nested subquery reference to outer query), i now solved the problem by creating a mysql function with the user id as parameter.
hope this will help other searchers
Remove the double nesting.
SELECT
`userouter`.`id` AS `user_id`
FROM
`users` AS `userouter`
LEFT JOIN (
SELECT
`invoice`.`user` as `user_id`, SUM(`invoiceposition`.`number` * `invoiceposition`.`amount`) AS `sum`
FROM
`invoices` AS `invoice` INNER JOIN
`invoicepositions` AS `invoiceposition` ON
`invoice`.`id` = `invoiceposition`.`invoice`
WHERE
`invoice`.`user` = `userouter`.`id`
GROUP BY
`invoice`.`id`
) AS `tmp`
ON `tmp`.`user_id` = `userouter`.`id`
WHERE
123456 = `userouter`.`id`
GROUP BY
`userouter`.`id`
I'm getting this error and i don't know what to do
#1054 - Unknown column 'ple.playlists' in 'on clause'
Please help, I'm testing directly in phpMyAdmin, heres my query:
SELECT
u.`idu` AS `user_id`,
u.`username` AS `user_name`,
u.`first_name` AS `first_name`,
u.`last_name` AS `last_name`,
u.`country` AS `user_country`,
u.`image` AS `user_image`,
u.`cover` AS `user_cover`,
u.`description` AS `user_description`,
pl.`id` AS `playlist_id`,
pl.`name` AS `playlist_name`,
t.`id` AS `track_id`,
t.`title` AS `track_title`,
t.`name` AS `track_name`,
t.`art` AS `track_art`,
t.`likes` AS `track_likes`,
t.`downloads` AS `track_downloads`,
t.`views` AS `track_plays`
FROM users AS u
INNER JOIN playlists AS pl
ON u.idu=pl.by
INNER JOIN playlistentries AS ple
ON pl.id=ple.playlists
INNER JOIN tracks AS t
ON ple.track=t.id
ORDER BY `user_id` ASC, `playlist_id` ASC, `track_id` ASC
Im using this query:
SELECT `projects`.*,
(SELECT SUM(`amount`)
FROM `accountprojectspayment`
WHERE `projects_id` = `projects`.`id`) AS `payed`
FROM `projects`
INNER JOIN `bids` ON `bids`.`id` = `projects`.`bids_id`
HAVING `bids`.`amount` >= `payed`
i get this error: Unknown column 'bids.amount' in 'having clause
But if i change the code to this:
SELECT `projects`.*, `bids`.`amount`,
(SELECT SUM(`amount`)
FROM `accountprojectspayment`
WHERE `projects_id` = `projects`.`id`) AS `payed`
FROM `projects`
INNER JOIN `bids` ON `bids`.`id` = `projects`.`bids_id`
HAVING `bids`.`amount` >= `payed`
the problem get solved but i do not want to use Select bids.amount
Use a derived table e.g. something like this:
SELECT `DT1`.*
FROM (SELECT `projects`.*,
(SELECT SUM(`amount`)
FROM `accountprojectspayment`
WHERE `projects_id` = `projects`.`id`) AS `payed`
FROM `projects`) AS `DT1`
INNER JOIN `bids` ON `bids`.`id` = `DT1`.`bids_id`
WHERE `bids`.`amount` >= `DT1`.`payed`;
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