MySql:通过算术运算从多个表中获取记录

时间:2017-01-17 15:17:20

标签: mysql

我有以下表格:

table_1
------------------
id      uid     category    balance 
1       1       A           100 
2       2       B           80

table_2
------------------
id      uid     name
1       1       ABC
2       2       XYZ

table_2
------------------
id      uid     last_pay
1       1       10
2       1       10
3       1       10
4       2       80

我想从三个表中获取记录,条件如下:

a) table_1.category = something
b) ( table_1.balance - table_3.SUM(`last_pay`) ) > 0

我想要table_1.category =' A'因为(100 - (10 + 10 + 10))> 0

尝试此查询但不起作用:

SELECT t1.uid,t1.category,t1.balance,t2.uid,t2.name FROM table_1 as t1 
LEFT JOIN table_2 as t2 ON t1.uid = t2.uid
WHERE t1.category = 2
AND t1.balance - (SELECT SUM(`last_pay`) FROM table_3 WHERE uid = ? )

2 个答案:

答案 0 :(得分:1)

好的,我给你一个标准化表的完整答案:

您可以规范化table_2并将列name插入表table_1。请参阅以下新表结构(使用table_1table_2):

table_1
--------------------------------------------
id      uid     category    balance     name
1       1       A           100         ABC
2       2       B           80          XYZ

table_2
------------------------
id      uid     last_pay
1       1       10
2       1       10
3       1       10
4       2       80

要创建这些表,可以使用以下脚本:

CREATE TABLE table_1 (
    `id` INT, 
    `uid` INT, 
    `category` VARCHAR(1), 
    `balance` INT,
    `name` VARCHAR(3)
);

INSERT INTO table_1 VALUES
    (1, 1, 'A', 100, 'ABC'),
    (2, 2, 'B', 80, 'XYZ');

CREATE TABLE table_2 (
    `id` INT, 
    `uid` INT, 
    `last_pay` INT
);

INSERT INTO table_2 VALUES
    (1, 1, 10),
    (2, 1, 10),
    (3, 1, 10),
    (4, 2, 80);

获得预期结果的查询:

SELECT t1.uid, t1.category, t1.balance, t2.uid, t1.name
FROM table_1 t1 
   LEFT JOIN (
       SELECT uid, SUM(last_pay) AS last_pay 
       FROM table_2
       GROUP BY uid
    ) t2 ON t1.uid = t2.uid
WHERE (t1.balance - t2.last_pay) > 0
  

您可以在此处找到一个有效的示例: http://sqlfiddle.com/#!9/a2e27/3/0

您想使用原始表吗? [原问题的答案]

如果可能,我建议将表格标准化!如果无法更改表结构,则可以使用以下查询来获得预期结果:

SELECT t1.uid, t1.category, t1.balance, t2.uid, t2.name
FROM table_1 AS t1 LEFT JOIN table_2 AS t2 ON t1.uid = t2.uid
    LEFT JOIN (
        SELECT uid, SUM(last_pay) AS last_pay 
        FROM table_3 
        GROUP BY uid
    ) t3 ON t1.uid = t3.uid
WHERE (t1.balance - t3.last_pay) > 0
  

您可以在此处找到一个有效的示例: http://sqlfiddle.com/#!9/f22024/7/0

答案 1 :(得分:1)

创建表/插入数据。

CREATE TABLE table_1
    (`id` int, `uid` int, `category` varchar(1), `balance` int)
;

INSERT INTO table_1
    (`id`, `uid`, `category`, `balance`)
VALUES
    (1, 1, 'A', 100),
    (2, 2, 'B', 80)
;


CREATE TABLE table_2
    (`id` int, `uid` int, `name` varchar(3))
;

INSERT INTO table_2
    (`id`, `uid`, `name`)
VALUES
    (1, 1, 'ABC'),
    (2, 2, 'XYZ')
;



CREATE TABLE table_3
    (`id` int, `uid` int, `last_pay` int)
;

INSERT INTO table_3
    (`id`, `uid`, `last_pay`)
VALUES
    (1, 1, 10),
    (2, 1, 10),
    (3, 1, 10),
    (4, 2, 80)
;

此查询检查table_1类别A和B. 如果table_1.balans高于0,则table_3.last_pay按类别汇总在一起

<强>查询

SELECT 
   table_1.uid
 , table_1.category
 , table_1.balance
 , table_2.uid
 , table_2.name
FROM (
 SELECT
    uid
  , SUM(table_3.last_pay) sum_last_pay
 FROM 
  table_3
 GROUP BY
  table_3.uid   
) AS 
 table_3_summed

INNER JOIN 
 table_1
ON
  table_1.uid = table_3_summed.uid

INNER JOIN 
 table_2
ON 
 table_1.uid = table_2.uid 

WHERE
 (table_1.balance - table_3_summed.sum_last_pay) > 0

<强>结果

   uid  category  balance     uid  name    
------  --------  -------  ------  --------
     1  A             100       1  ABC