MySQL UNION With Like和<

时间:2016-11-30 21:30:24

标签: mysql sql union

我正在尝试创建一个

的UNION

创建一个联盟,以提供身份证在100分钟且薪水低于65,000的教师的结果。

faculty
facID   name    salary
320 Laura Kassler   50000
125 Charity Burbage 62000
500 Pomona Sprout   61000
100 Minerva McGonagall  85000
185 Severus Snape   70000
200 Rubeus Hagrid   50000

这是我的代码:

select name, facID, salary
from faculty
where salary < '65000'
UNION
select name, facID, salary
from faculty
where facID like '1%';

结果:

+--------------------+-------+--------+
| name               | facID | salary |
+--------------------+-------+--------+
| Minerva McGonagall |   100 |  85000 |
| Charity Burbage    |   125 |  62000 |
| Severus Snape      |   185 |  70000 |
| Rubeus Hagrid      |   200 |  50000 |
| Laura Kassler      |   320 |  50000 |
| Pomona Sprout      |   500 |  61000 |
+--------------------+-------+--------+

答案应该是Charity Burbage facID 125和62000的薪水。 我做错了什么?

2 个答案:

答案 0 :(得分:1)

select name, facID, salary
from faculty
where salary < '65000'
and facID like '1%'
order by facID;

答案 1 :(得分:0)

然后尝试从第二个select语句的结果中返回任何内容。

    select name, facID, salary
    from faculty
    where salary < '65000' AND facID like '1%'
    UNION
    select name, facID, salary
    from faculty
    where 1 <> 1
    Order by facID