繁体   English   中英

如何从条件中的两个不同列中进行选择按另一个表排序并通过SQL将它们联接

[英]How to SELECT from two different columns in condition Order by another table and join them by SQL

我有两个表:

表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

这里的顺序必须是table2

我不知道这个SQL查询

所以请帮我

欢迎任何想法和建议

   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

编辑:纠正了一些错误,这应该可以正常工作。 经过测试数据测试。 用户是关键字,因此将其替换为用户。 用数据库中的相应列替换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM