dongtu1789 2012-03-10 20:39
浏览 77
已采纳

如何使用AS别名执行复杂查询

First of all, I am new here and just learning sql, so bear with me.

I have a simple question. Let's say I have a table called voting with the following columns:

id, token(int), candidate(int), rank(int)

.. and I want to perform a query like:

SELECT * 
  FROM  voting 
  WHERE rank > t1.rank 
  AND   token = t1.token

.. where t1 is

SELECT  rank,token 
  FROM  voting 
  WHERE candidate = $mycandidate

How can I do this in a single statement using AS alias, or whatever is the simplest method?

Please note that final table created may have rows with different candidates than i have specified i.e the rank,token variables are initially chosen according to candidate but once chosen they may contain any candidate value with them.

  • 写回答

1条回答 默认 最新

  • douzhangli9563 2012-03-10 20:49
    关注

    Something like this should do, I believe:

     select
        v1.*
     from
        voting v1 inner join
        voting v2
            on v1.token = v2.token and
               v2.candidate = $mycandidate and
               v1.rank > v2.rank
    

    EDIT changed SELECT * to SELECT v1.*

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答