join rows of one table to column of another table in SQL - mysql

How to join rows of one table to column of another table in Mysql?
Is it possile to join rows of one table to column of another table in Mysql?

Related

MySQL select all rows in one tables matching unique values in another table

I have 2 tables. Both contain 1 column with same value. I need to select all rows of one table if they have same value in another table.
How can do it?
I think what you are looking to do is
SELECT *
FROM table_1 INNER JOIN table_2
WHERE column.table_1 = column.table_2
This will return all the values that are in both table 1 and table 2 where the value in the column in table 1 is also in table 2

How to update a sql table only if other condition exist in another table

I have 2 sql separated tables in my database:
ds_users, containing: group_id
and
ds_users_data_members, containing: data_gender
I would like to set / Update the group_id to 6 for all data_gender equal to 2.
All this morning i tried to solve this issue , without success.
Please help. Thank you very much
I am assuming there must be a relation between those two tables. Without any relationship you cannot update record in one table by checking condition in another table.
let's say ds_users table has column user_id which is also exist in ds_users_data_members table.
so, you can write following query to update all records in ds_users for data_gender=2 in ds_users_data_members table
SQL SERVER EXAMPLE
UPDATE T
SET group_id=6
FROM ds_users T INNER JOIN ds_users_data_members T1 ON T.user_id=T1.user_id
WHERE T1.data_gender=2
MySQL EXAMPLE
UPDATE ds_users T INNER JOIN ds_users_data_members T1 ON T.`user_id`=T1.`user_id`
SET T.`group_id`=6
WHERE T1.`data_gender`=2;
You can replace the column name of user_id what you have given in your table.

MySql - Delete duplicate rows, without temp Table

I have this table email_addr_bean_rel with these fieldsid, email_address_id, bean_id, bean_module, primary_address, reply_to_address, date_created, date_modified, deleted
Out of these only records of column bean_id is duplicated twice.
I have tried this, but it doesn't work
CREATE TABLE email_addr_bean_rel_V AS SELECT DISTINCT * FROM email_addr_bean_rel;
DROP TABLE email_addr_bean_rel;
RENAME TABLE email_addr_bean_rel_V TO email_addr_bean_rel;
It still contains same number of records.

Updating Values of a Column from one Table to another

I have two tables:-
Source Table: ACT_DT with columns (CUST_NAME, ACC_TYPE, CUST_STAT, SB_ACT_DT);
Target Table: ORG_DT with columns (CUST_NAME, ACC_TYPE, CUST_STAT, SB_ACT_DT);
The column SB_ACT_DT in the Target table has all null values. I need to update that column with the values of same column as in source table. The condition to be checked are:
ACC_TYPE='Billing' and CUST_STAT='Active'.
The Target table has to be updated only if the above conditions are found true.
How can I do it? Your help is appreciated.
What you are looking for is Update table using Join
UPDATE ORG_DT o
JOIN ACT_DT a
ON o.CUST_NAME=a.CUST_NAME
AND
o.ACC_TYPE=a.ACC_TYPE
AND
o.CUST_STAT=a.CUST_STAT
SET o.SB_ACT_DT = a.SB_ACT_DT
WHERE a.ACC_TYPE='Billing' AND a.CUST_STAT='Active'

Map Values from Column in Table A to Corresponding Column in Table B

Part 1:
In MySQL suppose I have Table A which has more columns than Table B. I want to transfer values from Table B to Table A where the id row in A matches the id Row in B and update the values in table A from the values in table B.
Part 2:
Table B is a superset of table A, so how does one insert ids and their corresponding values from table B into table A while also updating id's that are in table A.
Like FreshPrinceOfSO already mentioned in the comments, you won't get code for free here.
But here are at least the steps. Two possibilities. Either you split the work up in two statements, one update then one insert statement. Or you could work with
INSERT ... ON DUPLICATE KEY UPDATE ...
You would have to have an unique index on the table for this to work.
For the first solution mentioned you'd inner join the tables for the update first, that's trivial. Then for the insert you'd use a select with a left join and with is null checking for entries that are not already in the table.
Good luck...