0

我有一个关于从 MySQL 数据库中加入 2 个表中的数据的问题。首先,我将解释我目前拥有的东西,然后我希望尽可能清楚。

我在数据库中有 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

我现在正在使用这两个查询从表中选择数据:

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 以来,我个人并没有太多使用 JOIN 的经验,我可以进行简单的 JOIN 查询,但我只是不知道该怎么做。如果您有更多问题,请在评论中提问。如果有人可以帮助我,那就太好了!

4

4 回答 4

1

这是您的表之间的简单连接

SELECT Mail_ID, Titel, Status, Subscriptions.Datum FROM Subscriptions
JOIN Mail ON (Subscription.Mail_ID=Mail.ID)
    WHERE Klant_ID = '".$_GET["ID"]."' GROUP BY Mail_ID

行的顺序是随机的,如果你想得到最后的数据,你的查询是正确的。

于 2013-03-22T09:00:06.020 回答
1

尝试:

select mail.id, mail.titel, subscriptions.status, subscriptions.datum
from mail join subscriptions on mail.id = subscriptions.mail_id
于 2013-03-22T09:00:06.207 回答
0
SELECT e.ID ,e.Titel,ea.Status,ea.Datum 
    FROM Mail e 
        LEFT JOIN Subscriptions ea 
            ON e.ID = ea.Mail_ID
            WHERE ea.Klant_ID = '".$_GET["ID"]."' 
            GROUP BY Mail_ID)
    ORDER BY e.ID ASC
于 2013-03-22T11:05:07.213 回答
0

Tyvm @kolonel peteruk、@Kaii 和 @JaMaBing 为您解答!

在您的帮助下,我能够将我的查询与您的合并。我终于用这个查询让它工作了:

SELECT Mail.ID, Mail.Titel, Subscriptions.ID, Subscriptions.Status, Subscriptions.Datum,      Subscriptions.Mail_ID, Subscriptions.Mail_ID, Subscriptions.Klant_ID 
FROM NAW.Subscriptions
JOIN NAW.Mail 
ON Mail.ID = Subscriptions.Mail_ID, 
(SELECT MAX(Subscriptions.ID) as ids, Mail_ID 
FROM NAW.Subscriptions
WHERE Klant_ID =6  
GROUP BY Mail_ID) table2
WHERE Subscriptions.ID=table2.ids
于 2013-03-22T10:35:50.903 回答