Not unique table/alias in mysql while joining from different database - mysql

I get the error [Err] 1066 - Not unique table/alias: 'vtiger_leadscf'
UPDATE vtigerdb.vtiger_leadscf
JOIN vtiger_new.vtiger_leaddetails ON vtiger_new.vtiger_leadscf.leadid = vtiger_new.vtiger_leaddetails.leadid
JOIN vtiger_new.vtiger_leadaddress ON vtiger_new.vtiger_leadaddress.leadaddressid = vtiger_new.vtiger_leadscf.leadid
JOIN vtiger_new.vtiger_crmentity ON vtiger_new.vtiger_crmentity.crmid = vtiger_new.vtiger_leadscf.leadid
JOIN vtiger_new.vtiger_users ON vtiger_new.vtiger_users.id = vtiger_new.vtiger_crmentity.smcreatorid
JOIN vtigerdb.vtiger_leadscf ON vtigerdb.vtiger_leadscf.leadid = vtiger_new.vtiger_leadscf.leadid
SET vtigerdb.vtiger_leadscf.cf_953 = CONCAT(vtiger_new.vtiger_users.first_name,' ',vtiger_new.vtiger_users.last_name)
WHERE vtigerdb.vtiger_leadscf.leadid = vtiger_new.vtiger_leadscf.leadid
AND cf_953 = ""
can't figure out whats wrong with it.

Last join and update reference the same table.
Use JOIN table AS other_name to avoid this problem.
Here is the corrected query.
Plus I think you meant vtiger_new on the last join and not vtigerdb as you wrote.
UPDATE vtigerdb.vtiger_leadscf
JOIN vtiger_new.vtiger_leaddetails ON vtiger_new.vtiger_leadscf.leadid = vtiger_new.vtiger_leaddetails.leadid
JOIN vtiger_new.vtiger_leadaddress ON vtiger_new.vtiger_leadaddress.leadaddressid = vtiger_new.vtiger_leadscf.leadid
JOIN vtiger_new.vtiger_crmentity ON vtiger_new.vtiger_crmentity.crmid = vtiger_new.vtiger_leadscf.leadid
JOIN vtiger_new.vtiger_users ON vtiger_new.vtiger_users.id = vtiger_new.vtiger_crmentity.smcreatorid
JOIN vtiger_new.vtiger_leadscf AS vlead ON vtigerdb.vtiger_leadscf.leadid = vlead.leadid
SET vtigerdb.vtiger_leadscf.cf_953 = CONCAT(vtiger_new.vtiger_users.first_name,' ',vtiger_new.vtiger_users.last_name)
WHERE vtigerdb.vtiger_leadscf.leadid = vlead.leadid
AND cf_953 = ""

Related

Not unique table/alias on join

I intend to do an update based on join, but getting an error. What is missing?
update
vna.patients,
vna.patient_observations,
vna.studies,
vna.series,
vna.instances,
vna.sop_classes,
vna.files,
vna.modalities,
vna.issuers
join patient_observations on atients.patient_id=patient_observations.patient_id
join studies
on patient_observations.study_id=studies.study_id
AND studies.patient_id=patients.patient_id
join series
on series.study_id=studies.study_id
join instances
on instances.series_id=series.series_id
join sop_classes
on sop_classes.sop_class_id=instances.sop_class_id
join files
on files.instance_id=instances.instance_id
left join modalities
on modalities.modality_id=series.modality_id
left join issuers
on (patients.issuer_of_patient_identifier=issuers.issuer_id)
set PATIENT_NAME='AAPM'
WHERE PATIENT_IDENTIFIER='TG18-2002';
ERROR 1066 (42000): Not unique table/alias: 'patient_observations'
You don't need to specify table names again which appears in join part
UPDATE
vna.patients
JOIN patient_observations
ON patients.patient_id = patient_observations.patient_id
JOIN studies
ON patient_observations.study_id = studies.study_id
AND studies.patient_id = patients.patient_id
JOIN series
ON series.study_id = studies.study_id
JOIN instances
ON instances.series_id = series.series_id
JOIN sop_classes
ON sop_classes.sop_class_id = instances.sop_class_id
JOIN files
ON files.instance_id = instances.instance_id
LEFT JOIN modalities
ON modalities.modality_id = series.modality_id
LEFT JOIN issuers
ON (patients.issuer_of_patient_identifier = issuers.issuer_id)
SET PATIENT_NAME = 'AAPM'
WHERE PATIENT_IDENTIFIER = 'TG18-2002' ;
Why are you mixing the two different JOIN syntaxes? Simple rule: Never use commas in the FROM clause (and that goes for UPDATE as well). I think you intend:
update vna.patients p
patient_observations po
on p.patient_id = po.patient_id join
studies st
on po.study_id = st.study_id AND
st.patient_id = p.patient_id join
series s
on s.study_id = st.study_id join
instances i
on i.series_id = s.series_id join
sop_classes sc
on sc.sop_class_id = i.sop_class_id join
files f
on f.instance_id = i.instance_id left join
modalities m
on m.modality_id = s.modality_id left join
issuers iss
on (p.issuer_of_patient_identifier = iss.issuer_id)
set p.PATIENT_NAME = 'AAPM'
where p.PATIENT_IDENTIFIER = 'TG18-2002';
That seems way too complicated. I'm guessing you just want:
update vna.patients p
set p.PATIENT_NAME = 'AAPM'
where p.PATIENT_IDENTIFIER = 'TG18-2002';
The error message is very clear, the table patient_observations is listed twice in the table references in your query. Give the second one a different alias if you really need to join it again in the same query:
...
vna.issuers
join patient_observations as po2 on patients. ....
Otherwise, remove one of them.
Also try to use the ANSI SQL join syntax instead of this old syntax.

Left join is not working on codeigniter

I am new in code igniter. I am trying to execute delete query using joining multiple tables, but I am getting error in query.
Here is my query code.
$this->db->from('order');
$this->db->join('item_order', 'item_order.order_id = order.order_id','left');
$this->db->join('product', 'product.product_number = item_order.item_number','left');
$this->db->join('product_to_image', 'product_to_image.p_id = product.products_id','left');
$this->db->join('product_to_dropbox', 'product_to_dropbox.products_id = product.products_id','left');
$this->db->where('order.user_name', $ebay_user_name);
$this->db->where('order.user_id', $user_id);
$this->db->delete('order');
When using Codeigniter you can not perform a DELETE with Join. see here and here.
You need to write a natural SQL. See link 1:
$sql = "DELETE o FROM order as o
LEFT JOIN item_order ON item_order.order_id = order.order_id
LEFT JOIN product ON product.product_number = item_order.item_number
LEFT JOIN product_to_image ON product_to_image.p_id = product.products_id
LEFT JOIN product_to_dropbox ON product_to_dropbox.products_id = product.products_id
WHERE order.user_name = ? and order.user_id = ?";
$this->db->query($sql, array($ebay_user_name, $user_id));
OR try with backticks `. But I'm not sure whether or not escaping them:
$sql = "DELETE o FROM `order` as o
LEFT JOIN `item_order` ON item_order.order_id = order.order_id
LEFT JOIN `product` ON product.product_number = item_order.item_number
LEFT JOIN `product_to_image` ON product_to_image.p_id = product.products_id
LEFT JOIN `product_to_dropbox` ON product_to_dropbox.products_id = product.products_id
WHERE order.user_name = ? and order.user_id = ?";
$this->db->query($sql, array($ebay_user_name, $user_id));

MySQL can’t specify target table for update in FROM multiple table joins

I have searched for days on how to get around this error while trying to update a field from a multiple join table, with a minimum date from the same mutiple join tableset.
This is my Update statement:
update vtiger_projectmilestone
Inner Join vtiger_projectmilestonecf ON vtiger_projectmilestone.projectmilestoneid = vtiger_projectmilestonecf.projectmilestoneid
Inner Join vtiger_crmentity ON vtiger_projectmilestone.projectmilestoneid = vtcrmm.crmid
inner join vtiger_project on vtiger_project.projectid = vtiger_projectmilestone.projectid
Inner Join vtiger_crmentity vtcrmp ON vtcrmp.crmid = vtiger_project.projectid
set vtiger_projectmilestone.projectmilestonedate =
(select min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
)
where vtiger_projectmilestone.projectid =
(select vtiger_project.projectid from vtiger_project
INNER JOIN vtiger_crmentity vtcrmp ON vtiger_project.projectid = vtcrmp.crmid
where vtcrmp.deleted = 0 order by vtiger_project.projectid desc limit 1)
and vtcrmp.deleted = 0
and vtcrmm.deleted = 0
and (vtiger_projectmilestone.projectmilestonedate is null or vtiger_projectmilestonecf.cf_763 is null) ;
This is a real life update query, not just a simple one table relationship.
I got round it by creating a temp table, inserting the value, updating the destination table and dropping the temp table.
I would really like to get this right, because it will come up more often.
All assistance is appreciated.
Cheers
Bernard Bailey
As stated in this answer you can't use the target update table in a subquery, as you can see in your query
SELECT min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join
--using target update table in query
vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
However i think that a work around is using the data from the table that you're updating, so instead of using joins, you could write some where conditions for example:
SELECT min(vtiger_projecttaskcf.cf_779)
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid = tvptcf.projecttaskid
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_crmentity AS vtcrmm ON tvpt.projecttasknumber = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
--using the projectmilestoneid in a where clause
AND tvpt.projecttasknumber=vtiger_projectmilestone.projectmilestoneid
The caveat could be that probably you will get some performance issues, also, as I don't know the full schema, I can't tell if using other tables in the subquery instead of vtiger_projectmilestone will give you the right result

mySQL how do you do a chain of join?

Hi! I am trying to figure out how to do a join in multiple steps in the same line of code. This is what i got so far, but I get an error 1064.
select * from jbcity
join jbsupplier on jbsupplier.city = jbcity.name where jbcity.state = "Mass"
join jbsupply on jbsupply.supplier = jbsupplier.id
join jbparts on jbparts.id = jbsupply.part;
Try this:
select * from jbcity
join jbsupplier on jbsupplier.city = jbcity.name
join jbsupply on jbsupply.supplier = jbsupplier.id
join jbparts on jbparts.id = jbsupply.part where jbcity.state = 'Mass';

Error with my UPDATE mysql query, with a lot of join

When I try the following query :
UPDATE cache_implementation
SET parent_through_compared_id = ncp.nid, parent_through_feature_id = nfp.nid
FROM cache_implementation n
INNER JOIN cache_compare nc ON n.compared_id = nc.nid
INNER JOIN cache_implementation ncp ON (nc.nid = ncp.compared_id AND n.feature_id = ncp.feature_id)
INNER JOIN cache_feature nf ON n.feature_id = nf.nid
INNER JOIN cache_implementation nfp ON (nf.nid = nfp.feature_id AND n.compared_id = nfp.compared_id)
I have the following error :
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 'FROM cache_implementation n INNER JOIN cache_compare nc ON n.compared_id = nc' at line 3
Through this query, I try to update two fields with value located in some other table, by making a mass update query.
You are using used in TSQL. Here's for MySQL.
UPDATE cache_implementation n
INNER JOIN cache_compare nc
ON n.compared_id = nc.nid
INNER JOIN cache_implementation ncp
ON (nc.nid = ncp.compared_id AND n.feature_id = ncp.feature_id)
INNER JOIN cache_feature nf
ON n.feature_id = nf.nid
INNER JOIN cache_implementation nfp
ON (nf.nid = nfp.feature_id AND n.compared_id = nfp.compared_id)
SET parent_through_compared_id = ncp.nid, parent_through_feature_id = nfp.nid
In MySQL multi-table update statement, the SET clause follows the table references. (This differs from the syntax used in other databases.)
To fix your statement, delete that first line, move the line with SET to the bottom, qualify the column references with the table alias, and change FROM to UPDATE. Voila.
UPDATEcache_implementation
FROM cache_implementation n
INNER JOIN ...
INNER JOIN ...
SET n.col = expr, n.col2 = expr
Multi-table update syntax documented here: http://dev.mysql.com/doc/refman/5.5/en/update.html