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
答案 0 :(得分:1)
在你的where子句中更改
WHERE call_id='521243ad953e-965inwuz1gku';
到
WHERE oc.call_id='521243ad953e-965inwuz1gku';
或
WHERE od.call_id='521243ad953e-965inwuz1gku';
没关系。
答案 1 :(得分: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'
答案 2 :(得分:1)
在where子句中尝试oc.call_id。
虽然这些值在这一点上已经匹配,但是sql解析器仍然需要知道你指的是哪一个。
答案 3 :(得分: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
不仅这会保存一些关键的stokes,但通过这样做,你将能够引用该列而不指定表名。