根据条件计算另一个表的总结果减去原始表列中的数字

时间:2017-08-07 14:16:23

标签: mysql

我担心我离开mysql的时间太长而且我已经走了一个兔子洞。

client_sessions

id      cid      sessions      date
------------------------------------------
1       102          5       01/07/2017
2       102          3       01/08/2017
3       141          5       29/07/2017

用户102从01/07/2017 id - 1开始有5个会话 用户102从01/08/2017 id-2

开始有3个会话

client_sessions_history

id      sid      date
----------------------------
1       1      03/07/2017
2       1      07/07/2017
3       1      14/07/2017
4       1      21/07/2017
5       1      27/07/2017
6       2      01/08/2017

sid是来自client_sessions的id,这里有5个为1 和2来自这里。

我想要的结果是:

date            remaining
-----------------------------
01/07/2017          0
01/08/2017          2

这将是来自client_sessions的会话,其日期是唯一的,减去sid所独有的client_sessions_history中的总会话数

我已尝试过以下内容,但我一直在圈子里。

select a.sessions, a.id, (select count(distinct sid) from client_sessions_history where b.sid = a.id) as remaining from client_sessions as a, client_sessions_history as b where a.cid = 102

基本上我想得到cid 102的剩余总数

3 个答案:

答案 0 :(得分:1)

创建表/插入数据

CREATE TABLE client_sessions
    (`id` INT, `cid` INT, `sessions` INT, `date` VARCHAR(10))
;

INSERT INTO client_sessions
    (`id`, `cid`, `sessions`, `date`)
VALUES
    (1, 102, 5, '01/07/2017'),
    (2, 102, 3, '01/08/2017'),
    (3, 141, 5, '29/07/2017')
;


CREATE TABLE client_sessions_history
    (`id` INT, `sid` INT, `date` VARCHAR(10))
;

INSERT INTO client_sessions_history
    (`id`, `sid`, `date`)
VALUES
    (1, 1, '03/07/2017'),
    (2, 1, '07/07/2017'),
    (3, 1, '14/07/2017'),
    (4, 1, '21/07/2017'),
    (5, 1, '27/07/2017'),
    (6, 2, '01/08/2017')
;

您需要创建一个已交付的表,其中COUNT客户端历史记录会话。

<强>查询

SELECT 
    client_sessions.date
 , (client_sessions.sessions - client_sessions_grouped.client_sessions_history_sid_count) AS remaining
FROM ( 

  SELECT 
      client_sessions.id
    , COUNT(client_sessions_history.sid) AS client_sessions_history_sid_count
   FROM 
    client_sessions
   INNER JOIN 
     client_sessions_history
   ON
     client_sessions.id =  client_sessions_history.sid 
   GROUP BY
    client_sessions.id 
) 
 AS
   client_sessions_grouped
INNER JOIN
  client_sessions
ON
  client_sessions.id = client_sessions_grouped.id

WHERE
  client_sessions.cid = 102  

<强>结果

date        remaining  
----------  -----------
01/07/2017            0
01/08/2017            2

答案 1 :(得分:0)

可能是你需要一个不同的计数和分组

http://198.1.109.253/~ecc2014/api/?verifyKey=abcdefghijklmnopqrstuvwxyz&task=login&email=user@yopmail.com&pwd=secret
verifyLoginCredentials Request failed with error: Optional("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>400 Bad Request</title>\n</head><body>\n<h1>Bad Request</h1>\n<p>Your browser sent a request that this server could not understand.<br />\n</p>\n<hr>\n<address>Apache/2.4.27 (cPanel) OpenSSL/1.0.2k mod_bwlimited/1.4 Server at 198.1.109.253 Port 80</address>\n</body></html>\n")

答案 2 :(得分:0)

尝试此查询:

SELECT 
  date , 
  (sessions - (SELECT COUNT(*) FROM client_sessions_history WHERE sid = client_sessions.id )) AS remaining

FROM client_sessions   
WHERE cid = 102