SQL语句联表查询 多表联合查询SQL语句怎么写
SQL\u591a\u8868\u67e5\u8be2\u8bed\u53e5\u600e\u4e48\u5199SQL\u5199\u591a\u8868\u67e5\u8be2\u8bed\u53e5\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a
\u6211\u4eec\u9700\u8981\u51c6\u5907\u7684\u6750\u6599\u5206\u522b\u662f\uff1a\u7535\u8111\u3001sql\u67e5\u8be2\u5668\u3002
1\u3001\u9996\u5148\uff0c\u6253\u5f00sql\u67e5\u8be2\u5668\uff0c\u8fde\u63a5\u4e0a\u76f8\u5e94\u7684\u6570\u636e\u5e93\u8868\uff0c\u4f8b\u5982m1\u8868\u3001m2\u8868\u3002
2\u3001\u70b9\u51fb\u201c\u67e5\u8be2\u201d\u6309\u94ae\uff0c\u518d\u8f93\u5165\uff1aselect max(km) from m1 join m2 on m1.md=m2.md where id=14 and lx=15\u3002
3\u3001\u70b9\u51fb\u201c\u8fd0\u884c\u201d\u6309\u94ae\uff0c\u6b64\u65f6\u67e5\u8be2\u5230km\u7684\u6700\u5927\u503c\u4e3a20\u3002
\u4e00\u4f7f\u7528SELECT\u5b50\u53e5\u8fdb\u884c\u591a\u8868\u67e5\u8be2
SELECT \u5b57\u6bb5\u540d FROM \u88681\uff0c\u88682 \u2026 WHERE \u88681.\u5b57\u6bb5 = \u88682.\u5b57\u6bb5 AND \u5176\u5b83\u67e5\u8be2\u6761\u4ef6
SELECT a.id,a.name,a.address,a.date,b.math,b.english,b.chinese FROM tb_demo065_tel AS b,tb_demo065 AS a WHERE a.id=b.id
\u6ce8:\u5728\u4e0a\u9762\u7684\u7684\u4ee3\u7801\u4e2d\uff0c\u4ee5\u4e24\u5f20\u8868\u7684id\u5b57\u6bb5\u4fe1\u606f\u76f8\u540c\u4f5c\u4e3a\u6761\u4ef6\u5efa\u7acb\u4e24\u8868\u5173\u8054\uff0c\u4f46\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u4e0d\u5e94\u8be5\u8fd9\u6837\u4f7f\u7528\uff0c\u6700\u597d\u7528\u4e3b\u5916\u952e\u7ea6\u675f\u6765\u5b9e\u73b0
\u4e8c\u4f7f\u7528\u8868\u7684\u522b\u540d\u8fdb\u884c\u591a\u8868\u67e5\u8be2
\u5982:SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_demo065 a,tb_demo065_tel b WHERE a.id=b.id AND b.id='$_POST[textid]'
SQL\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u4e3a\u8868\u6307\u5b9a\u522b\u540d
\u7b2c\u4e00\u79cd\u662f\u901a\u8fc7\u5173\u952e\u5b57AS\u6307\u5b9a,\u5982
SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_demo065 AS a,tb_demo065_tel AS b WHERE a.id=b.id
\u7b2c\u4e8c\u79cd\u662f\u5728\u8868\u540d\u540e\u76f4\u63a5\u52a0\u8868\u7684\u522b\u540d\u5b9e\u73b0
SELECT a.id,a.name,a.address,b.math,b.english,b.chinese FROM tb_demo065 a,tb_demo065_tel b WHERE a.id=b.id
\u4f7f\u7528\u8868\u7684\u522b\u540d\u5e94\u6ce8\u610f\u51e0\u4e0b\u51e0\u70b9
(1)\u522b\u540d\u901a\u5e38\u662f\u4e00\u4e2a\u7f29\u77ed\u4e86\u7684\u8868\u540d\uff0c\u7528\u4e8e\u5728\u8fde\u63a5\u4e2d\u5f15\u7528\u8868\u4e2d\u7684\u7279\u5b9a\u5217\uff0c\u5982\u679c\u8fde\u63a5\u4e2d\u7684\u591a\u4e2a\u8868\u4e2d\u6709\u76f8\u540c\u7684\u540d\u79f0\u5217\u5b58\u5728\uff0c\u5fc5\u987b\u7528\u8868\u540d\u6216\u8868\u7684\u522b\u540d\u9650\u5b9a\u5217\u540d
(2)\u5982\u679c\u5b9a\u4e49\u4e86\u8868\u7684\u522b\u540d\u5c31\u4e0d\u80fd\u518d\u4f7f\u7528\u8868\u540d
\u4e09\u5408\u5e76\u591a\u4e2a\u7ed3\u679c\u96c6
SQL\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7UNION \u6216 ALL\u5c06\u591a\u4e2aSELECT\u8bed\u53e5\u7684\u67e5\u8be2\u7ed3\u679c\u5408\u5e76\u8f93\u51fa\uff0c\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57\u7684\u4f7f\u7528\u8bf4\u660e\u5982\u4e0b\uff1a
UNION:\u5229\u7528\u8be5\u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u591a\u4e2aSELECT \u8bed\u53e5\u7684\u67e5\u8be2\u7ed3\u679c\u5408\u5e76\u8f93\u51fa\uff0c\u5e76\u5220\u9664\u91cd\u590d\u884c
ALL:\u5229\u7528\u8be5\u5173\u952e\u5b57\u53ef\u4ee5\u5c06\u591a\u4e2aSELECT \u8bed\u53e5\u7684\u67e5\u8be2\u7ed3\u679c\u5408\u5e76\u8f93\u51fa\uff0c\u4f46\u4e0d\u4f1a\u5220\u9664\u91cd\u590d\u884c
\u5728\u4f7f\u7528UNION\u6216ALL\u5173\u952e\u5b57\u5c06\u591a\u4e2a\u8868\u5408\u5e76\u8f93\u51fa\u65f6\uff0c\u67e5\u8be2\u7ed3\u679c\u5fc5\u987b\u5177\u6709\u76f8\u540c\u7684\u7ed3\u6784\u5e76\u4e14\u6570\u636e\u7c7b\u578b\u5fc5\u987b\u517c\u5bb9,\u53e6\u5916\u4f7f\u7528UNION\u65f6\u4e24\u5f20\u8868\u7684\u5b57\u6bb5\u6570\u91cf\u4e5f\u5fc5\u987b\u76f8\u540c\uff0c\u5426\u5219\u4f1a\u63d0\u793aSQL\u8bed\u53e5\u6709\u9519\u8bef\u3002
e.x:SELECT id,name,pwd FROM tb_demo067 UNION SELECT uid,price,date FROM tb_demo067_tel
\u56db\u7b80\u5355\u5d4c\u5957\u67e5\u8be2
\u5b50\u67e5\u8be2:\u5b50\u67e5\u8be2\u662f\u4e00\u4e2aSELECT\u67e5\u8be2\uff0c\u8fd4\u56de\u5355\u4e2a\u503c\u4e14\u5d4c\u5957\u5728SELECT\u3001INSERT\u3001UPDATE\u548cDELETE\u8bed\u53e5\u6216\u5176\u5b83\u67e5\u8be2\u8bed\u53e5\u4e2d\uff0c\u4efb\u4f55\u53ef\u4ee5\u4f7f\u7528\u8868\u8fbe\u5f0f\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528\u5b50\u67e5\u8be2.
SELECT id,name,sex,date FROM tb_demo068 WHERE id in(SELECT id FROM tb_demo068 WHERE id='$_POST[test]')
\u5185\u8fde\u63a5\uff1a\u628a\u67e5\u8be2\u7ed3\u679c\u4f5c\u4e3aWHERE\u5b50\u53e5\u7684\u67e5\u8be2\u6761\u4ef6\u5373\u79f0\u4e3a\u5185\u8fde\u63a5
\u4e94\u590d\u6742\u7684\u5d4c\u5957\u67e5\u8be2
\u591a\u8868\u4e4b\u95f4\u7684\u5d4c\u5957\u67e5\u8be2\u53ef\u4ee5\u901a\u8fc7\u8c13\u8bcdIN\u5b9e\u73b0\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b:
test_expression[NOT] IN{
subquery
}
\u53c2\u6570\u8bf4\u660e\uff1atest_expression\u6307SQL\u8868\u8fbe\u5f0f\uff0csubquery\u5305\u542b\u67d0\u7ed3\u679c\u96c6\u7684\u5b50\u67e5\u8be2
\u591a\u8868\u5d4c\u5957\u67e5\u8be2\u7684\u539f\u7406:\u65e0\u8bba\u662f\u591a\u5c11\u5f20\u8868\u8fdb\u884c\u5d4c\u5957\uff0c\u8868\u4e0e\u8868\u4e4b\u95f4\u4e00\u5b9a\u5b58\u5728\u67d0\u79cd\u5173\u8054\uff0c\u901a\u8fc7WHERE\u5b50\u53e5\u5efa\u7acb\u6b64\u79cd\u5173\u8054\u5b9e\u73b0\u67e5\u8be2
\u516d\u5d4c\u5957\u67e5\u8be2\u5728\u67e5\u8be2\u7edf\u8ba1\u4e2d\u7684\u5e94\u7528
\u5b9e\u73b0\u591a\u8868\u67e5\u8be2\u65f6\uff0c\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u8c13\u8bcdANY\u3001SOME\u3001ALL,\u8fd9\u4e9b\u8c13\u8bcd\u88ab\u79f0\u4e3a\u5b9a\u91cf\u6bd4\u8f83\u8c13\u8bcd\uff0c\u53ef\u4ee5\u548c\u6bd4\u8f83\u8fd0\u7b97\u7b26\u8054\u5408\u4f7f\u7528\uff0c\u5224\u65ad\u662f\u5426\u5168\u90e8\u8fd4\u56de\u503c\u90fd\u6ee1\u8db3\u641c\u7d22\u6761\u4ef6.SOME\u548cANY\u8c13\u8bcd\u662f\u5b58\u5728\u91cf\u7684\uff0c\u53ea\u6ce8\u91cd\u662f\u5426\u6709\u8fd4\u56de\u503c\u6ee1\u8db3\u641c\u7d22\u6761\u4ef6\uff0c\u8fd9\u4e24\u4e2a\u8c13\u8bcd\u7684\u542b\u4e49\u76f8\u540c\uff0c\u53ef\u4ee5\u66ff\u6362\u4f7f\u7528;ALL\u8c13\u8bcd\u79f0\u4e3a\u901a\u7528\u8c13\u8bcd\uff0c\u5b83\u53ea\u5173\u5fc3\u662f\u5426\u6709\u8c13\u8bcd\u6ee1\u8db3\u641c\u7d22\u8981\u6c42.
SELECT * FROM tb_demo069_people WHERE uid IN(SELECT deptID FROM tb_demo069_dept WHERE deptName='$_POST[select]')
SELECT a.id,a.name FROM tb_demo067 AS a WHERE id<3)
>ANY \u5927\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u67d0\u4e2a\u503c
>=ANY \u5927\u4e8e\u7b49\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u67d0\u4e2a\u503c
<=ANY \u5c0f\u4e8e\u7b49\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u67d0\u4e2a\u503c
=ANY \u7b49\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u67d0\u4e2a\u503c
!=ANY\u6216>ANY \u4e0d\u7b49\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u67d0\u4e2a\u503c
>ALL \u5927\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u6240\u6709\u503c
>=ALL \u5927\u4e8e\u7b49\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u6240\u6709\u503c
<=ALL \u5c0f\u4e8e\u7b49\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u6240\u6709\u503c
=ALL \u7b49\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u6240\u6709\u503c
!=ALL\u6216>ALL \u4e0d\u7b49\u4e8e\u5b50\u67e5\u8be2\u4e2d\u7684\u6240\u6709\u503c
\u4e03.\u4f7f\u7528\u5b50\u67e5\u8be2\u4f5c\u6d3e\u751f\u7684\u8868
\u5728\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u7528\u5230\u4ece\u4e00\u4e2a\u4fe1\u606f\u8f83\u4e3a\u5b8c\u5584\u7684\u8868\u4e2d\u6d3e\u751f\u51fa\u4e00\u4e2a\u53ea\u542b\u6709\u51e0\u4e2a\u5173\u952e\u5b57\u6bb5\u7684\u4fe1\u606f\u8868\uff0c\u901a\u8fc7\u5b50\u67e5\u8be2\u5c31\u53ef\u4ee5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807,\u5982
SELECT people.name,people.chinese,people.math,people.english FROM (SELECT name,chinese,math,english FROM tb_demo071) AS people
\u6ce8:\u5b50\u67e5\u8be2\u5e94\u9075\u5faa\u4ee5\u4e0b\u89c4\u5219:
(1)\u7531\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5f15\u5165\u7684\u5185\u5c42\u5b50\u67e5\u8be2\u53ea\u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\u6216\u5217\u540d\uff0c\u5728\u5916\u5c42\u8bed\u53e5\u4e2d\u7684WHERE\u5b50\u53e5\u5185\u547d\u540d\u7684\u5217\u5fc5\u987b\u4e0e\u5185\u5c42\u5b50\u67e5\u8be2\u547d\u540d\u7684\u5217\u517c\u5bb9
(2)\u7531\u4e0d\u53ef\u66f4\u6539\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5f15\u5165\u7684\u5b50\u67e5\u8be2(\u6bd4\u8f83\u8fd0\u7b97\u7b26\u540e\u9762\u4e0d\u8ddf\u5173\u952e\u5b57ANY\u6216ALL)\u4e0d\u5305\u62ecGROUP BY \u6216 HAVING\u5b50\u53e5\uff0c\u9664\u975e\u9884\u5148\u786e\u5b9a\u4e86\u6210\u7ec4\u6216\u5355\u4e2a\u7684\u503c
(3)\u7528EXISTS\u5f15\u5165\u7684SELECT\u5217\u8868\u4e00\u822c\u90fd\u7531*\u7ec4\u6210\uff0c\u4e0d\u5fc5\u6307\u5b9a\u5217\u540d
(4)\u5b50\u67e5\u8be2\u4e0d\u80fd\u5728\u5185\u90e8\u5904\u7406\u5176\u7ed3\u679c
\u516b\u4f7f\u7528\u5b50\u67e5\u8be2\u4f5c\u8868\u8fbe\u5f0f
SELECT (SELECT AVG(chinese)FROM tb_demo071),(SELECT AVG(english)FROM tb_demo071),(SELECT AVG(math)FROM tb_demo071) FROM tb_demo071
\u6ce8\uff1a\u5728\u4f7f\u7528\u5b50\u67e5\u8be2\u65f6\u6700\u597d\u4e3a\u5217\u8868\u9879\u53d6\u4e2a\u522b\u540d\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u7528\u6237\u5728\u4f7f\u7528mysql_fetch_array()\u51fd\u6570\u65f6\u4e3a\u8868\u9879\u8d4b\u503c,\u5982
SELECT (SELECT AVG(chinese) FROM tb_demo071) AS yuwen ,(SELECT AVG(english) FROM tb_demo071) AS yingyu,(SELECT AVG(math) FROM tb_demo071) AS shuxue FROM tb_demo071
\u4e5d\u4f7f\u7528\u5b50\u67e5\u8be2\u5173\u8054\u6570\u636e
SELECT * FROM tb_demo072_student WHERE id=(SELECT id FROM tb_demo072_class WHERE className = '$_POST[text]')
\u5341\u591a\u8868\u8054\u5408\u67e5\u8be2
\u5229\u7528SQL\u8bed\u53e5\u4e2d\u7684UNION\uff0c\u53ef\u4ee5\u5c06\u4e0d\u540c\u8868\u4e2d\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u4fe1\u606f\u663e\u793a\u5728\u540c\u4e00\u5217\u4e2d\u3002
e.x:SELECT * FROM tb_demo074_student UNION SELECT * FROM tb_demo074_fasten
\u6ce8:\u4f7f\u7528UNION\u65f6\u5e94\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\uff1a
(1)\u5728\u4f7f\u7528UNION\u8fd0\u7b97\u7b26\u7ec4\u5408\u7684\u8bed\u53e5\u4e2d\uff0c\u6240\u6709\u9009\u62e9\u5217\u8868\u7684\u8868\u8fbe\u5f0f\u6570\u76ee\u5fc5\u987b\u76f8\u540c\uff0c\u5982\u5217\u540d\u3001\u7b97\u672f\u8868\u8fbe\u5f0f\u53ca\u805a\u5408\u51fd\u6570\u7b49
(2)\u5728\u6bcf\u4e2a\u67e5\u8be2\u8868\u4e2d\uff0c\u5bf9\u5e94\u5217\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u4e00\u6837\u3002
\u5341\u4e00\u5bf9\u8054\u5408\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f
\u4e3a\u4e86UNION\u7684\u8fd0\u7b97\u517c\u5bb9\uff0c\u8981\u6c42\u6240\u6709SELECT\u8bed\u53e5\u90fd\u4e0d\u80fd\u6709ORDER BY\u8bed\u53e5\uff0c\u4f46\u6709\u4e00\u79cd\u60c5\u51b5\u4f8b\u5916\uff0c\u90a3\u5c31\u662f\u5728\u6700\u540e\u4e00\u4e2aSELECT\u8bed\u53e5\u4e2d\u653e\u7f6eORDER BY \u5b50\u53e5\u5b9e\u73b0\u7ed3\u679c\u7684\u6700\u7ec8\u6392\u5e8f\u8f93\u51fa\u3002
e.x:SELECT * FROM tb_demo074_student UNION SELECT * FROM tb_demo074_fasten ORDER BY id
\u4f7f\u7528UNION\u6761\u4ef6\u4e0a\u76f8\u5bf9\u6bd4\u8f83\u82db\u523b\uff0c\u6240\u4ee5\u4f7f\u7528\u6b64\u8bed\u53e5\u65f6\u4e00\u5b9a\u8981\u6ce8\u610f\u4e24\u4e2a\u8868\u9879\u6570\u76ee\u548c\u5b57\u6bb5\u7c7b\u578b\u662f\u5426\u76f8\u540c
\u5341\u4e8c\u6761\u4ef6\u8054\u5408\u8bed\u53e5
SELECT * FROM tb_demo076_BEIJING GROUP BY name HAVING name='\u4eba\u6c11\u90ae\u7535\u51fa\u7248\u793e' OR name='\u673a\u68b0\u5de5\u4e1a\u51fa\u7248\u793e' UNION SELECT * FROM tb_demo076_BEIJING GROUP BY name HAVING name >'\u4eba\u6c11\u90ae\u7535\u51fa\u7248\u793e' AND name >'\u673a\u68b0\u5de5\u4e1a\u518d\u7248\u793e' ORDER BY id
\u4e0a\u9762\u8bed\u53e5\u5e94\u7528\u4e86GROUP BY\u5206\u7ec4\u8bed\u53e5\u548cHAVING\u8bed\u53e5\u5b9e\u73b0\u6761\u4ef6\u8054\u5408\u67e5\u8be2\u3002\u5176\u5b9e\u73b0\u76ee\u7684\u662f\u5148\u4fdd\u8bc1\u5c06'\u4eba\u6c11\u90ae\u7535\u51fa\u7248\u793e'\u548c'\u673a\u68b0\u5de5\u4e1a\u51fa\u7248\u793e'\u59cb\u7ec8\u4f4d\u4e8e\u540d\u5355\u6700\u524d\u5217\uff0c\u7136\u540e\u518d\u8f93\u51fa\u5176\u5b83\u7684\u51fa\u7248\u793e
\u5341\u4e09\u7b80\u5355\u5185\u8fde\u63a5\u67e5\u8be2
SELECT filedlist FROM table1 [INNER] JOIN table2 ON table1.column1 = table2.column1
\u5176\u4e2d\uff0cfiledlist\u662f\u8981\u663e\u793a\u7684\u5b57\u6bb5,INNER\u8868\u793a\u8868\u4e4b\u95f4\u7684\u8fde\u63a5\u65b9\u5f0f\u4e3a\u5185\u8fde\u63a5\uff0ctable1.column1=table2.column1\u7528\u4e8e\u6307\u660e\u4e24\u8868\u95f4\u7684\u8fde\u63a5\u6761\u4ef6\uff0c\u5982:
SELECT a.name,a.address,a.date,b.chinese,b.math,b.english FROM tb_demo065 AS a INNER JOIN tb_demo065_tel AS b on a.id=b.id
\u5341\u56db\u590d\u6742\u5185\u8fde\u63a5\u67e5\u8be2
\u590d\u6742\u7684\u5185\u8fde\u63a5\u67e5\u8be2\u662f\u5728\u57fa\u672c\u7684\u5185\u8fde\u63a5\u67e5\u8be2\u7684\u57fa\u7840\u4e0a\u518d\u9644\u52a0\u4e00\u4e9b\u67e5\u8be2\u6761\u4ef6\uff0c\u5982:
SELECT a.name,a.address,a.date,b.chinese,b.math,b.english FROM tb_demo065 AS a INNER JOIN tb_demo065_tel AS b on a.id=b.id WHERE b.id=(SELECT id FROM tb_demo065 WHERE tb_demo065.name='$_POST[text]')
\u603b\u4e4b\uff0c\u5b9e\u73b0\u8868\u4e0e\u8868\u4e4b\u95f4\u7684\u5173\u8054\u7684\u672c\u8d28\u662f\u4e24\u8868\u4e4b\u95f4\u5b58\u5728\u5171\u540c\u7684\u6570\u636e\u9879\u6216\u8005\u76f8\u540c\u7684\u6570\u636e\u9879\uff0c\u901a\u8fc7WHERE \u5b50\u53e5\u6216\u5185\u8fde\u63a5INNER JOIN \u2026 ON \u8bed\u53e5\u5c06\u4e24\u8868\u8fde\u63a5\u8d77\u6765\uff0c\u5b9e\u73b0\u67e5\u8be2
\u5341\u4e94\u4f7f\u7528\u5916\u8fde\u63a5\u5b9e\u73b0\u591a\u8868\u8054\u5408\u67e5\u8be2
(1)LEFT OUTER JOIN\u8868\u793a\u8868\u4e4b\u95f4\u901a\u8fc7\u5de6\u8fde\u63a5\u65b9\u5f0f\u76f8\u4e92\u8fde\u63a5\uff0c\u4e5f\u53ef\u7b80\u5199\u6210LEFT JOIN,\u5b83\u662f\u4ee5\u5de6\u4fa7\u7684\u8868\u4e3a\u57fa\u51c6\u6545\u79f0\u5de6\u8fde\u63a5\uff0c\u5de6\u4fa7\u8868\u4e2d\u6240\u6709\u4fe1\u606f\u5c06\u88ab\u5168\u90e8\u8f93\u51fa\uff0c\u800c\u53f3\u4fa7\u8868\u4fe1\u606f\u5219\u53ea\u4f1a\u8f93\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u4fe1\u606f\uff0c\u5bf9\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u4fe1\u606f\u5219\u8fd4\u56deNULL
e.x:SELECT a.name,a.address,b.math,b.english FROM tb_demo065 AS A LEFT OUTER JOIN tb_demo065_tel AS b ON a.id=b.id
(2)RIGHT OUTER JOIN\u8868\u793a\u8868\u4e4b\u95f4\u901a\u8fc7\u53f3\u8fde\u63a5\u65b9\u5f0f\u76f8\u4e92\u8fde\u63a5\uff0c\u4e5f\u53ef\u7b80\u5199\u6210RIGHT JOIN,\u5b83\u662f\u4ee5\u53f3\u4fa7\u7684\u8868\u4e3a\u57fa\u51c6\u6545\u79f0\u53f3\u8fde\u63a5\uff0c\u53f3\u4fa7\u8868\u4e2d\u6240\u6709\u4fe1\u606f\u5c06\u88ab\u5168\u90e8\u8f93\u51fa\uff0c\u800c\u5de6\u4fa7\u8868\u4fe1\u606f\u5219\u53ea\u4f1a\u8f93\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u4fe1\u606f\uff0c\u5bf9\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u4fe1\u606f\u5219\u8fd4\u56deNULL
E.X:SELECT a.name,a.address,b.math,b.english FROM tb_demo065 AS A RIGHT OUTER JOIN tb_demo065_tel AS b ON a.id=b.id
\u5341\u516d\u5229\u7528IN\u6216NOTIN\u5173\u952e\u5b57\u9650\u5b9a\u8303\u56f4
e.x:SELECT * FROM tb_demo083 WHERE code IN(SELECT code FROM tb_demo083 WHERE code BETWEEN '$_POST[text1]' AND '$_POST[text2]')
\u5229\u7528IN\u53ef\u6307\u5b9a\u5728\u8303\u56f4\u5185\u67e5\u8be2\uff0c\u82e5\u8981\u6c42\u5728\u67d0\u8303\u56f4\u5916\u67e5\u8be2\u53ef\u4ee5\u7528NOT IN\u4ee3\u66ff\u5b83
\u5341\u4e03\u7531IN\u5f15\u5165\u7684\u5173\u8054\u5b50\u67e5\u8be2
e.x:SELECT * FROM tb_demo083 WHERE code IN(SELECT code FROM tb_demo083 WHERE code = '$_POST[text]')
\u5341\u516b\u5229\u7528HAVING\u8bed\u53e5\u8fc7\u6ee4\u5206\u7ec4\u6570\u636e
HAVING\u5b50\u53e5\u7528\u4e8e\u6307\u5b9a\u7ec4\u6216\u805a\u5408\u7684\u641c\u7d22\u6761\u4ef6\uff0cHAVING\u901a\u5e38\u4e0eGROUP BY \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5982\u679cSQL\u8bed\u53e5\u4e2d\u4e0d\u542bGROUP BY\u5b50\u53e5\uff0c\u5219HAVING\u7684\u884c\u4e3a\u4e0eWHERE\u5b50\u53e5\u4e00\u6837.
e.x:SELECT name,math FROM tb_demo083 GROUP BY id HAVING math > '95'
SELECT item,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
HAVING sum(a.need_pay)<> sum(b.paymoney)
扩展资料:
关于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 之中。
sql多表关联查询跟条件查询大同小异,主要是要知道表与表之前的关系很重要;
举例说明:(某数据库中有3张表分别为:userinfo,dep,sex)
userinfo(用户信息表)表中有三个字段分别为:user_di(用户编号),user_name(用户姓名),user_dep(用户部门) 。(关系说明:userinfo表中的user_dep字段和dep表中的dep_id字段为主外键关系,userinfo表中的user_sex字段和sex表中的sex_id字段为主外键关系)
dep(部门表)表中有两个字段分别为:dep_id(部门编号),dep_name(部门名称)。(主键说明:dep_id为主键)
sex(性别表)表中有两个字段分别为:sex_id(性别编号),sex_name(性别名称)。(主键说明:sex_id为主键)
一,两张表关键查询
1、在userinfo(用户信息表)中显示每一个用户属于哪一个部门。sql语句为:
select userinfo.user_di,userinfo.user_name,dep_name from userinfo,dep where userinfo.user_dep=dep.dep_id
2、在userinfo(用户信息表)中显示每一个用户的性别。sql语句为:
select userinfo.user_di,userinfo.user_name,sex.sex_name from userinfo,sex where userinfo.user_sex=sex.sex_id
二、多张表关键查询
最初查询出来的userinfo(用户信息表)表中部门和性别都是以数字显示出来的,如果要想在一张表中将部门和性别都用汉字显示出来,需要将三张表同时关联查询才能实现。
sql语句为:
select userinfo.user_di,userinfo.user_name,dep.dep_name,sex.sex_name from userinfo,dep,sex where userinfo.user_dep=dep.dep_id and userinfo.user_sex=sex.sex_id
(多个条件用and关联)
联表查询一般有内连接、左/右连接、外连接。
内连接:
select * from t1 inner join t2 on t1.id1=t2.id2;
左连接:
select * from t1 left join t2 on t1.id1=t2.id2;
右连接:
select * from t1 right join t2 on t1.id1=t2.id2;
外连接:
select * from t1 outter join t2 on t1.id1=t2.id2;
--左
select * from tablea as a left join tableb as b on a.ID=b.ID left join tablec as c on a.ID=c.ID where XXXXXXXX
--右
select * from tablea as a right join tableb as b on a.ID=b.ID right join tablec as c on a.ID=c.ID where XXXXXXXX
--内连
select * from tablea as a inner join tableb as b on a.ID=b.ID inner join tablec as c on a.ID=c.ID where XXXXXXXX
select * from tablea as a,tableb as b,tablec as c where a.ID=b.ID and a.ID=c.ID
可以混用 left join right join inner join
楼上的好像条件判断不对,试下这个
SELECT item,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
HAVING sum(a.need_pay)<> sum(b.paymoney)
绛旓細SQL璇彞锛歴elect from student left join course on student.ID=course.ID 鎵ц缁撴灉锛氬乏澶栬繛鎺ュ寘鍚玪eft join宸﹁〃鎵鏈夎锛屽鏋滃乏琛ㄤ腑鏌愯鍦ㄥ彸琛ㄦ病鏈夊尮閰嶏紝鍒欑粨鏋滀腑瀵瑰簲琛屽彸琛ㄧ殑閮ㄥ垎鍏ㄩ儴涓虹┖(NULL).娉細姝ゆ椂鎴戜滑涓嶈兘璇寸粨鏋滅殑琛屾暟绛変簬宸﹁〃鏁版嵁鐨勮鏁般傚綋鐒舵澶鏌ヨ缁撴灉鐨勮鏁扮瓑浜庡乏琛ㄦ暟鎹殑琛屾暟锛屽洜涓...
绛旓細1銆侀鍏堝湪妗岄潰涓婏紝鐐瑰嚮鈥淢anagement Studio鈥濆浘鏍囥2銆佷箣鍚庡湪璇ョ晫闈腑锛岀偣鍑诲乏涓婅鈥滄柊寤烘煡璇⑩濋夐」銆3銆佹帴鐫鍦ㄨ鐣岄潰涓锛岃緭鍏ヨ法鏁版嵁搴撹繛琛ㄦ煡璇㈢殑sql璇彞鈥渟elect a.name as aname ,a.grade as bgrade ,b.name as bname,b.grade as bgrade from LGEMPS.dbo.test2 as a, test.dbo.rss as ...
绛旓細SELECT item,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 HAVING sum(a.need_pay)<> sum(b.paymoney)...
绛旓細SQL璇█涓,鍙互閫氳繃UNION 鎴 ALL灏嗗涓猄ELECT璇彞鐨鏌ヨ缁撴灉鍚堝苟杈撳嚭,杩欎袱涓叧閿瓧鐨勪娇鐢ㄨ鏄庡涓:UNION:鍒╃敤璇ュ叧閿瓧鍙互灏嗗涓猄ELECT 璇彞鐨勬煡璇㈢粨鏋滃悎骞惰緭鍑,骞跺垹闄ら噸澶嶈ALL:鍒╃敤璇ュ叧閿瓧鍙互灏嗗涓猄ELECT 璇彞鐨勬煡璇㈢粨鏋滃悎骞惰緭鍑,浣嗕笉浼氬垹闄ら噸澶嶈鍦ㄤ娇鐢║NION鎴朅LL鍏抽敭瀛楀皢澶氫釜琛ㄥ悎骞惰緭鍑烘椂,鏌ヨ缁撴灉蹇呴』鍏锋湁鐩稿悓鐨...
绛旓細涓銆佺敤杩炴帴鏌ヨ锛歋ELECT * FROM Student INNER JOIN Score ON Cno=2 AND Grade>=90 浜屻佺敤瀛愭煡璇細SELECT * FROM Student WHERE Sno IN(SELECT Sno FROM Score WHERE Cno=2 AND Grade>90)
绛旓細);insert into users values (5,3,'jack5', 23, '涓婃捣');insert into users values (6,3,'jack6', 23, '涓婃捣');3銆鏌ヨ琛涓暟鎹紱select * from users;4銆佺紪鍐sql锛屽叧鑱斾袱寮犺〃锛泂elect * from users t , company b where t.companyid = b.companyid and t.companyid = 1;...
绛旓細3.涓涓仈鍚鏌ヨ锛屼竴涓祵濂楁煡璇 select student.name from student where student.id IN (select c1.sid from choose c1 where choose.cid='1'union select c2.sid from choose c2 where choose.cid='2'on c1.sid=c2.sid )4.鍏跺疄灏辨槸鑷繛鎺ユ煡璇㈠拰琛屽垪浜ゆ崲鐨勯棶棰橈細select student.id,(case ...
绛旓細1銆佹暟鎹鏌ヨ璇█锛圖QL:Data Query Language锛夊叾璇彞锛屼篃绉颁负鈥滄暟鎹绱㈣鍙モ濓紝鐢ㄤ互浠庤〃涓幏寰楁暟鎹紝纭畾鏁版嵁鎬庢牱鍦ㄥ簲鐢ㄧ▼搴忕粰鍑恒備繚鐣欏瓧SELECT鏄疍QL锛堜篃鏄墍鏈塖QL锛夌敤寰楁渶澶氱殑鍔ㄨ瘝锛屽叾浠朌QL甯哥敤鐨勪繚鐣欏瓧鏈塛HERE锛孫RDER BY锛孏ROUP BY鍜孒AVING銆傝繖浜汥QL淇濈暀瀛楀父涓庡叾瀹冪被鍨嬬殑SQL璇彞涓璧蜂娇鐢ㄣ2銆佹暟鎹搷浣...
绛旓細1銆SQL璇彞锛歴elect u.*,r.*,r.id rid from user u left join sys_user_role sur on u.id = sur.useridleft join sys_role r on sur.roleid = r.id 鍥剧墖锛氾紙琛ㄥ悕鎴浘锛夎鐐瑰嚮杈撳叆鍥剧墖鎻忚堪 璇风偣鍑昏緭鍏ュ浘鐗囨弿 娉1锛歶ser锛堢敤鎴疯〃锛夛紝sys_role锛堣鑹茶〃锛夛紝sys_user_role锛堝叧绯昏〃锛...
绛旓細宸ュ叿/鍘熸枡 璁$畻鏈 SQLserver2008 鏂规硶/姝ラ 1銆佹煡璇㈡墍鏈夐夎鐨勫鐢熺殑瀛﹀彿锛屽鍚嶏紝璇剧▼鍚嶅強鎴愮哗銆鏌ヨ璇彞銆2銆佸嚭鐜版煡璇㈢粨鏋溿3銆佹煡璇㈡墍鏈夋瘮璧甸洩骞撮緞澶х殑瀛︾敓鐨勫鍚嶅拰骞撮緞鏌ヨ璇彞銆4銆佸嚭鐜版煡璇㈢粨鏋溿