简体   繁体   English

JSON 对象列上的 PostgreSQL JOIN

[英]PostgreSQL JOIN on JSON Object column

I'm supposed to join 3 different tables on postgres:我应该在 postgres 上加入 3 个不同的表:

  • lote_item (on which I have some books id's) lote_item (我有一些书籍 ID)
  • lote_item_log (on which I have a column "attributes", with a JSON object such as {"aluno_id": "2823", "aluno_email": "someemail@outlook.com", "aluno_unidade": 174, "livro_codigo": "XOZK-0NOYP0Z1EMJ"} ) - Obs.: Some values on aluno_unidade are null lote_item_log (我在其中有一列“属性”,带有一个 JSON 对象,例如{"aluno_id": "2823", "aluno_email": "someemail@outlook.com", "aluno_unidade": 174, "livro_codigo": " XOZK-0NOYP0Z1EMJ"} ) -观察aluno_unidade上的某些值为

and finally最后

  • company (on which I have every school name for every aluno_unidade. Ex: aluno_unidade = 174 ==> nome_fantasia = mySchoolName ).公司(我有每个 aluno_unidade 的每个学校名称。例如: aluno_unidade = 174 ==> nome_fantasia = mySchoolName )。

Joining the first two tables was easy, since lote_item_log has a foreign key which I could match like this:加入前两个表很容易,因为lote_item_log有一个外键,我可以像这样匹配:

SELECT * FROM lote_item JOIN lote_item_log ON lote_item.id = lote_item_log.lote_item_id SELECT * FROM lote_item JOIN lote_item_log ON lote_item.id = lote_item_log.lote_item_id

Now, I need to get the School Name , contained on table company , with the aluno_unidade ID from table lote_item_log .现在,我需要获取包含在表company 中School Name以及表lote_item_log 中aluno_unidade ID。

My current query is:我目前的查询是:

        SELECT 
            *
        FROM 
            lote_item
        JOIN
            lote_item_log
        ON
            lote_item.id = lote_item_log.lote_item_id
        JOIN
            company
        ON
            (
                SELECT
                    JSON_EXTRACT_PATH_TEXT(attributes, 'aluno_unidade')::int
                FROM
                    lote_item_log
                WHERE
                    operation_id = 6
            ) = company.senior_id
        WHERE 
            item_id = {book_id};

operation_id determines which school is active. operation_id确定哪个学校是活跃的。

ERROR I'M GETTING:我得到的错误:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.CardinalityViolation) more than one row returned by a subquery used as an expression sqlalchemy.exc.ProgrammingError: (psycopg2.errors.CardinalityViolation) 用作表达式的子查询返回多于一行

I tried LIMIT 1, but then I got just an empty array.我尝试了 LIMIT 1,但后来我得到了一个空数组。

What I need is:我需要的是:

lote_item.created_at | lote_item.updated_at | lote_item.item_id | uuid | aluno_email | c014_id | nome_fantasia | cnpj | is_franchise | is_active
         somedate    |     somedate         |   some_item_id    | XJW4 | someemail@a | some_id | SCHOOL NAME   | cnpj |    t       |      t       

I got it.我知道了。

Not sure it's the best way, but worked...不确定这是最好的方法,但有效...

        SELECT 
            *
        FROM 
            lote_item
        JOIN
            lote_item_log
        ON
            lote_item.id = lote_item_log.lote_item_id
        JOIN
            company
        ON
            JSON_EXTRACT_PATH_TEXT(attributes, 'aluno_unidade')::int = company.senior_id
        WHERE 
            item_id = {book_id};

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

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