使用连接联合表编辑查询 - 结果中不需要的列

时间:2016-02-06 22:35:53

标签: mysql sql

我需要编辑sql查询以在结果中显示除stuffId之外的每一列。 Column stuffId仅用于连接目的。

select
    *
from
    Stuff
    join
    (
        select
            stuffId
            , salary
            , hireDate
            , laptop
            , car
        from
            Employee
        union
        select
            stuffId
            , ROUND(Manager.salary + (Manager.salary * cast(bonus as float) / 100), 2) as salary
            , hireDate
            , laptop
            , car
    from
        Manager
    ) people
        on Stuff.id = people.stuffId
order by
    Stuff.id

2 个答案:

答案 0 :(得分:1)

select中列出您想要的列:

select stuff.col1, stuff.col2, . . ., 
       people.salary, people.hireDate, people.laptop, people.car
. . .

如果您只希望stuffId出现一次,则有一种方法是:

select stuff.*, people.salary, people.hireDate, people.laptop, people.car

另一种方法是使用using代替on

答案 1 :(得分:1)

您的第一行代码是请求' *' (所有栏目)。您可以指定要作为输出查看的列。我修改了您的查询,必须删除' ORDER BY Stuff.id'既然你不想要' id'在你的输出中。

select
    p.salary
    , p.hireDate
    , p.laptop
    , p.car
from
    Stuff s
    join
    (
        select
            stuffId
            , salary
            , hireDate
            , laptop
            , car
        from
            Employee
        union
        select
            stuffId
            , ROUND(Manager.salary + (Manager.salary * cast(bonus as float) / 100), 2) as salary
            , hireDate
            , laptop
            , car
    from
        Manager
    ) people p
        on s.id = p.stuffId

您可以根据自己的喜好从表格中选择列。