Update query with multiple innerjoins - mysql

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.

Related

MySQL inner join changes column value

I found something really confusing recently about inner join:
select records.id,
records.amount,
records.payment_amount,
orders.id as or_id
from `records`
**`inner`** `join order_from_site orders on records.id = orders.`record_id`
will somehow set the payment_amount value to 0.0.
If however, I change the inner join to left join, the value is correctly preserved. Not sure if this is the expected behavior or a bug?
I'm using MySQL 8.0.
Your question does not provide any data from the two tables so it will be hard to determine the answer to your question without the data.
That said, an inner join requires matched data from both sides to be present in order to display a row. A left join requires the table from the left to be present and only optionally on the right.

Inner join query returning empty set

I have two tables with varchar columns having common values.
I am trying to perform a simple inner join on the tables on those varchar columns but I am getting empty set as a result.
There are null values present in these columns.
Collation for both columns are same.
I googled for it but cant find a solution.
The query is a simple join query and doesn't have any syntax issues.
When I change the inner to left join I get a resultset but all the columns in the right table are empty even for the rows passes the join condition.
I created two dummy tables with a common varchar to run a join query and it works.
So it is not a bug.So what are the possible reasons for getting empty sets any guesses.
The server is mysql version 5.5.37
update a set a.id=x where a.col in(Select col from b where .....);
Another issue is that i have a query like above but when I run it It kind of hangs...On running
SHOW PROCESSLIST;
the status is sending data...
What does it mean?
Can anyone help me.
Edit 1
The query is like this
UPDATE table1 bo INNER JOIN table2 ae
ON bo.joincol=ae.joincol AND ae.criteriaFieldName='UNSPSC' AND ae.taskId=4 AND ae.batchId = 44
SET bo.taskid=ae.taskId WHERE bo.procesId = 44 AND (taskid IS NULL OR taskid = 0);
Table1 contains 50+ columns
Table2 contains 8 columns
Unfortunately I can't disclose the data.
Interesting fact is if I join them using taskId I get a resultset.
For your second issue, try doing it as a JOIN instead of using IN:-
UPDATE a
INNER JOIN b
ON a.col = b.col
WHERE ........
SET a.id=x

Update Query with 2 Inner Joins between two databases on same server

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

MYSQL SELECT: check if rowdata exists

In my SQL query i'm checking on different parameters. Nothing strange happens when there is data in each of the tables for the inserted tripcode. But when one table has no data in it I don't get any data at all. Even if the other tables have data. So I need to be able to check if the table has data in it and if it has, I need to select.
SELECT roadtrip_tblgeneral.*,
GROUP_CONCAT(distinct roadtrip_tblhotels.hotel) as hotels,
GROUP_CONCAT(distinct roadtrip_tbllocations.location) as locations,
GROUP_CONCAT(distinct roadtrip_tbltransports.transport) as transports
FROM roadtrip_tblgeneral
INNER JOIN roadtrip_tblhotels
ON roadtrip_tblgeneral.id = roadtrip_tblhotels.tripid
INNER JOIN roadtrip_tbllocations
ON roadtrip_tblgeneral.id = roadtrip_tbllocations.tripid
INNER JOIN roadtrip_tbltransports
ON roadtrip_tblgeneral.id = roadtrip_tbltransports.tripid
WHERE roadtrip_tblgeneral.tripcode = :tripcode
GROUP BY roadtrip_tblgeneral.id
Only the tables with the GROUP_CONCAT in front need the check. I already tried with the keyword EXISTS in front of it.
Thanks in advance.
The INNER JOIN keyword returns rows when there is at least one match in both tables. You can't have a match if there is no data, perhaps you want to use a LEFT JOIN or a FULL JOIN.
Left join will be use as it returns all the data from the table at left, even if there is no matching rows in right table

A basic 3 table inner join in MySQL?

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 :)