2
mysql> select * from on_connected;
+----+-----------+-------------+---------------------------+---------------------+
| id | extension | destination | call_id                   | created_at          |
+----+-----------+-------------+---------------------------+---------------------+
| 11 |   1111111 | 01155555551 | 521243ad953e-965inwuz1gku | 2013-08-19 17:11:53 |
+----+-----------+-------------+---------------------------+---------------------+

mysql> select * from on_disconnected;
+----+-----------+-------------+---------------------------+---------------------+
| id | extension | destination | call_id                   | created_at          |
+----+-----------+-------------+---------------------------+---------------------+
|  1 |   1111111 | 01155555551 | 521243ad953e-965inwuz1gku | 2013-08-19 17:11:57 |
+----+-----------+-------------+---------------------------+---------------------+
1 row in set (0.00 sec)

两者之间有4秒的时间差。我想使用某种类型的查询来计算差异。我知道 TIMEFIFF() 并加入,但此时缺乏形成查询的技能。

到目前为止,这是我的尝试:

SELECT TIMEDIFF(to_seconds(od.created_at), to_seconds(oc.created_at)) 
FROM on_connected oc 
JOIN on_disconnected od 
ON oc.call_id=od.call_id 
WHERE call_id='521243ad953e-965inwuz1gku';

mysql报告:

ERROR 1052 (23000): Column 'call_id' in where clause is ambiguous
4

4 回答 4

1

在您的 where 子句中更改

WHERE call_id='521243ad953e-965inwuz1gku';

WHERE oc.call_id='521243ad953e-965inwuz1gku';

或者

WHERE od.call_id='521243ad953e-965inwuz1gku';

没关系。

于 2013-08-19T16:59:05.210 回答
1

如果您想要始终保持差异

SELECT TIME_TO_SEC(TIMEDIFF(od.created_at, oc.created_at)) 
FROM on_connected oc
JOIN on_disconnected od ON od.call_id = oc.call_id

演示

对于单个call_id,您需要为过滤器中的列名起别名:

WHERE oc.call_id = '521243ad953e-965inwuz1gku'

演示

于 2013-08-19T16:59:32.060 回答
1

在 where 子句中尝试 oc.call_id。

尽管此时值已经匹配,但 sql 解析器仍然需要知道您指的是哪一个。

于 2013-08-19T16:59:38.347 回答
0

当您JOIN使用两个表中名称相同的列时,您可以使用该USING子句代替ON

SELECT TIMEDIFF(to_seconds(od.created_at), to_seconds(oc.created_at)) 
FROM on_connected oc 
JOIN on_disconnected od 
USING(call_id)                             -- eq. to `od.call_id = oc.call_id`
WHERE call_id='521243ad953e-965inwuz1gku'; -- no need to specify the table name here

不仅这样会节省一些键,而且通过这样做,您将能够在不指定表名的情况下引用该列。

于 2013-08-19T17:21:09.253 回答