Mysql database has a tableA which has a many columns . one of the columns is SIM1.
Another table is tableB which has many columns . one of the columns is SIM2
the requirement is to join all columns of tableA and tableB given that SIM1 = SIM2.
LIKE THIS
tableA
col1 col2 SIM1 ..........col24
a x 1 5
b y 1 3
c z 0 2
d g 2 1
tableB
colA colB SIM2
x g 1
y f 0
x s 0
y e 2
The result of Select query should be
col1 col2 SIM1............col24 colA colB SIM2
a x 1 ........... 5 x g 1
c z 0 ......... . 2 x s 0
d g 2 .......... 1 y e 2
is it possible?
select * from tableA inner join tableB on tableA.SIM1 = tableB.SIM2
Related
Effectively, I would like to update values in a subset of an SQL table using values from an alternate subset of that same table. Take the following dataframe representation as an example:
Company Employee Type Value
0 X A Category A 10
1 X A Category B 5
2 X A Category C 4
3 X A Category D 0
4 X A Category E 0
5 X A Category F 0
6 X A Category G 9
7 X B Category A 15
8 X B Category B 8
9 X B Category C 6
10 X B Category D 0
11 X B Category E 0
12 X B Category F 0
13 X B Category G 7
For both Employee A and Employee B, I would like to update Value where Type is in the set (Category D, Category E, Category F) with the corresponding value in Value where Type is in the set (Category A, Category B, Category C). Thus my desired outcome would be as follows:
Company Employee Type Value
0 X A Category A 10
1 X A Category B 5
2 X A Category C 4
3 X A Category D 10
4 X A Category E 5
5 X A Category F 4
6 X A Category G 9
7 X B Category A 15
8 X B Category B 8
9 X B Category C 6
10 X B Category D 15
11 X B Category E 8
12 X B Category F 6
13 X B Category G 7
What would be the most effective way of doing this in MySQL? My attempt was the following:
UPDATE new_table
SET
Value = (SELECT
Value
FROM
(SELECT
*
FROM
new_table) t
WHERE
Type IN ('Category A', 'Category B', 'Category C'))
WHERE
Type IN ('Category D', 'Category E', 'Category F');
*My real column names are different than the above, as I assume these are reserved names
Update
Based on OP comments, there needs to be a Type translation table which indicates where the new value should come from for a given category:
UPDATE new_table t1
JOIN (SELECT 'C1' AS Cat1, 'C4' AS Cat2
UNION ALL
SELECT 'C2' AS Cat1, 'C5' AS Cat2
UNION ALL
SELECT 'C3' AS Cat1, 'C6' AS Cat2
) cats ON cats.Cat2 = t1.Type
JOIN new_table t2 ON t2.Employee = t1.Employee AND t2.Type = cats.Cat1
SET t1.Value= t2.Value
Demo on SQLFiddle
Original Answer
Since you have a direct relationship between the Type values, you can take advantage of that in your update query by JOINing new_table to itself where the difference in Type is 3 and updating Value from the JOINed table:
UPDATE new_table t1
JOIN new_table t2 ON t2.Employee = t1.Employee AND t2.Type = t1.Type - 3
SET t1.Value= t2.Value
WHERE t1.Type in (4, 5, 6);
Demo on SQLFiddle
I have a row that should be duplicated in certain conditions.
Example:
Case 1:
a b c d e
---------------
1 4 25 10 NULL
if e is Null, display a, b and c:
1 4 25
Case 2:
a b c d e
---------------
1 4 25 10 55
if e is not Null, duplicate the row
1 4 25 => column a,b,c
1 4 10 => column a,b,d
Use this query
Using union you can achieve this use case
select a,b,c from table
union all
select a,b,d from table where e is not null
If you don't want to scan the table twice, then you can use cross joinand some logic:
select a, b,
(case when n = 1 then c else d end)
from t cross join
(select 1 as n union all select 2) n
where n = 1 or
(n = 2 and e is not null);
I want to output two rows in a one entry but with two tables. here is my table:
table_A
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled
-----------------------------------------------------------------------
1 1 Y Y Y
2 2 Y Y N
3 10 N Y Y
table_B
-----------------------------------------------------------------------
checkkey checknum status
-----------------------------------------------------------------------
1 1 V
2 2 V
3 10 V
I want an output like this
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled status
-----------------------------------------------------------------------
1 1 Y Y Y
1 1 Y Y Y V
2 2 Y Y N
2 2 Y Y N V
3 10 N Y Y
3 10 N Y Y V
you can use a UNION , first query will get all with status as V based on tableB and second one gets all rows from A
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, b.status
from table_A a
inner join table_B b
on a.checkkey = b.checkkey
union all
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, NULL as status
from table_A a
order by checkkey, checknum;
I think this does what you want:
select a.*, 'V' as status
from table_A a
union all
select a.*, NULL as status
from table_A a
order by checkkey, checknum;
Table_B doesn't seem necessary at all.
I have two tables A and B with values below
Table A
X
------
1
2
3
Table B
X Y
------ ------
1 A
2 A
3 A
1 B
2 B
1 C
3 D
I need to find only the Y values from Table B which match All of the values in Table A.
So for the above example the only Y value that matches is A (A has an X value of 1,2,and 3)
This is an example of a "set-within-sets" subquery. I like to approach this with aggregation and a having clause.
select b.y
from tableB b join
tableA a
on b.X = a.X
group by b.y
having count(distinct b.x) = (select count(*) from tableA);
So I have two tables that I need to aggregate data by.
The first looks like this:
zip code | key
x 1
x 2
x 3
y 4
y 5
The second looks like this:
characteristics | key
a 1
b 1
c 1
d 2
e 2
f 3
g 4
and I need to join them to look like this...
zip code | key | characteristics
x 1 a
x 1 b
x 1 c
x 2 d
x 2 e
x 3 f
y 4 g
... ... ...
I can't quite think of what the correct subqueries / joins would be to make this happen. Any help is really appreciated.
Try this
select table1.zipCode, table1.key, table2.characteristics
from table1
inner join table2 on table1.key = table2.key
Ok...
Then try this.
select t1.zipcode, t1.keys, t2.character
from table_1 t1
full outer join Table_2 t2 on t1.keys = t2.keys