简体   繁体   English

嵌套查询语句的MySQL查询

[英]MySql query for nested select statement

I have a table like this: 我有一张这样的桌子:

id   method  callid
-------------------------
1    INVITE  pW.wKkwjxt
2    BYE     pW.wKkwjxt
3    INVITE  YcIEWypRd
4    BYE     YcIEWypRd
5    INVITE  .--.jr1oji1ddt
6    BYE     .--.jr1oji1ddt
7    INVITE  rM7D3IhQR
8    INVITE  kUc0YNjQy
9    INVITE  S07wSM3G
10   BYE     rM7D3IhQR

In this table method column having 2 methods INVITE and BYE with same callid. 在此表的方法列中,具有2个方法的INVITEBYE具有相同的callid。 I need the query like the callid having INVITE and not having BYE with the same callid 我需要像CallId的查询其INVITE并没有BYE用相同的呼叫ID

Output like this: 像这样的输出:

id  method  callid
---------------------
8   INVITE  kUc0YNjQy
9   INVITE  S07wSM3G 

I tried with below query but not getting correct output. 我尝试使用以下查询,但未获得正确的输出。

SELECT `ac1`.`method`,`ac2`.`method`,`ac1`.`callid`,`ac2`.`callid`,`ac1`.`id`,`ac2`.`id` 
FROM `acc` `ac1`
join `acc` `ac2` ON `ac1`.`callid` = `ac2`.`callid` and `ac2`.`method` != 'BYE'

Any one can help me please. 任何人都可以帮助我。

You can do a left join on the callid, when it has no matching callid then it will be null so you can do a2.id IS NULL to filter those out. 您可以在callid上进行左连接,如果没有匹配的callid,则它将为null,因此您可以执行a2.id IS NULL来过滤掉它们。

SELECT `ac1`.`method`,`ac2`.`method`,`ac1`.`callid`,`ac2`.`callid`,`ac1`.`id`,`ac2`.`id` 
FROM acc a1
LEFT JOIN acc a2 ON a2.callid=a1.callid AND a2.method='BYE'
WHERE a1.method='INVITE' 
AND a2.id IS NULL

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

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