Join two tables by row - mysql

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 ;

Related

Left join with other table with where clause

I have 2 tables in MySQL database that I would like to join, where I would like to contain all results from table1.
My tables looks like this:
table1
id
name
1
name1
2
name2
3
name3
4
name4
5
name5
6
name6
7
name7
8
name8
table2
id
table1_id
myfield
1
3
test1
2
2
test2
3
1
test1
4
4
test2
5
5
null
6
2
null
What I am trying to achieve is to get a table which contains all the rows from table1 and only the data that is joined from table2.
This is my query:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
where myfield="test1"
group by t1.id
But this is not what I want to achieve.
What I want to achieve is to get all records from table1 and to have all related records from table2 where table2.myfield="test1". And for the other for table2.mytable to have null (if they do not fulfil table2.myfield="test1").
Any help is much appreciated!
Thanks!
move the where clause to the on clause:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
and myfield="test1"
group by t1.id
BTW: some DBMS does not allow select * with group by. So select the id and some aggregated values or remove group by id

How to query update in sql based on data from other table

I Have 2 Tables.
As Example:
table1
ID, data_static1, name1
1 8 Muna
2 1 Andi
3 7 null
table2
ID, data_static2, name2
1 0 Aji
2 1 Andi
3 2 max
4 3 nadine
5 4 Rio
6 5 Panji
7 6 Eko
8 7 Pan
9 8 Muna
I want to update the column name1 in table1 based on the largest ID in table1 where table1.data_static1 is the same as table2.data_static2.
I want the results as below
table1
ID, data_static1, name
1 8 Muna
2 1 Andi
3 7 Pan
I've tried the following code
mysql> UPDATE theDB.table1 SET name1=(SELECT name2 FROM table2 WHERE data_static2=(SELECT data_static1 From table1 WHERE ID IN(SELECT MAX(ID) FROM table1))) WHERE table1.ID IN(SELECT MAX(table1.ID) FROM theDB.table1);
I get an error message
ERROR 1093 (HY000): You can't specify target table 'table1' for update in FROM clause
Simplest solution would be to use a correlated subquery for this:
update table1 t1
set name1 = (
select name2
from table2 t2
where t1.data_static1 = t2.data_static2
order by id desc limit 1
);
You can use JOIN too:
update table1 t1
join (
select *
from table2 t
join (
select data_static2,
max(id) as id
from table2
group by data_static2
) t2 using (data_static2, id)
) t2 on t1.data_static1 = t2.data_static2
set t1.name1 = t2.name2;

Select query except another table

I got 2 tables.
TABLE 1
ID FRANCHISENAME TELEPHONE FRANCHISE_ID
1 BURGER 666-555-999 5
2 JSUBS 666-555-999 7
3 STEAKS 777-888-999 3
TABLE 2
ID NAME TELEPHONE EMAIL FRANCHISE_ ID
5 JOHN 555-444-333 JOHN#GMAIL.COM 5
5 JOHN 555-444-333 JOHN#GMAIL.COM 7
6 EDGARD 555-444-333 EDGARD#GMAIL.COM 9
I want to retrieve all data in table one, except for that data where the user has his email in Table 2. As for example JOHN has franchise_id 5 and 7, so the query would only return
3 STEAKS, 777-888-999, 3
Assuming that TABLE_1 & TABLE_2 relate to each other through TABLE_1.FRANCHISE_ID & TABLE_2.FRANCHISE_ID
You can use NOT EXISTS
SELECT
*
FROM TABLE_1 T1
WHERE NOT EXISTS(
SELECT *
FROM TABLE_2 T2
WHERE T2.FRANCHISE_ID = T1.FRANCHISE_ID
AND T2.EMAIL = 'JOHN#GMAIL.COM'
)
OR
You can use LEFT JOIN along with IS NULL
SELECT
T1.*
FROM TABLE_1 T1
LEFT JOIN TABLE_2 T2 ON T1.FRANCHISE_ID = T2.FRANCHISE_ID
WHERE T2.FRANCHISE_ID IS NULL;
SELECT t1.*
FROM
Table1 t1
LEFT JOIN Table2 t2
ON t1.FRANCHISE_ID = t2.FRANCHISE_ID
AND LEN(IFNULL(t2.EMAIL,'')) > 0
WHERE
t2.ID IS NULL
Even if there is a record in Table 2 if it has no email it will be returned by this query. You can expand say to > 7 or more to check for a minimum level of validity of email address.
This should get you there using the NOT IN function. It will exclude records from Table 1 if there is a matching Franchise ID in Table 2, unless the email field is Table 2 is null:
SELECT * FROM Table1
WHERE Table1.Franchise_ID NOT IN
(SELECT Table2.Franchise_ID FROM Table2
WHERE Table2.Email IS NOT NULL);

How to select a column from one table and two columns from another table when both the tables have no relation in T-SQL?

I will explain my problem as simple as possible.
I have written a select query Query1 on Table1 which gives the me the following result
SELECT * FROM Table1 WHERE TypeID=1
ID Column1 Column2 TypeID
1 A A 1
2 B B 1
3 C C 1
I have another table Table2 which has data in the following format
ID Column1 Column2
1 0 0
1 1 1
2 2 2
2 3 3
2 4 4
3 5 5
3 6 6
I have written a another select query Query2 on Table1 which gives the me the following result
SELECT * FROM Table1 WHERE TypeID=2
ID Column1 Column2 TypeID
4 A A 2
5 B B 2
6 C C 2
Table1 and Table2 have no column in common and data in Column1 and Column2 for both TypeID's in Table1 is same and currently data in Table2 has data with values of ID column from Table1 for id's 1,2,3 only and i want to write a select query to select same data from Table2 but with values of ID column from Table1 with TypeID as 2 which I have given below
ID Column1 Column2
4 0 0
4 1 1
5 2 2
5 3 3
5 4 4
6 5 5
6 6 6
how can I achieve this by writing a select query in sql server?
select T1.column1, isnull(T2.column2,0) as column2,
isnull(T2.column3,0) as column3
from table1 T1
left outer join on Table2 T2 on T1.column9=T2.column9
where ....
If I understood the question correctly, the query whould be like that:
SELECT
T_ID.T2_ID,
Column1,
Column2
FROM
Table2
JOIN
(
SELECT
T1.ID AS T1_ID,
T2.ID AS T2_ID
FROM
Table1 AS T1
JOIN Table1 AS T2 ON T1.Column1 = T2.Column1 AND T1.Column2 = T2.Column2
WHERE
T1.TypeID = 1
AND T2.TypeID = 2
) AS T_ID ON Table2.ID = T_ID.T1_ID

Mysql not union

So I have 2 tables.
table 1:
ID CUST_NO
1 51555
2 51556
3 51111
4 44444
5 54878
6 13548
and table 2:
ID CUST_NO
1 51555
2 51556
3 31333
4 97948
5 65488
6 14648
. .....
I know I can use union to get the CUST_NO's that appear in both tables. However, I need to get the list of CUST_NO's that appear in table 1, but not in table 2.
So a result for this should be
51111
44444
54878
13548
I bet this is really easy but I just can't use my head right now, any thoughts?
select t1.CUST_NO
from Table1 t1
left outer join Table2 t2 on t1.CUST_NO = t2.CUST_NO
where t2.CUST_NO is null
select cust_no
from table1
where not exists
(select cust_no from table2 where table2.cust_no = table1.cust_no)
A NOT IN subquery is simplest, though probably not fastest:
SELECT
ID,
CUST_NO
FROM tab1
WHERE CUST_NO NOT IN (SELECT CUST_NO FROM tab2);