多表的JOIN语句不起作用

时间:2018-10-29 16:12:57

标签: mysql

我昨晚才开始学习SQL。我在使用多个JOIN语句显示数据时遇到了麻烦。我有的表是:

Table: CUSTOMER
Contains CustomerID, Country, Last Name

Table: TRANS
Contains CustomerID, TransactionID, DateSold, WorkID

Table: WORK
Contains WorkID, Title, Description

这是我的查询:

Select CUSTOMER.LastName, CUSTOMER.CustomerID, WORK.WorkID, 
        Description, Title

FROM CUSTOMER JOIN TRANS
        ON CUSTOMER.CustomerID = TRANS.CustomerID

JOIN WORK
        ON TRANS.WorkID = WORK.WorkID

WHERE DateSold = '11/17/2014'

GROUP BY CUSTOMER.CustomerID, TRANS.CustomerID, CUSTOMER.LastName,
         WORK.WorkID, Title, DateSold, Description

请注意,在本文中,仅在select语句中,我故意省略了GROUP BY语句中出现的一些项目。 (它们包含在SELECT语句中不会导致程序正常执行。)

显示的只是GROUP BY语句,但没有实际数据。请帮我解决我做错的事情。谢谢。

2 个答案:

答案 0 :(得分:0)

在mysql中使用firewalls: main: pattern: ^/ host: %main_domain% form_login: provider: fos_userbundle csrf_provider: form.csrf_provider logout: path: /logout target: /login anonymous: true context: main_context sub1: pattern: ^/ host: %sub1_domain% form_login: provider: fos_userbundle csrf_provider: form.csrf_provider logout: path: /logout target: /login anonymous: true context: main_context sub2: pattern: ^/ host: %sub2_domain% .... 函数来转换日期字符串。

STR_TO_DATE

答案 1 :(得分:0)

您可以将一个表与两个不同的表联接在一起,但是必须从公用表开始。 对于您写查询的方式,您要说的是先与TRANS一起加入CUSTOMER,然后再与WORK一起加入,但是这种情况的条件不正确(而且这不是您想要做的)。

Select CUSTOMER.LastName, CUSTOMER.CustomerID, WORK.WorkID, 
        Description, Title

FROM TRANS JOIN CUSTOMER
        ON CUSTOMER.CustomerID = TRANS.CustomerID

JOIN WORK
        ON TRANS.WorkID = WORK.WorkID

WHERE DateSold = '11/17/2014'

GROUP BY CUSTOMER.CustomerID, TRANS.CustomerID, CUSTOMER.LastName,
         WORK.WorkID, Title, DateSold, Description

是TRANS,您首先与CUSTOMER加入,然后与WORK合作。