在SQL中使用union和inner join

时间:2015-05-24 13:54:52

标签: sql sql-server

我有下面的函数,它合并了2个具有相同字段但数据不同的表,然后在某些字段上执行内连接。 但是,在UNION ALL之后,我在id上得到“id'附近的'语法不正确”。我很难看到我的语法不正确。

IF (@booking_id is NULL AND @id is NULL)
    BEGIN

    SELECT

    A.id,
    A.booking_id,
    A.todo_type,
    A.due,
    A.urgent,
    A.status,
    A.comment,
    A.create_user,
    A.create_date,
    A.delete_date,
    B.first_name, 
    B.last_name, 
    H.holiday_name, 
    H.date_start,
    H.date_end,
    TT.lookup_desc todo_type_desc,
    TS.lookup_desc status_desc

    FROM

    (
        SELECT
        id, booking_id, todo_type, due, urgent, status, comment, create_user, create_date, delete_date
        FROM
        [TODO]
        UNION ALL
        id, booking_id, todo_type, due, urgent, status, comment, create_user, create_date, delete_date
        FROM
        [TODO_OPERATOR]
    )

    A

    INNER JOIN LOOKUP TT ON A.todo_type=TT.lookup_id
    INNER JOIN LOOKUP TS ON A.status=TS.lookup_id
    INNER JOIN BOOKING B ON A.booking_id=B.booking_id
    INNER JOIN HOLIDAY_REF H ON B.holiday_id=H.holiday_id

    WHERE A.delete_date is NULL and TS.lookup_desc<>'Closed'
    ORDER BY A.create_date
    END

1 个答案:

答案 0 :(得分:2)

您需要在for (int j=i+1; j<=i+3; j++){ if (fileContent.size() < j && !fileContent.get(j).matches(PATTERN)){ return false; } else { break; } }

之后添加SELECT
UNION ALL