我有一个关于从MySQL数据库的2个表中加入数据的问题。首先,我会解释我目前有什么,然后我想要尽可能清楚。从2个MySQL表中加入数据
我在数据库中有2个表看起来是这样的:
Table: Subscriptions
Columns:
ID int(11) PK AI
Klant ID int(11)
Mail ID int(11)
Status varchar(15)
Datum varchar(15)
ID Klant_ID Mail_ID Status Datum
123 6 6 90 21-03-2013
124 6 6 10 21-03-2013
125 6 5 90 21-03-2013
126 6 5 10 21-03-2013
127 6 1 90 20-03-2013
128 6 1 10 20-03-2013
129 6 2 10 21-03-2013
130 6 2 90 21-03-2013
131 6 4 90 21-03-2013
132 6 4 10 21-03-2013
和:
Table: Mail
Columns:
ID int(11) PK AI
Content longtext
Datum varchar(15)
Titel varchar(150)
ID Content Datum Titel
1 (alot of encoded html) 18-03-13 test
2 (alot of encoded html) 18-03-13 test2
4 (alot of encoded html) 18-03-13 alles weer testen
5 (alot of encoded html) 20-03-13 testje
6 (alot of encoded html) 21-03-13 Statusupdate week 6
我使用这2个查询现在选择从表中的数据:
SELECT ID, Titel FROM Mail
SELECT * FROM Subscriptions,
(SELECT MAX(ID) as ids, Mail_ID FROM Subscriptions
WHERE Klant_ID = '".$_GET["ID"]."' GROUP BY Mail_ID) table2
WHERE ID=table2.ids
我想要使用JOIN来获取查询,以便能够使用html创建此表:
我本人来说没有得到使用JOIN,因为我第一次使用它昨天太多的经验,我可以做一个简单的连接查询,但我只是不知道如何做到这一点。如果您有更多问题,请在评论中提问。如果有人能帮助我,这将是伟大的!
被@JaMaBing提供最简单,最快捷的方式,但如果你有一个像'Datum'对你的领域的许多相同的名字,你需要给您想要选择的表格和列的别名。 – ksugiarto 2013-03-22 09:03:22