I am trying to Update a column in a table in one database after doing two inner joins, one of which is with a table from another database on the same server. I was trying to follow this solution here:
Update Query with INNER JOIN between tables in 2 different databases on 1 server
This isn't working for me. It gives me: you have an error in your syntax near s_u
Here's my attempt (UPDATE: I removed the code sanitation. So this is the exact code I'm running now):
UPDATE s_u
SET s_u.bill_address_id=spree_billing.id
FROM spree_users AS s_u
INNER JOIN magento.customer_entity_int AS default_billing
ON default_billing.entity_id=s_u.magento_id
AND default_billing.attribute_id=14
INNER JOIN spree_addresses AS spree_billing
ON spree_billing.magento_address_id=default_billing.value;
You reference to something called spree_billing which does not appear anywhere in your statement. You need to fix it. The problem is probably here:
UPDATE s_u
SET s_u.bill_address_id=billing.id -- modify to billing, as it is what third_table's alias is called
FROM some_table AS s_u
INNER JOIN magento.another_table AS default_billing
ON default_billing.entity_id=s_u.magento_id
AND default_billing.attribute_id=14
INNER JOIN third_table AS billing
ON billing.magento_address_id=default_billing.value;
please can you try this :
UPDATE spree_users AS s_u
INNER JOIN magento.customer_entity_int AS default_billing
ON default_billing.entity_id=s_u.magento_id
AND default_billing.attribute_id=14
INNER JOIN spree_addresses AS spree_billing
ON spree_billing.magento_address_id=default_billing.value
SET s_u.bill_address_id=spree_billing.id;
I didn't tried your tables but test on my sample test
The answer in you answer is for SQL-Server. you should look this link. UPDATE multiple tables in MySQL using LEFT JOIN
updated
so, syntax error is removed but it takes long. I want to check how many records matches JOIN or join is performing efficiently. please can you post following query's output?
SELECT COUNT(*)
FROM spree_users AS s_u
INNER JOIN magento.customer_entity_int AS default_billing
ON default_billing.entity_id=s_u.magento_id
AND default_billing.attribute_id=14
INNER JOIN spree_addresses AS spree_billing
ON spree_billing.magento_address_id=default_billing.value
Related
This is working:
SELECT *
FROM ((((defect
JOIN project_testcase ON
defect.Test_Id=project_testcase.Test_Id)
JOIN testcase ON
defect.Test_Id=testcase.Test_Id)
JOIN project_pm ON
project_testcase.Project_Id=project_pm.Project_Id)
JOIN employee ON
employee.Emp_id=project_pm.Emp_id)
However, this does not work:
SELECT *
FROM ((((defect
JOIN project_testcase ON
defect.Test_Id=project_testcase.Test_Id)
JOIN testcase ON
defect.Test_Id=testcase.Test_Id)
JOIN project_pm ON
project_testcase.Project_Id=project_pm.Project_Id)
JOIN employee ON
employee.Emp_id=project_pm.Emp_id)
WHERE Project_Id LIKE '%$categ%'
As I have used JOIN tables and joined using Project_Id. Is that the error?
The first thing I would do to troubleshoot this is to paste it into an SQL formatter. This will help find syntax errors and could help you see a logic error. I would recommend freeformatter.com.
Second you can get rid of the parenthesis.
The Fix
You need to specify what table to get the Project_Id in the WHERE because it is in multiple tables, but for clarity I would always specify what table it comes from.
select
*
from
defect
join
project_testcase
on defect.Test_Id=project_testcase.Test_Id
join
testcase
on defect.Test_Id=testcase.Test_Id
join
project_pm
on project_testcase.Project_Id=project_pm.Project_Id
join
employee
on employee.Emp_id=project_pm.Emp_id
where
project_testcase.Project_Id like '%$categ%'
Project_Id in the where clause is ambiguous, you need to define it as you have done on your joins ie WHERE project_pm.Project_Id like '%$categ%'
In the where clause of the 2nd query you need to tell the rdbms exactly which Project_Id field you want to filter on, since multiple tables contain the Project_Id field, e.g. project_pm.Project_Id.
Starting by removing all those unnecessary parenthesis, formatting and aliases reveals a fairly simple query underneath.
select * --This should really be the columns you actually need instead of all of them
from defect d
join project_testcase ptc on d.Test_Id = ptc.Test_Id
join testcase t on d.Test_Id = t.Test_Id
join project_pm p on ptc.Project_Id = p.Project_Id
join employee e on e.Emp_id = p.Emp_id
where p.Project_Id like '%$categ%'
Of course this begs the question of why do you have text like that in a column named Project_Id? That does not look like a project id to me. And since you are using a leading wildcard you have a nonSARGable query so you have eliminated the ability of index seeks on that column.
I'm trying to update table with multiple inner joins however mysql is giving an error with no description
UPDATE videos v1
INNER JOIN parent_channel
ON (parent_channel.video_id=v1.video_id AND parent_channel.parent_type=“1”)
INNER JOIN parent_name
ON parent_name.item_id= parent_channel.parent_name_id
SET v1.video_name=parent_name.item_name
you're not using real quotes in parent_channel.parent_type=“1”
change that to parent_channel.parent_type="1" or even parent_channel.parent_type=1 if it's an integer.
I am trying to update a column with a calculated column in a inner join.
The logic is simple but I am struggling with the syntax
(this is just a dummy SQL, to explain what I am trying to accomplish - it does not run)
UPDATE t1
SET t1.BodyText = t2.final
from Questions as t1
INNER JOIN translations as t2
on t2.QuestionId=t1.QuestionID
CONCAT(t1.BodyText,t2.QuestionBodyText) as final
The task is simple, concat a question with its translation. I found some questions related to this issue on stackoverflow, but they where no help, maybe because they were discussing SQL Server.
Similar:
Update a table using JOIN in SQL Server?
I tried that:
UPDATE Questions t1
JOIN translations t2
on t1.QuestionID=t2.QuestionId
SET t1.BodyText = CONCAT(t1.BodyText,t2.QuestionBodyText)
But it does not have any effect on the database.
This is an equivalent SELECT that works:
SELECT CONCAT(t1.BodyText,t2.QuestionBodyText) FROM Questions t1
JOIN translations t2
on t1.QuestionID=t2.QuestionId
Update, when I used this update query on phpmyadmin it worked, on workbench it did not..
Ah, your syntax is out of whack.
Update Questions t1
join translations t2
on t2.QuestionID = t1.QuestionID
set t1.BodyText = concat(t1.bodytext,t2.questionbodytext)
I tried looking for a question such as this on SO but a lot of them were outer joins with more complicated select clauses whereas my question is more simple and should be a faster reference for newbies at MySQL.
Being used to Oracle SQL, I was trying to join three tables and perform a delete like so:
DELETE * FROM tbl_login, tbl_profile, images
WHERE tbl_login.loginid = tbl_profile.loginid
AND tbl_profile.loginid = images.loginid
AND loginid = 'derek';
In MySQL my attempt is:
DELETE FROM tbl_profile, images, tbl_login
USING tbl_profile INNER JOIN images INNER JOIN tbl_login
WHERE tbl_profile.loginid = images.loginid
AND images.loginid = tbl_login.loginid
AND loginid='derek';
I ran this in the SQL section of PHPMyadmin and it told me that loginid was ambiguous which I thought was funny because if I'm joining the three tables why would it be ambiguous? So I edited it and made it
tbl_login.loginid = 'derek'
that deleted the correct row from the tbl_login table but it ended up deleting all the rows from my other tables. What am I doing wrong here?
Remove tbl_profile and images from your FROM clause.
I think your query should look something like this (note the different way the join conditions are defined):
DELETE FROM tbl_profile, images, tbl_login
USING tbl_profile INNER JOIN images ON images.loginid = tbl_profile.loginid
INNER JOIN tbl_login ON tbl_login.loginid = tbl_profile.loginid
WHERE tbl_login.loginid='derek';
I assume you want to delete the rows from all 3 tables. If you only want to delete from tbl_login, the previous answer tells you how to do it :)
I have database with schema on picture below and I need to select everything related to one row (one id) of [letaky]. That means the related [zamestnanci], every related [obsah] and every [knihy] in it.
This is the first time i used relations in database and i have no idea how to make such a select.
Use JOIN ... ON:
SELECT *
FROM zamestnanci
JOIN lekaty ON lekaty.zamestnanciid = zamestnanci.id
JOIN obsah ON obsah.idletaku = lekaty.id
JOIN knihy ON knihy.id = obsah.idknihy
WHERE letaky.id = 123
You may also want to consider whether you need INNER JOIN, LEFT JOIN or RIGHT JOIN for each of these joins. The difference between these JOINs is described in many other questions on StackOverflow, for example this one:
SQL Join Differences