[英]PostgreSQL JOIN on JSON Object column
I'm supposed to join 3 different tables on postgres:我应该在 postgres 上加入 3 个不同的表:
and finally最后
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.