Mysql update with subquery join - mysql

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)

Related

#1054 - Unknown column 'purchasedetail.itemId' in 'on clause'

I have an error in purchasedetail table when I am trying to fetch this record. This is my select query:
SELECT itemstock.itemId
FROM itemstock
JOIN item ON item.itemId = purchasedetail.itemId
JOIN purchasemaster ON purchasemaster.purchaseMasterId = purchasedetail.purchaseMasterId
JOIN purchasedetail ON purchasedetail.itemId = item.itemId
JOIN party ON party.partyId = purchasemaster.partyId
WHERE
purchasemaster.partyId = ".$_REQUEST['partyId']."
AND itemstock.quantity > 0
GROUP BY itemName,itemCode
This is the error:
#1054 - Unknown column 'purchasedetail.itemId' in 'on clause'
When joining tables, the columns you join "on" have to belong to those tables. You are trying to join on columns from other tables that you haven't mentioned yet. Try this:
SELECT itemstock.itemId
FROM itemstock
JOIN item ON item.itemId = itemstock.itemId
JOIN purchasedetail ON purchasedetail.itemId = item.itemId
JOIN purchasemaster ON purchasemaster.purchaseMasterId = purchasedetail.purchaseMasterId
JOIN party ON party.partyId = purchasemaster.partyId
WHERE purchasemaster.partyId = 5 AND itemstock.quantity > 0 GROUP BY itemName,itemCode

MySQL Unknown column in having clause

I'm trying to get all the wins per team, however, SQL decides to throw an error
The following query is being executed:
SELECT `t`.`teamcode`, COUNT(*) AS `gewonnen`
FROM `Team` `t`
INNER JOIN `Wedstrijd` `w` ON `w`.`teamthuis` = `t`.`teamcode`
GROUP BY `w`.`teamthuis`
HAVING `w`.`scorethuis` > `w`.`scoreuit`
#1054 - Unknown column 'w.scorethuis' in 'having clause'
Without aliases:
SELECT `Team`.`teamcode`, COUNT(*) AS `gewonnen`
FROM `Team`
INNER JOIN `Wedstrijd` ON `Wedstrijd`.`teamthuis` = `Team`.`teamcode`
GROUP BY `Wedstrijd`.`teamthuis`
HAVING `Wedstrijd`.`scorethuis` > `Wedstrijd`.`scoreuit`
#1054 - Unknown column 'Wedstrijd.scorethuis' in 'having clause'
There is no need to use HAVING. Try WHERE instead:
SELECT `t`.`teamcode`, COUNT(*) AS `gewonnen`
FROM `Team` `t`
INNER JOIN `Wedstrijd` `w` ON `w`.`teamthuis` = `t`.`teamcode`
WHERE `w`.`scorethuis` > `w`.`scoreuit`
GROUP BY `w`.`teamthuis`
I think if you had select w.scorethuis w.scoreuit both in select statement then 'Having;' would work. But I faced the same problem and I resolved it by the above way.
SELECT `t`.`teamcode`, COUNT(*),***`w`.`scorethuis`, `w`.`scoreuit`*** AS `gewonnen`
FROM `Team` `t`
INNER JOIN `Wedstrijd` `w` ON `w`.`teamthuis` = `t`.`teamcode`
GROUP BY `w`.`teamthuis`
HAVING `w`.`scorethuis` > `w`.`scoreuit`

MySQL IFNULL, Alias and LEFT JOIN result an Unknown column error

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'

Updating users table from complex SQL query, users.id not recognised?

So the other day, I asked this question about how to combine three complex queries and found a way to do it. Now I'm trying to use those queries to update a field in the users table and can't find a way to make it work. Here's the query:
update users set field_sum =(
select sum(field_sum) from (
select sum(field_one) as field_sum
from t_a join t_b on (t_a.bid = t_b.id) where t_b.user_id=users.id
union all
select sum(field_two) as field_sum
from t_c join t_d on (t_c.did = t_d.id) where t_d.user_id=users.id
union all
select sum(field_three) as field_sum
from t_e where t_e.user_id=users.id
) as field_sumT
)
When I try to run it, I get the following error: ERROR 1054 (42S22): Unknown column 'users.id' in 'where clause'. When I try removing the .user_id=users.id bit from each where clause, it will run but ends up with the total sum of field_sum, not just the field_sum for that user. Is there any way to accomplish this?
Use:
UPDATE USERS u
LEFT JOIN (SELECT t_b.user_id,
SUM(field_one) as field_sum
FROM t_a
JOIN t_b on t_a.bid = t_b.id
GROUP BY t_b.user_id) a ON a.user_id = u.id
LEFT JOIN (SELECT t_d.user_id,
SUM(field_two) as field_sum
FROM t_c
JOIN t_d on t_c.did = t_d.id
GROUP BY t_d.user_id) b ON b.user_id = u.id
LEFT JOIN (SELECT t_e.user_id,
SUM(field_three) as field_sum
from t_e
GROUP BY t_e.user_id) c ON c.user_id = u.id
SET field_num = COALESCE(a.field_sum, 0) + COALESCE(b.field_sum, 0) + COALESCE(c.field_sum, 0)
Caveat
This will set any users with no records in the supporting rows to have a field_sum value of zero. Do you only want to update those with a record in at least one of those tables?

Magento Store - SQL Error

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