tags:

views:

42

answers:

3

what is the syntax if I want to utilize 2 or more tables for my mysql query. Example, I'm going to fetch the idnumber from the 1st table and the religion on the 2nd table. And the query will return the combined version of those 2 tables showing only the religion and idnumber.

The code might look something like this , but it doesn't work:

select t1.IDNO, t1.LNAME t2.RELIGION  from t1, t2 where t2.IDNO='03A57'
+1  A: 
SELECT t1.IDNO, t1.LNAME FROM t1 LEFT JOIN t2.RELIGION ON ( t2.IDNO = t1.IDNO )

(more or less)

The Join is the command that will link the two.

http://dev.mysql.com/doc/refman/5.0/en/join.html

peachykeen
It is worth noting that the join command is really only used when the tables have related information (ie, share something like a postID) and generally shouldn't be used when the two tables are unrelated.For more reading: http://www.w3schools.com/Sql/sql_join.asp
Ryan
+1  A: 

The SQL query would be as follows:

SELECT a.idnumber, b.religion FROM table1 a, table2 b

You can add conditions from both tables as well by doing the following:

SELECT a.idnumber, b.religion FROM table1 a, table2 b WHERE b.religion = 'Christian'

More information can be found in this thread: http://www.astahost.com/info.php/mysql-multiple-tables_t12815.html

Ryan
A: 

The code below would do a cross-join.

SELECT tb1.id, tb2.religion FROM tb1 JOIN tb2 ON (tb1.religion_id = tb2.religion_id) WHERE t2.IDNO='03A57'

Again... see http://dev.mysql.com/doc/refman/5.0/en/join.html...

Simon