比较同一个表中的两个值

时间:2016-03-15 11:17:39

标签: php mysql sql

我有一张桌子,我想得到所有的名字 某人的工资低于下一个人的工资 同桌,我试过这个但是 它没有任何建议吗?

select t1.Name, t2.Name as Name2 
from employees t1 
inner join employees t2 on 
t1.ID = t2.ID
where t1.Salary < t2.Salary;

我试图打印出薪水较低的每个人的姓名,例如

Joe "has less then" Bob
Joe "has less then" foo
Joe "has less then" Bar
Joe "has less then" Pete

4 个答案:

答案 0 :(得分:1)

比较您当前和下一个ID列

UPDATE [Table] SET [Number] = REPLACE([Number], CHAR(34), '')

答案 1 :(得分:0)

您正在加入相同的ID。你正在比较同一个人的工资。

答案 2 :(得分:0)

试试这个:

select t.LastName
from (
    select Name, 
    Salary, 
    @last_name AS LastName, 
    @last_sal AS LastSal, 
    @last_name := Name , 
    @last_sal := Salary 
    from employees
    cross join (select @last_name := NULL, @last_sal := NULL) param
    ) t
where t.LastSal < t.Salary;

答案 3 :(得分:0)

select t1.Name || " has less then " || t2.Name from employees t1, employees t2 where t1.Salary < t2.Salary;

如果您删除内部联接(结果联接将是交叉联接),我相信您将能够找到您要查找的内容。

PS:查询未在任何数据库架构上进行测试,但是它会起作用,但对连接语法不确定。