简体   繁体   English

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

[英]MySql : Fetch records from multiple table by arithmetic operations

I've the following tables : 我有以下表格:

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

I want to grab records from the three tables and the conditions are as follows : 我想从这三个表中获取记录,条件如下:

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

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

Trying this query but its not working : 尝试此查询,但不起作用:

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 = ? )

OK, I give you a full answer with normalized tables: 好的,我为您提供标准化表格的完整答案:

You can normalize the table_2 and insert the column name to table table_1 . 您可以规范化table_2并将列name插入表table_1 See the following and new table structure (with table_1 and table_2 ): 请参阅以下新表结构(带有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

To create these tables, you can use the following script: 要创建这些表,可以使用以下脚本:

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);

A query to get your expected result: 查询以获取您的预期结果:

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

You can find a working example here: http://sqlfiddle.com/#!9/a2e27/3/0 您可以在这里找到一个有效的示例: http : //sqlfiddle.com/#!9/a2e27/3/0

You want to use your original tables? 您要使用原始表格吗? [answer for original question] [原始问题的答案]

If it is possbile I recommend to normalize the tables! 如果有可能,我建议对表格进行规范化! If it is not possible to change the table structure, you can use the following query to get your expected result: 如果无法更改表结构,则可以使用以下查询来获得期望的结果:

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

You can find a working example here: http://sqlfiddle.com/#!9/f22024/7/0 您可以在这里找到一个有效的示例: http : //sqlfiddle.com/#!9/f22024/7/0

Create table/ insert data. 创建表/插入数据。

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)
;

This query checks for both table_1 categories A and B And If the table_1.balans is higher then 0 when table_3.last_pay is summed together per category 此查询检查table_1类别A和B,并且如果按类别将table_3.last_pay相加,则table_1.balans高于0,则为0。

Query 询问

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

Result 结果

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

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

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