如何从另一个表中的条件Order中的两个不同列进行SELECT,并通过SQL将它们连接起来

时间:2010-11-27 09:38:17

标签: sql join

我有两张桌子:

表1是..

     firstname  |   lastname  

     Peter      |    Das

     Das        |    James

     vector     |   Call

     cans       |    Das

和table2是

 id |  user   |  roll
 ___________________
 1 |  cans   |  5

 2 |  James  |  2

 3 |  Peter  |  8

 4 | vector |  6

我希望输出结果为

 id |   name
 __________
 3 |  Peter

 1 |  Cans

 2 |  James

sql查询的描述可能是这样的

SELECT (lastname WHERE firstname='Das' AND firstname WHERE lastname='Das' FROM table1 AS name
(SELECT roll from table2 AS rollorder WHERE user=name) ORDER BY rollorder DESC

这里的命令nust应该是table2

我不知道这个SQL查询

所以请帮帮我

欢迎提出各种意见和建议

1 个答案:

答案 0 :(得分:1)

   select t2.id, (case when t1.lastname = 'Das' then t1.firstname
                 when t1.firstname = 'Das' then t1.lastname end)
    from (select (case when lastname = 'Das' then firstname
                       when firstname = 'Das' then lastname end) as name_to_compare,
                 firstname,
                 lastname
            from table1
           where firstname = 'Das' or 
                 lastname = 'Das') t1 inner join
          table2 t2 on t1.name_to_compare = t2.users
    order by t2.roll desc
编辑:纠正了一些错误,这应该是完美的。测试数据测试。用户是关键字,因此将其替换为用户。替换为数据库中的相应列。