Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql join two table

TABLE A >>
uid name
1   test1
2   test2
3   test3
4   test4

TABLE B >>
uid address
1   address1
2   address2
4   address3

RESULT
1   test1   address1
2   test2   address2
3   test3
4   test4   address3

Can anyone show me how to write a query and fetch the result as above, Thanks very much! i have tried join, left and right join. all result nothing.

like image 861
Bill Avatar asked Feb 07 '12 06:02

Bill


People also ask

How can I join two tables in SQL?

The join is done by the JOIN operator. In the FROM clause, the name of the first table ( product ) is followed by a JOIN keyword then by the name of the second table ( category ). This is then followed by the keyword ON and by the condition for joining the rows from the different tables.

Can we full join 2 tables in SQL?

As you can see, joining three tables in SQL isn't as hard as it sounds. In fact, you can join as many tables as you like – the idea behind it is the same as joining only two tables. It's very helpful to take a look at the data midstep and imagine that the tables you've already joined are one table.

How do I join two tables in two SQL columns?

If you'd like to get data stored in tables joined by a compound key that's a primary key in one table and a foreign key in another table, simply use a join condition on multiple columns. In one joined table (in our example, enrollment ), we have a primary key built from two columns ( student_id and course_code ).

How do I join two tables in SQL with all columns?

Multiple tables can be merged by columns in SQL using joins. Joins merge two tables based on the specified columns (generally, the primary key of one table and a foreign key of the other). Below is the generic syntax of SQL joins. USING (id);


5 Answers

You can write left outer join between this two tables Best way to understand is check the below image

Query for your requirement

SELECT A.uid, A.name, B.address FROM A LEFT JOIN B ON A.uid=B.uid 

Reading this original article on The Code Project will help you a lot: Visual Representation of SQL Joins.

alt text

Find original one at: Difference between JOIN and OUTER JOIN in MySQL.

like image 184
Pranay Rana Avatar answered Sep 30 '22 10:09

Pranay Rana


SELECT A.uid, A.name, B.address FROM A LEFT OUTER JOIN B ON A.uid = B.uid
like image 22
Kai G Avatar answered Sep 30 '22 11:09

Kai G


You say you tried a left join but didn't give any attempts --- one of the first logical attempts would have been:

SELECT A.uid, A.name, B.address
FROM A
LEFT JOIN B ON A.uid=B.uid

Hey presto! it gives you what you were after.

like image 35
mathematical.coffee Avatar answered Sep 30 '22 09:09

mathematical.coffee


You can use any join.I write this query for full join.

select A.uid,A.name,B.address from A FULL JOIN B ON A.uid = B.uid
like image 38
Suresh Avatar answered Sep 30 '22 10:09

Suresh


I guess you're after an empty value if there is no value for B, that is having the same uid in A.

If this is the case, IFNULL will return the default value you specified in case the parameter is null (ISNULL is used in MSSQL):

SELECT A.value, IFNULL(B.value, '')
FROM A LEFT JOIN B
ON A.uid = B.uid

This will produce something like:

test1   address1
test2   address2
test3   
test4   address3
like image 22
Pavel Donchev Avatar answered Sep 30 '22 09:09

Pavel Donchev