如何从同一张表中查找计数(自我联接)

时间:2019-07-13 12:12:43

标签: mysql database

如何将一列与其余各列按同一列的值分组。

我有一个表,其中包含进入退出场景,在该场景中,具有1个进入条形码的1个toll_id和方法1的车辆进入可以退出其他toll_id的休息,并且那条码存储在exit_barcode和方法2中。

table name paid_vehicle
-----------------------------------------------------------------------
 toll_id  |   vehicletype   |  entry_barcode | exit_barcode |   way   |
-----------------------------------------------------------------------
81        |     car         |   asdfghjk     |0             |1        |
-----------------------------------------------------------------------
83        |     bus         |   qwertyu      |0             |1        |
-----------------------------------------------------------------------
82        |     truck       |   dfghfgsd     |poiuyt        |2        |
-----------------------------------------------------------------------
84        |     hcm         |   sdfgsdfg     |lkjhg         |2        |
-----------------------------------------------------------------------
86        |     osv         |   zxcvb        |0             |1        |
-----------------------------------------------------------------------
81        |     bus         |   asdf         |0             |1        |
-----------------------------------------------------------------------
82        |     car         |   dfghghfg     |asdfghjk      |2        |
-----------------------------------------------------------------------
89        |     bus         |   dfhgkjhd     |qwertyu       |2        |
-----------------------------------------------------------------------
88        |     truck       |   poiuyt       |0             |1        |
-----------------------------------------------------------------------
87        |     hcm         |   lkjhg        |0             |1        |
-----------------------------------------------------------------------
87        |     osv         |   sdfgsdfg     |zxcvb         |2        |
-----------------------------------------------------------------------
89        |     bus         |   dfolfgpkg    |asdf          |2        |
-----------------------------------------------------------------------
81        |     car         |   jhfkggtg     |asdfghjk      |2        |
-----------------------------------------------------------------------

现在我想找到 我从1开始进入其他条形码的数量。

select * from paid_vehicle where way='2'  
 and exit_barcode  not in(select entry_barcode from paid_vehicle where   toll_id='81' and way='1' AND entry_barcode!='');
toll_id   |  exit_toll_id   | count
------------------------------------
81        |   82            |  900
------------------------------------
81        |   83            |  4500
------------------------------------
81        |   84            |  9050
------------------------------------
81        |   85            |  7910

1 个答案:

答案 0 :(得分:0)

您可以尝试加入进入和退出条形码

    select t.tool_id, t.exit_toll_id, count(*) count 
    from  (
        select a.tool_id ,  a.entry_barcode, b.tool_id, exit_toll_id  b.exit_barcode
        from paid_vehicle a 
        left join paid_vehicle b on a.entry__barcode = b.entry_barcode
    )
    group by  t.tool_id, t.exit_toll_id
    order by t.tool_id, t.exit_toll_id