How to insert null value when doing left join in SQL - mysql

I have these 2 table (each element is in column):
TABLE A:
ID 1 2 3 4 5
TD 20 10 0 1 7
TABLE B:
ID 2 3
TD 40 30
I want to do a LEFT JOIN to obtain a FINAL_TABLE in which I have all the ID od TABLE A and the one of table B in common with A. I want also that in case there is ID in TABLE A and not in TABLE B (ex. ID 1,4,5), in the FINAL_TABLE I will show NULL at that ID.
The FINAL_TABLE should be (see all in column):
ID 1 2 3 4 5
TDA 20 10 0 1 7
TDB NULL 40 30 NULL NULL
How can I achieve that?

Not quite sure if this is what you ask for, but perhaps you just want to add one result to another?
select 'TDA', col1, col2, col3, col4, col5 from tableA
UNION ALL
select 'TDB', null, col1, col2, null, null from tableB

I think this should work. Could you please try this once.
INSERT INTO FINAL_TABLE
(
ID,
tdA,
tdB
)
SELECT
A.ID,
A.td,
B.td
FROM
TABLEA
LEFT OUTER JOIN TABLEB ON (A.ID=B.ID)
GROUP BY
A.ID,
A.td,
B.td
;

Related

How to make a template table in SQL that pulls specific columns from other different tables

I have multiple tables with multiple columns, but for this question say I just have three tables with one column each:
table1:
id
A
B
1
20
14
2
11
table2:
id
C
D
E
100
14
4
101
16
12
19
table3:
id
F
1234
6
8765
11
Desired output:
mainTable:
id
tableName
columnName
value
1
table1
A
20
1
table1
B
14
2
table1
B
11
100
table2
C
14
101
table2
C
16
101
table2
D
12
101
table2
E
19
8765
table3
F
11
As you could notice, I'd like the query to also have a condition where it only chooses to insert into the main table if the column value is greater than 10.
Let me know if I can add any further information to the question.
you can use union and a CTE for clarity to insert your rows with a single insert
with t as (
select id, 'table1' Tablename, 'col1' ColumnName, col1 as Value
from table1
union all
select id, 'table2', 'col2', col2
from table2
union all
select id, 'table3', 'col3', col3
from table3
)
insert into mainTable (id, tableName, columnName, Value)
select id, tableName, columnName, Value
from t
where value > 10
You want to create a so-called key/value table. They are generally a nuisance to work with, but you will have your reasons. The query to get from a normal table to a key/value table is to select value by value and union the results.
select id, 'table1' as table_name, 'A' as column_name, a as value from table1 where a > 10
union all
select id, 'table1' as table_name, 'B' as column_name, b as value from table1 where b > 10
union all
select id, 'table2' as table_name, 'C' as column_name, c as value from table2 where c > 10
union all
select id, 'table2' as table_name, 'D' as column_name, d as value from table2 where d > 10
union all
...

Deleting rows in a table with no primary key constraint

There are two tables A and B with same structure (number of columns, column names etc.). There is no primary key constraint for both A and B. Some of the columns values can be null but not mentioned as a constraint.
Creation of table looks like below
CREATE TABLE IF NOT EXISTS TableA
( col1 INT,
col2 VARCHAR(50)
col3 BIGINT )
I need to delete rows in A which are in B i.e A = A - B
There are around 100 columns in the original table (I have simplified it above). So listing all the columns is not desirable.
How do I do this task?
I had to add rows from another table C which I did by using INSERT INTO.
INSERT INTO tableA VALUES
(
SELECT * From tableC
EXCEPT
SELECT * from tableA
)
Use Left Join Of Table A with Table B and select all columns in
"ON" condition while joining then Select those rows or records which have null values in Table B's any column.
For Example:
TABLE A:
id name data
1 DAN 123
2 ANTONY 234
3 DAN 456
4 DAN 856
5 JOSEPH 546
6 ANTONY 784
7 JOSEPH 896
TABLE B:
id name data
1 DAN 123
5 JOSEPH 546
7 JOSEPH 896
QUERY for the above problem is:
SELECT A.id,A.name,A.data
FROM A
LEFT JOIN B
ON A.id = B.id
AND A.name = B.name
AND A.data = b.data
WHERE B.id IS NULL;
Result is:
id name data
2 ANTONY 234
3 DAN 456
4 DAN 856
6 ANTONY 784
You can also check this on below link:
http://sqlfiddle.com/#!9/d9e34b/4
delete from A where (column1, column2) not in
((
select column1, column2from A
minus
select column1, column2 from B
))
include all the columns from the table in select statement. As you have mentioned both table has same number of columns and names this query will work for you
You can delete data from A using inner join between A and B table,
Delete A
From A
Inner Join B
On A.id = B.Id
And A.name = B.name
And A.Data = b.data;
Try following Demo
Demo

Join two tables by row

I have two tables with same column I need to merge that two table like below
Table1
id name
1 test1
4 test7
5 test9
6 test3
Table2
id name
2 test2
3 test5
6 test3
Result
id name
1 test1
2 test2
3 test5
4 test7
5 test9
6 test3
So I need to join/merge the two tables by id and you can see id 6 present in both table I need to override table 2 value and give above result. Kindly help me to solve the issue.
Thank you.
select id,name from table1
union
select id,name from table2 ;
other way
select * from (
select id,name from table1
union
select id,name from table2)temp order by temp.id ;
This will arrange records id wise
UNION will eliminate duplicate record , In your case it's id 6
When you want sorting then must be to create inner query like this
select * from
(
select id,name from table1 t1
union
select id,name from table2 t2
)a order by a.id ;

How to get column value and average in the same row in mysql?

I have a table tbl structure having
col1 col2 col3
1 10 3
2 20 3
3 30 4
4 40 4
Need to get output as
col2 col3 Avg
10 3 15
20 3 15
30 4 35
40 4 35
So basically I need average on col3.
I tried
select col2,col3,avg(col2) from tbl1 group by col3
But this would only give us the first row in each matching group.
How to get this done in mysql ?
select tbl1.col2, tbl1.col3, tbl2.avgcol2
from tbl1
inner join
(
select col3,avg(col2) as avgcol2
from tbl1
group by col3
) tbl2 on tbl1.col3 = tbl2.col3
You can use a subquery on your SELECT:
select t1.col2,
t1.col3,
(select avg(t2.col2) from tab1 t2 where t2.col3 = t1.col3) AS AVERAGE
FROM tab1 t1
sqlfiddle demo

left join not working - get records not in other table

I feel weird, as every other answer says it works, but I can't get the correct results :(
table A: id_num, name<br>
table B: id_num
table A has index on name, but not unique. id_num is unique in this table.<br>
table B has index on id_num, but not unique.
I want to get table A names, who are NOT in table B.
This not working:
**SELECT a.name FROM a
LEFT JOIN b ON (a.id_num = b.id_num)
WHERE b.id_numb IS NULL**
its returning names that ARE in table b (and some that are not).
this didn't work either:
**SELECT distinct(a.name)
FROM a where a.id_num
not in (select distinct(b.id_num) from b )**
I can't comprehend how a.names are being return who ARE IN table B, when the SQL says NOT IN.
what am I missing?
thanks
By right, the left join should work. And as well as in.
Here is a sample. So you may want to publish and show your table schema and data for more justice on your question. Best if you could just create the tables on SQLFIDDLE and show the reference. ;)
Here is just SAMPLE:-
SQLFIDDLE reference
Sample Tables:
tab1:
COL1 COL2
1 john
2 tim
3 jack
4 rose
tab2:
COL3 COL4
1 2
2 3
3 5
4 5
5 2
Query:
select * from tab1
where col1 not in
(select distinct col4 from tab2)
;
Results:
COL1 COL2
1 john
4 rose
As per OP's updated comments and table structure
OP mentioned the table tab1 will have multiple records for same name. According to his original table design, there's NO NAME field in the table tab2. It's also much recommended if OP had provided the expected results initially.
* SQLFIDDLE Reference
OP's table data:
COL1 COL2
1 john
2 tim
3 jack
4 rose
5 john
6 john
COL3 COL4
1 2
2 3
3 5
4 5
5 2
6 6
Query: When you do not want any name that is duplicated
select t.col1, t.col2
from tab1 t
join
(select t1.col2, count(*) as counts
from tab1 t1
group by t1.col2
having count(*) = 1) x
on t.col2 = x.col2
where t.col1 not in
(select distinct col4 from tab2)
;
Results: Here is Rose, the only record that has no duplicates and do not exist in tab2
COL1 COL2
4 rose
try this
select t1.name from table1 t1
where t1.name not in
(select t2.name from table2 t2 )
look the DEMO SQLFIDDLE
EDIT:
if you have only id_num in your second table then here look to
THIS DEMO SQLFIDDLE