{"id":2924,"date":"2021-07-21T08:24:06","date_gmt":"2021-07-21T08:24:06","guid":{"rendered":"https:\/\/www.bearloga.space\/?page_id=2924"},"modified":"2021-07-21T08:24:07","modified_gmt":"2021-07-21T08:24:07","slug":"sqlalchemy-orm-operatory-filtrov","status":"publish","type":"page","link":"https:\/\/www.bearloga.space\/uk\/sqlalchemy-orm-operatory-filtrov\/","title":{"rendered":"SQLAlchemy ORM \u2014 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0438\u0437\u0443\u0447\u0438\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u043a\u043e\u0434\u0430\u043c\u0438 \u0438 \u0432\u044b\u0432\u043e\u0434\u043e\u043c.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u0420\u0430\u0432\u043d\u043e<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u041e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 ==, \u0438 \u043e\u043d \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">result = session.query(Customers).filter(Customers.id == 2)\n\nfor row in result:\n   print (\"ID:\", row.id, \"Name: \",row.name, \"Address:\",row.address, \"Email:\",row.email)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">SQLAlchemy \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 SQL \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT customers.id \nAS customers_id, customers.name \nAS customers_name, customers.address \nAS customers_address, customers.email \nAS customers_email\nFROM customers\nWHERE customers.id = ?<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u044b\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ID: 2 Name: Komal Pande Address: Banjara Hills Secunderabad Email: komal@gmail.com\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u041d\u0435 \u0440\u0430\u0432\u043d\u043e<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0434\u043b\u044f \u043d\u0435 \u0440\u0430\u0432\u043d\u043e, \u2014 \u044d\u0442\u043e! =, \u0418 \u043e\u043d \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0438 \u043d\u0435 \u0440\u0430\u0432\u043d\u043e.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">result = session.query(Customers).filter(Customers.id! = 2)\n\nfor row in result:\n   print (\"ID:\", row.id, \"Name: \",row.name, \"Address:\",row.address, \"Email:\",row.email)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 SQL \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT customers.id \nAS customers_id, customers.name \nAS customers_name, customers.address \nAS customers_address, customers.email \nAS customers_email\nFROM customers\nWHERE customers.id != ?<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u044b\u0432\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u0432\u044b\u0448\u0435 \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ID: 1 Name: Ravi Kumar Address: Station Road Nanded Email: ravi@gmail.com\nID: 3 Name: Rajender Nath Address: Sector 40, Gurgaon Email: nath@gmail.com\nID: 4 Name: S.M.Krishna Address: Budhwar Peth, Pune Email: smk@gmail.com\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u043f\u043e\u0434\u043e\u0431\u043d\u043e<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u0421\u0430\u043c \u043c\u0435\u0442\u043e\u0434 like () \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0438 LIKE \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f WHERE \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 SELECT.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">result = session.query(Customers).filter(Customers.name.like('Ra%'))\nfor row in result:\n   print (\"ID:\", row.id, \"Name: \",row.name, \"Address:\",row.address, \"Email:\",row.email)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u044b\u0448\u0435 SQLAlchemy \u043a\u043e\u0434 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u0435\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044e SQL \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT customers.id \nAS customers_id, customers.name \nAS customers_name, customers.address \nAS customers_address, customers.email \nAS customers_email\nFROM customers\nWHERE customers.name LIKE ?<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0418 \u0432\u044b\u0432\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ID: 1 Name: Ravi Kumar Address: Station Road Nanded Email: ravi@gmail.com\nID: 3 Name: Rajender Nath Address: Sector 40, Gurgaon Email: nath@gmail.com\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0412<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u042d\u0442\u043e\u0442 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 \u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u043a \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435.&nbsp;\u042d\u0442\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c in_ ().<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">result = session.query(Customers).filter(Customers.id.in_([1,3]))\nfor row in result:\n   print (\"ID:\", row.id, \"Name: \",row.name, \"Address:\",row.address, \"Email:\",row.email)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0417\u0434\u0435\u0441\u044c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 SQL, \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u043e\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c SQLite, \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT customers.id \nAS customers_id, customers.name \nAS customers_name, customers.address \nAS customers_address, customers.email \nAS customers_email\nFROM customers\nWHERE customers.id IN (?, ?)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u044b\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ID: 1 Name: Ravi Kumar Address: Station Road Nanded Email: ravi@gmail.com\nID: 3 Name: Rajender Nath Address: Sector 40, Gurgaon Email: nath@gmail.com\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0410 \u0422\u0410\u041a\u0416\u0415<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u042d\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0443\u0442\u0435\u043c&nbsp;<strong>\u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0435\u0432, \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u044f\u0442\u044b\u043c\u0438, \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0435 \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430 and_ (),<\/strong>&nbsp;\u043a\u0430\u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0438\u0436\u0435 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">result = session.query(Customers).filter(Customers.id&gt;2, Customers.name.like('Ra%'))\nfor row in result:\n   print (\"ID:\", row.id, \"Name: \",row.name, \"Address:\",row.address, \"Email:\",row.email)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import and_\nresult = session.query(Customers).filter(and_(Customers.id&gt;2, Customers.name.like('Ra%')))\n\nfor row in result:\n   print (\"ID:\", row.id, \"Name: \",row.name, \"Address:\",row.address, \"Email:\",row.email)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041e\u0431\u0430 \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043f\u0440\u0438\u0432\u043e\u0434\u044f\u0442 \u043a \u043f\u043e\u0445\u043e\u0436\u0435\u043c\u0443 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044e SQL \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT customers.id \nAS customers_id, customers.name \nAS customers_name, customers.address \nAS customers_address, customers.email \nAS customers_email\nFROM customers\nWHERE customers.id &gt; ? AND customers.name LIKE ?<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u044b\u0445\u043e\u0434 \u0434\u043b\u044f \u0432\u044b\u0448\u0435\u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u0440\u043e\u043a \u043a\u043e\u0434\u0430 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ID: 3 Name: Rajender Nath Address: Sector 40, Gurgaon Email: nath@gmail.com\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u0418\u041b\u0418 \u0416\u0415<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u042d\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c&nbsp;<strong>or_ ()<\/strong>&nbsp;.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import or_\nresult = session.query(Customers).filter(or_(Customers.id&gt;2, Customers.name.like('Ra%')))\n\nfor row in result:\n   print (\"ID:\", row.id, \"Name: \",row.name, \"Address:\",row.address, \"Email:\",row.email)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0434\u0432\u0438\u0436\u043e\u043a SQLite \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 SQL \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT customers.id \nAS customers_id, customers.name \nAS customers_name, customers.address \nAS customers_address, customers.email \nAS customers_email\nFROM customers\nWHERE customers.id &gt; ? OR customers.name LIKE ?<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0438\u0437\u0443\u0447\u0438\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u043a\u043e\u0434\u0430\u043c\u0438 \u0438 \u0432\u044b\u0432\u043e\u0434\u043e\u043c. \u0420\u0430\u0432\u043d\u043e \u041e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 ==, \u0438 \u043e\u043d \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0438 \u0434\u043b\u044f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[103],"tags":[],"class_list":["post-2924","page","type-page","status-publish","hentry","category-sqlalchemy"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages\/2924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/comments?post=2924"}],"version-history":[{"count":0,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages\/2924\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/media?parent=2924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/categories?post=2924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/tags?post=2924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}