sql joins

Post Reply
User avatar
Escape_Artist
Posts: 639
Joined: Sun Apr 10, 2011 9:53 pm

sql joins

Post by Escape_Artist » Fri Sep 16, 2011 12:34 am

teach me all joins.
Me gusta Capitalismo Anarquista!

User avatar
Pigeon
Posts: 18059
Joined: Thu Mar 31, 2011 3:00 pm

Re: sql joins

Post by Pigeon » Fri Sep 16, 2011 12:40 am

You don't know anything about joins?

User avatar
Escape_Artist
Posts: 639
Joined: Sun Apr 10, 2011 9:53 pm

Re: sql joins

Post by Escape_Artist » Fri Sep 16, 2011 12:52 am

I'm brain dead after today.....


Inner joins = only those that match
Outer = all and nulls for the non match
left = everything from the first table in the join and matches/null
full outer = non matching

.........ugh my brain hurts to much at the moment to explain what I need... .. 1 sec

I want to combine both tables primary keys into one distinct list


Distinct_Primary_Key_All (from table 1 & 2), Table_1_variable, Table_2_variable

I think I just worked myself retarded... it seems simple since i typed it out.... i suck at sql.


I'm a lefty cause I always had one table with all the primary keys.
Me gusta Capitalismo Anarquista!

User avatar
Pigeon
Posts: 18059
Joined: Thu Mar 31, 2011 3:00 pm

Re: sql joins

Post by Pigeon » Fri Sep 16, 2011 1:06 am

union maybe

User avatar
Pigeon
Posts: 18059
Joined: Thu Mar 31, 2011 3:00 pm

Re: sql joins

Post by Pigeon » Fri Sep 16, 2011 1:56 am

select id from a
union
select id from b

would give the ids. if you add the extra column from each table, I believe they are distinct based on the combination of id and other colunm. That might not be what you want.

User avatar
Dr Exile
Posts: 2349
Joined: Tue Apr 05, 2011 5:37 pm
Location: Skellig Michael

Re: sql joins

Post by Dr Exile » Fri Sep 16, 2011 2:17 pm

Why don't you guys just use Fortran and simplify your live's?
Credo quia absurdum.

User avatar
Royal
Posts: 10565
Joined: Mon Apr 11, 2011 5:55 pm

Re: sql joins

Post by Royal » Fri Sep 16, 2011 2:20 pm

Dr Exile wrote:Why don't you guys just use Fortran and simplify your live's?
ahahaha

I took a class in that. Taught by mathematician that solved some 100+ year old math problem.

User avatar
Pigeon
Posts: 18059
Joined: Thu Mar 31, 2011 3:00 pm

Re: sql joins

Post by Pigeon » Fri Sep 16, 2011 3:19 pm

SELECT a.id, a.f1, b.f1
FROM a
RIGHT OUTER JOIN b
ON a.id=b.id

Only if the ID exists in both tables

Code: Select all

mysql> select * from a;
+------+------+
| id   | f1   |
+------+------+
|    1 | a    |
|    2 | aa   |
+------+------+

mysql> select * from b;
+------+------+
| id   | f1   |
+------+------+
|    1 | b    |
|    2 | bb   |
+------+------+

+------+------+------+
| id   | f1     | f1   |
+------+------+------+
|    1 | a    | b    |
|    2 | aa   | bb   |
+------+------+------+

Add an id only to b

+------+------+
| id   | f1   |
+------+------+
|    1 | b    |
|    2 | bb   |
|    3 | c    |
+------+------+

and this happens

+------+------+------+
| id   | f1   | f1   |
+------+------+------+
|    1 | a    | b    |
|    2 | aa   | bb   |
| NULL | NULL | c    |
+------+------+------+
Guess the last case will require a program to build a table that uses just a union, reads the result and merges the fields by id.

Post Reply