开发者

How to get data from 2 mysql tables

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'


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


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


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜