关联表查询sql语句
Ⅰ 两表关联查询sql语句的,要怎么写
1、首先打开数据库客户端Navicat Premium 15工具,点击并打开一个数据库连接。
Ⅱ SQL语句联表查询
SELECTitem,sum(a.need_pay) '需要支付金额',sum(b.paymoney) '已付金额'
FROM A,(SELECT b.A_id,sum(B.money) 'paymoney' FROM B group by B.A_id) b
where A.A_id = b.A_id
GROUP BY A.item
HAVINGsum(a.need_pay)<> sum(b.paymoney)
(2)关联表查询sql语句扩展阅读:
关于sql语句连表查询
语法
SELECT fields
FROM table1 INNER JOIN table2
ON table1.field1 compopr table2.field1 AND
ON table1.field2 compopr table2.field2 OR
ON table1.field3 compopr table2.field3;
也可以通过如下语法嵌套 JOIN 语句:
SELECT fields
FROM table1 INNER JOIN
(table2 INNER JOIN [( ]table3
[INNER JOIN [( ]tablex [INNER JOIN ...)]
ON table3.field3 compopr tablex.fieldx)]
ON table2.field2 compopr table3.field3)
ON table1.field1 compopr table2.field2;
LEFT JOIN 或 RIGHT JOIN 可以嵌套在 INNER JOIN 之中,但是 INNER JOIN 不能嵌套于 LEFT JOIN 或 RIGHT JOIN 之中。