1054 - Unknown column 'XXX' in 'on clause' - mysql

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)

Related

Update sentence with subquery on MySQL

I have the following sentence, that returns the error
Unknown column targets.ID_TARGET in where clause
and I can't find any solution. Could you guys help?
The proposal is update 'sw_automatic' for each row with the value that th subquery provides (0 or 1)
update bt_pry_targets targets
set targets.sw_automatic = (
(
SELECT (CASE WHEN Task.ID_TP_TASKS_GROUPS = 694 THEN '0' ELSE '1' END) AS TYPE_TASK,
Task.ID_TASK FROM bt_tasks AS Task
INNER JOIN bt_pry_cmp_workflows AS BtCmpWorkflows ON (Task.ID_PRY_CMP_WORKFLOW = BtCmpWorkflows.ID_PRY_CMP_WORKFLOW)
INNER JOIN bt_pry_components AS PryComponent ON (PryComponent.ID_PRY_COMPONENT = BtCmpWorkflows.ID_PRY_COMPONENT )
INNER JOIN bt_components AS Component ON (PryComponent.ID_COMPONENT = Component.ID_COMPONENT)
INNER JOIN bt_pry_targets AS PryTarget ON (PryComponent.ID_TARGET = PryTarget.ID_TARGET)
INNER JOIN bt_flows AS Flows ON (Flows.ID_FLOW = Task.ID_FLOW)
WHERE Flows.SW_END_DEPENDENCE = 1
AND PryTarget.ID_TARGET = targets.ID_TARGET
GROUP BY Task.ID_TASK) )
where targets.sw_automatic is null;
In you subquery the column targets.ID_TARGET in not visible
so you could try using you subquery as a join table for updated
update bt_pry_targets targets
inner join (
SELECT (CASE WHEN Task.ID_TP_TASKS_GROUPS = 694 THEN '0' ELSE '1' END) AS TYPE_TASK,
Task.ID_TASK FROM bt_tasks AS Task
INNER JOIN bt_pry_cmp_workflows AS BtCmpWorkflows ON (Task.ID_PRY_CMP_WORKFLOW = BtCmpWorkflows.ID_PRY_CMP_WORKFLOW)
INNER JOIN bt_pry_components AS PryComponent ON (PryComponent.ID_PRY_COMPONENT = BtCmpWorkflows.ID_PRY_COMPONENT )
INNER JOIN bt_components AS Component ON (PryComponent.ID_COMPONENT = Component.ID_COMPONENT)
INNER JOIN bt_pry_targets AS PryTarget ON (PryComponent.ID_TARGET = PryTarget.ID_TARGET)
INNER JOIN bt_flows AS Flows ON (Flows.ID_FLOW = Task.ID_FLOW)
WHERE Flows.SW_END_DEPENDENCE = 1
AND PryTarget.ID_TARGET = targets.ID_TARGET
GROUP BY Task.ID_TASK
) t on t.PryTarget = targets.ID_TARGET
AND targets.sw_automatic is null
set targets.sw_automatic = t.TYPE_TASK

MySQL: JOIN multiple tables

I have the following query I am trying to join 2 tables (' Industry' , 'Country' ) on 2 conditions, but it gives me the following error
Error Code: 1054. Unknown column 'i.id' in 'on clause'
Does anybody know how should I tackle this?
SELECT c.name AS country_name, i.name as industry_name, num_projects, num_consultants, admin_rating
FROM industry i, country c
JOIN (SELECT pc.country_id, pi.industry_id, COUNT(p.id) AS num_projects
FROM project p, project_country pc, project_industry pi
where p.id = pc.project_id and pi.project_id=p.id
GROUP BY pc.country_id,pi.industry_id) x ON x.country_id = c.id and x.industry_id=i.id
JOIN (SELECT u.country_id,ie.industry_id, COUNT(u.id) AS num_consultants
FROM user u, consultant_profile, industry_experience ie
WHERE u.is_active = 1 AND u.type = 0 and
ie.consultant_profile_id= consultant_profile.id
and u.id= consultant_profile.id
GROUP BY u.country_id,ie.industry_id) y ON y.country_id = c.id and y.industry_id = i.id order by num_projects DESC limit 20;
EDIT the table structure is as following:
industry - id
project_industry - industry_id, project_id
industry_experience - consultant_profile_id, industry_id
consultant_profile - id,user_id
Since you still did not provide any sql fiddle
you can start from my one:
http://sqlfiddle.com/#!9/6c0569/1
SELECT pc.country_id, pi.industry_id,
COUNT(p.id) AS num_projects,
COUNT(u.id) AS num_consultants
FROM project p
INNER JOIN project_country pc
ON p.id = pc.project_id
INNER JOIN project_industry pi
ON pi.project_id=p.id
INNER JOIN `user` u
ON u.is_active = 1 AND u.type = 0
and u.country_id = pc.country_id
INNER JOIN industry_experience ie
ON u.id = ie.consultant_profile_id
AND ie.industry_id = pi.industry_id
GROUP BY pc.country_id, pi.industry_id
if you will add some data into that fiddle we can discuss deeper

MySQL - #1054 - Unknown column 'childs' in 'on clause'

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

MySQL limit inside of the subquery produces error

I'm receiving:
[Err] 1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
While executing the following query:
UPDATE account.account
SET STATUS = 'BLOCK'
WHERE
id IN (
SELECT
p.account_id
FROM
log.log
LEFT JOIN player.player p ON (p.id = log.who)
WHERE
vnum = 71054
AND how = 'BUY'
GROUP BY
log.`who`
HAVING
COUNT(log.who) > 2
LIMIT 10
);
Is there a posibility to rewrite this query so MySQL could execute it?
The solution is to join against a subquery rather than use an IN(). The INNER JOIN will only return rows in account matching ids from the limited subquery. It is then possible to do the UPDATE without a WHERE clause.
Update
account.account AS account
INNER JOIN (
SELECT
p.account_id
FROM
log.log
LEFT JOIN player.player p ON (p.id = log.who)
WHERE
vnum = 71054
AND how = 'BUY'
GROUP BY log.`who`
HAVING COUNT(log.who) > 2
LIMIT 10
) subq ON account.id = subq.id
SET STATUS='BLOCK'
To verify the rows that would be modified, use a SELECT first:
SELECT
account.*
FROM
account.account
INNER JOIN (
SELECT
p.account_id
FROM
log.log
LEFT JOIN player.player p ON (p.id = log.who)
WHERE
vnum = 71054
AND how = 'BUY'
GROUP BY log.`who`
HAVING COUNT(log.who) > 2
LIMIT 10
) subq ON account.id = subq.id

Unknown column even though it exists

I have
SELECT
servisler.geo_location,
servisler.ADRES_MERKEZ,
servisler.ADRES_ILCE,
servisler.ADRES_IL,
servisler.FIRMA_UNVANI,
servisler.ADRES_ISTEL,
servisler.YETKILI_ADISOYADI,
urun_gruplari.GRUP_ADI
FROM
servisler
INNER JOIN urun_gruplari ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
INNER JOIN kullanici ON kullanici.SERVIS_RECNO = servisler.RECNO
INNER JOIN kullanici_cihaz ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO AND kullanici_cihaz.URUN_GRUP_NO = urun_gruplari.RECNO
where kullanici.kullanici = 'MAR.EDI.003'
but it says
[Err] 1054 - Unknown column 'kullanici_cihaz.URUN_GRUP_NO' in 'on clause'
enen though the column exits. What is its problem?
schema
Server version: 5.1.33-community-log
I think this is because you're using the kullanici_cihaz table in your ON clause, and that before this table is linked.
Try to rearrange your INNER JOIN :
SELECT
servisler.geo_location,
servisler.ADRES_MERKEZ,
servisler.ADRES_ILCE,
servisler.ADRES_IL,
servisler.FIRMA_UNVANI,
servisler.ADRES_ISTEL,
servisler.YETKILI_ADISOYADI,
urun_gruplari.GRUP_ADI
FROM
servisler
INNER JOIN kullanici ON kullanici.SERVIS_RECNO = servisler.RECNO
INNER JOIN kullanici_cihaz ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO
INNER JOIN urun_gruplari ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
WHERE
kullanici.kullanici = 'MAR.EDI.003'
EDIT
You also have some 'loop inner join', I removed it :
INNER JOIN urun_gruplari ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
INNER JOIN kullanici_cihaz ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO AND kullanici_cihaz.URUN_GRUP_NO = urun_gruplari.RECNO
Because kullanici.SERVIS_RECNO does not exist when you join the tables: servisler, urun_gruplari. Join table kullanici first before joining urun_gruplari.
SELECT servisler.geo_location,
servisler.ADRES_MERKEZ,
servisler.ADRES_ILCE,
servisler.ADRES_IL,
servisler.FIRMA_UNVANI,
servisler.ADRES_ISTEL,
servisler.YETKILI_ADISOYADI,
urun_gruplari.GRUP_ADI
FROM servisler
INNER JOIN kullanici
ON kullanici.SERVIS_RECNO = servisler.RECNO
INNER JOIN urun_gruplari
ON kullanici_cihaz.URUN_GRUP_NO= urun_gruplari.RECNO
INNER JOIN kullanici_cihaz
ON kullanici.RECNO = kullanici_cihaz.KUL_RECNO AND
kullanici_cihaz.URUN_GRUP_NO = urun_gruplari.RECNO
where kullanici.kullanici = 'MAR.EDI.003'