如何使用left join on and查询 如何使用left join on and查询

\u5982\u4f55\u4f7f\u7528left join on and\u67e5\u8be2

1\u3001\u8868a\u7ed3\u6784\u548c\u6570\u636e
table a(id, type):

id type

----------------------------------

1 1

2 1

3 2

2\u3001\u8868b\u7ed3\u6784\u548c\u6570\u636e
table b(id, class):

id class

---------------------------------

1 1

2 2

3\u3001SQL\u8bed\u53e5\u8c03\u7528
--sql\u8bed\u53e51\uff1a

select a.*, b.* from a left join b on a.id = b.id and a.type = 1;

\u6267\u884c\u7ed3\u679c\u4e3a:

a.id a.type b.id b.class

----------------------------------------

1 1 1 1

2 1 2 2

3 2

--sql\u8bed\u53e52\uff1a

select a.*, b.* from a left join b on a.id = b.id where a.type = 1;

\u6267\u884c\u7ed3\u679c\u4e3a:

a.id a.type b.id b.class

----------------------------------------

1 1 1 1

2 1 2 2

--sql\u8bed\u53e53\uff1a

select a.*, b.* from a left join b on a.id = b.id and b.class = 1;

\u6267\u884c\u7ed3\u679c\u4e3a\uff1a

a.id a.type b.id b.class

----------------------------------------

1 1 1 1

2 1

3 2


\u2013sql\u8bed\u53e51\u53ef\u89c1\uff0cleft join \u4e2d\u5de6\u8868\u7684\u5168\u90e8\u8bb0\u5f55\u5c06\u5168\u90e8\u88ab\u67e5\u8be2\u663e\u793a\uff0con \u540e\u9762\u7684\u6761\u4ef6\u5bf9\u5b83\u4e0d\u8d77\u4f5c\u7528\uff0c\u9664\u975e\u518d\u540e\u9762\u518d\u52a0\u4e0awhere\u6765\u8fdb\u884c\u7b5b\u9009\uff0c\u8fd9\u5c31\u662fsql\u8bed\u53e52\u4e86\u3002
\u2013\u7531sql\u8bed\u53e53\u53ef\u89c1\uff0con\u540e\u9762\u7684\u6761\u4ef6\u4e2d\uff0c\u53f3\u8868\u7684\u9650\u5236\u6761\u4ef6\u5c06\u4f1a\u8d77\u4f5c\u7528\u3002

select * from table a left join table b on a.id=b.id

\u7c7b\u4f3c\u8fd9\u6837\uff0c\u53d6\u4e2a\u8868\u7684\u522b\u540d\u5c31\u6210\uff0c\u4f60\u60f3\u8fde\u51e0\u6b21\u5c31\u8fde\u51e0\u6b21\uff0c\u8868\u7684\u522b\u540d\u4e0d\u540c\u5c31\u597d\u3002\u867d\u7136\u4e0d\u77e5\u9053\u4f60\u8fd9\u6837\u505a\u7684\u610f\u4e49\u3002\u3002

你好
关于如何使用left join on and查询:
一、概念:
–把left join左边的表的记录全部找出来。系统会先用表A和表B做个笛卡儿积,然后以表A为基表,去掉笛卡儿积中表A部分为NULL的记录。最后形成你的结果。
–进行左连接时,就有涉及到主表、辅表,这时主表条件写在WHERE之后,辅表条件写在ON后面。
二、总结:
–sql语句1可见,left join 中左表的全部记录将全部被查询显示,on 后面的条件对它不起作用,除非再后面再加上where来进行筛选,这就是sql语句2了。
–由sql语句3可见,on后面的条件中,右表的限制条件将会起作用。
希望对你有帮助

left join on and
左键连接

扩展阅读:xbox series x ... 一对多left join ... left join on 1 1 ... 小表left join大表 ... left join on使用方法 ... left join以哪个表为主 ... left semi join ... left join 大表放前面 ... left join多怎么优化 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网