關聯表查詢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 之中。