简体   繁体   English

尝试添加 INNER JOIN 时 SQL 查询不起作用

[英]SQL Query Not working when trying to add an INNER JOIN

I have an SQL Statement which I finally got working today, Then I wanted to add an INNER JOIN to return my labels instead of numbers and can't get it to work.我有一个 SQL 语句,我今天终于开始工作了,然后我想添加一个 INNER JOIN 来返回我的标签而不是数字,但无法让它工作。

I have three tables port_days, ports and ships我有三个表 port_days、ports 和 ship

The query that I wrote today is..我今天写的查询是..

    SELECT  
        t1.port_day_id, 
        t1.port_id, 
        t1.port_date,
        t1.ship_id,
        t1.arrival_time,
        t1.depart_time
    FROM 
        port_days t1
    WHERE 
        (ship_id = '140' OR ship_id = '145') 
    AND EXISTS (SELECT 
                     port_id, port_date
                FROM 
                     port_days t2
                WHERE 
                     (ship_id = '140' OR ship_id = '145') 
                     AND t1.port_date >= '2016-02-01'
                     AND t1.port_date <= '2016-10-01'
                     AND t1. port_id = t2. port_id
                     AND t1. port_date = t2. port_date
                     GROUP BY port_id, port_date
                     HAVING COUNT(port_day_id) > 1)
   ORDER BY port_date

So far after testing this seems to return all the correct results like this..到目前为止,在测试之后,这似乎返回了所有正确的结果。

{"port_day_id":"290","port_id":"1021","port_date":"2016-06-18","ship_id":"140","arrival_time":"06:30:00","depart_time":"16:30:00"} {"port_day_id":"290","port_id":"1021","port_date":"2016-06-18","ship_id":"140","arrival_time":"06:30:00","出发时间":"16:30:00"}

so now I want to replace the ids for the labels using and INNER JOIN and came up with the query below, but I can't seem to get it to work.所以现在我想使用INNER JOIN替换标签的 id 并提出下面的查询,但我似乎无法让它工作。

    SELECT  
         s.ship_name,
         s.company_name,
         p.port_name,
         p.port_country,
         t1.port_id, 
         t1.port_date,
         t1.ship_id,
         t1.arrival_time,
         t1.depart_time
    FROM 
        port_days t1
    INNER JOIN  
        ships s, ports p
    ON 
        t1.ship_id = s.ship_id 
        AND t1.port_id = p.port_id 
    WHERE 
        (ship_id = '" . $ship_id . "' OR ship_id = '" . $ship2_id . "') 
        AND EXISTS (SELECT 
                        port_id, port_date
                    FROM 
                        port_days t2
                    WHERE (ship_id = '" . $ship_id . "' 
                        OR ship_id = '" . $ship2_id . "') 
                        AND t1.port_date >= '" . $start_date . "'
                        AND t1.port_date <= '" . $end_date . "'
                        AND t1. port_id = t2. port_id
                        AND t1. port_date = t2. port_date
                        HAVING COUNT(port_day_id) > 1)                         
   ORDER BY port_date)

The ship and ports tables hold the ids and labels. ship 和 ports 表保存 id 和标签。

This is what I am hoping to have returned...这就是我希望返回的...

{"port_name":"Southampton",port_country":"England","port_date":"2016-06-18","ship_name":"Arcadia","company_name":"P&O Cruises","arrival_time":"06:30:00","depart_time":"16:30:00"} {"port_name":"Southampton",port_country":"England","port_date":"2016-06-18","ship_name":"Arcadia","company_name":"P&O Cruises","arrival_time":" 06:30:00","depart_time":"16:30:00"}

Thanks in advance.提前致谢。

Try something like this.....尝试这样的事情......

SELECT  
     s.ship_name,
     s.company_name,
     p.port_name,
     p.port_country,
     t1.port_id, 
     t1.port_date,
     t1.ship_id,
     t1.arrival_time,
     t1.depart_time
FROM         port_days t1
INNER JOIN   ships     s  ON t1.ship_id = s.ship_id 
INNER JOIN   ports     p  ON t1.port_id = p.port_id 
WHERE  (t1.ship_id = '" . $ship_id . "' OR t1.ship_id = '" . $ship2_id . "') 
  AND EXISTS (SELECT 
                    port_id, port_date
                FROM 
                    port_days t2
                WHERE (ship_id = '" . $ship_id . "' 
                    OR ship_id = '" . $ship2_id . "') 
                    AND t1.port_date >= '" . $start_date . "'
                    AND t1.port_date <= '" . $end_date . "'
                    AND t1. port_id = t2. port_id
                    AND t1. port_date = t2. port_date
                    HAVING COUNT(port_day_id) > 1)                         
ORDER BY port_date

Keep in mind that INNER JOIN works between port_days and ships, there's no sense putting other table fields in the ON section.请记住,INNER JOIN 在 port_days 和 ship 之间工作,将其他表字段放在 ON 部分是没有意义的。 So if you want to relate other tables, you need a second INNER JOIN.因此,如果您想关联其他表,则需要第二个 INNER JOIN。

FROM 
    port_days t1
INNER JOIN  
    ships s ON t1.ship_id = s.ship_id 
INNER JOIN ports p ON t1.port_id = p.port_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM