简体   繁体   English

在 PostgreSQL. SQLAlchemy. Python 上用两个表搜索 SQL 请求

[英]Search SQL request with two tables on PostgreSQL. SQLAlchemy. Python

Need help in request making on SQL or SQLAlchemy在 SQL 或 SQLAlchemy 上提出请求时需要帮助

First table named as Rows第一个表名为Rows

sid资料库 unit_sid单位名称
ROW_UUID1 ROW_UUID1 UNIT_UUID1 UNIT_UUID1
ROW_UUID2 ROW_UUID2 UNIT_UUID1 UNIT_UUID1
ROW_UUID3 ROW_UUID3 UNIT_UUID UNIT_UUID

Second table with name Records名称为Records的第二个表

row_sid (==SID from ROWS) row_sid(==来自 ROWS 的 SID) item_sid item_sid content (str)内容(str)
ROW_UUID1 ROW_UUID1 ITEM_UUID1 ITEM_UUID1 Decription 1说明1
ROW_UUID1 ROW_UUID1 ITEM_UUID2 ITEM_UUID2 Decription 1说明1
ROW_UUID2 ROW_UUID2 ITEM_UUID1 ITEM_UUID1 Description 3描述 3
ROW_UUID2 ROW_UUID2 ITEM_UUID2 ITEM_UUID2 Description 2说明 2
ROW_UUID3 ROW_UUID3 ITEM_UUID1 ITEM_UUID1 Description 5说明 5
ROW_UUID3 ROW_UUID3 ITEM_UUID2 ITEM_UUID2 Description 1描述 1

I need an example of a SQL query, where I can specify a search for several content values for different item_sid我需要一个 SQL 查询的示例,我可以在其中指定搜索不同 item_sid 的多个内容

For example I need all ROWS where例如我需要所有行

  1. item_sid == ITEM_UUID1 and content == Description 1 item_sid == ITEM_UUID1 和 content == Description 1
  2. item_sid == ITEM_UUID2 and content == Description 1 item_sid == ITEM_UUID2 and content == Description 1

Request like bellow will not work for me, because I need search in two item_sid in same time for receiving unique ROWS像下面这样的请求对我不起作用,因为我需要同时搜索两个 item_sid 以接收唯一的 ROWS

select  row_sid
from rows
    left join record on rows.sid = record.row_sid
    where (item_sid = '877aeeb4-c68e-4942-b259-288e7aa3c04b' and
          content  like '%TEXT%')
    and (item_sid = 'cc22f239-db6c-4041-92c6-8705cb621525' and
          content  like '%TEXT2%') GROUP BY row_sid

Solved like解决像

select  row_sid
from rows
    left join record on rows.sid = record.row_sid
    where (item_sid = '877aeeb4-c68e-4942-b259-288e7aa3c04b' and
          content  like '%TEXT%')
    or (item_sid = 'cc22f239-db6c-4041-92c6-8705cb621525' and
          content  like '%TEXT2%') GROUP BY row_sid having count(row_sid) = 2

But maybe there are more beautiful solution?但也许有更漂亮的解决方案? I want to request different number of item_sids (2-5) in the same time我想同时请求不同数量的 item_sids (2-5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM