Forum OpenACS Development: SQL queries 'n TCL

Collapse
Posted by Iuri Sampaio on
Hi,

I did a SQL query to select some values (i.e. organization_ids) from a random organization table filtered by user_id.

Then, I need to hold those values (organization_ids) to use in another SQL query in a diferent table to actually get the organization info i need.

So far I see i need to use a db_list to get the organization_ids in a TCL variable.
Then, I use a foreach,
in order to read the tcl list and execute a Select to each organization_id.

I don;t have a perfect idea on how to code this.
where can i find more info about this issue?

Does anyone know a better aproach?

regards,

Collapse
2: Re: SQL queries 'n TCL (response to 1)
Posted by Patrick Giagnocavo on
Iuri, what you need is to do a subselect. This is where you take the 2 queries and combine them, returning only the results you want. The method you mention will be extremely slow.

If on the other hand, you need to have the user select from a list (from the first query) and then return information based on what the user chooses, it is better to have them first choose the organization_ids , then go ahead and do the query with the organization_ids as a list.

Does that answer your question?

Collapse
3: Re: SQL queries 'n TCL (response to 2)
Posted by Iuri Sampaio on
I beileve the first method you mentioned is the one i need.

The subselect is the best way, Since the users dont need to choose anything, I meant, the first query is just to match the users(oganizations owners) with the organizations, in order to get the orgs info.

By the way, I have no clue how to use subselect! :(

Collapse
4: Re: SQL queries 'n TCL (response to 1)
Posted by Malte Sussdorff on
The full documentation on subqueries can be found at http://www.postgresql.org/docs/current/static/functions-subquery.html

You can do the following:

select * from organizations where organization_id in (select organization_id from yourtable where user_id = :foo)

Hope this helps

Collapse
5: Re: SQL queries 'n TCL (response to 4)
Posted by Brian Fenton on
Or you could just do an inner join. 😊

select o.* from organizations o, yourtable y
where o.organization_id = y.organization_id
and y.user_id = :foo

Brian

Collapse
6: Re: SQL queries 'n TCL (response to 5)
Posted by Iuri Sampaio on
The select i did is a litle bit tricky, since my organization pkg is set up as non-singleton, so i need to pass the package_id and organization_id in the query to get each organization of each package the user owns.

the query:
?xml version="1.0"?
queryset
fullquery name="orgs_query"
querytext
SELECT
o.organization_id,
o.name,
o.legal_name,
o.reg_number,
o.notes,
ot.type as organization_type
FROM
organizations o,
organization_types ot,
organization_type_map tm
WHERE
o.organization_id in (SELECT ow.organization_id
FROM organization_owner ow
WHERE user_id = :user_id)
AND
o.organization_id = tm.organization_id and
o.packages = :(SELECT ow.package_id
FROM organization_owner ow
WHERE user_id =: user_id)
tm.organization_type_id = ot.organization_type_id and
[template::list::filter_where_clauses -and -name "orgs"]
[template::list::orderby_clause -orderby -name orgs]
/querytext
/fullquery

The attribution is incorrectly made
o.packages = :(SELECT ow.package_id
FROM organization_owner ow
WHERE user_id =: user_id)

MAlte,
I haven't found anything similar to this on the link you post above. Although it's a very good source, thanks a lot.

Collapse
7: Re: SQL queries 'n TCL (response to 1)
Posted by Iuri Sampaio on
So far,
the problem is solved. Easily than i expected!!

fullquery name="orgs_query"
querytext
SELECT
ow.organization_id,
ow.package_id,
o.organization_id,
o.name,
o.legal_name,
o.reg_number,
o.notes,
ot.type as organization_type
FROM
organization_owner ow,
organizations o,
organization_types ot,
organization_type_map tm
WHERE
ow.user_id = :user_id and
o.organization_id = tm.organization_id and
tm.organization_type_id = ot.organization_type_id and
o.package_id = ow.package_id
[template::list::filter_where_clauses -and -name "orgs"]
[template::list::orderby_clause -orderby -name orgs]
/querytext
/fullquery

thanks a lot for the feedback

Collapse
8: Re: SQL queries 'n TCL (response to 7)
Posted by Iuri Sampaio on
I just found out the query does not filter by package_id. So, orgs created by users in another org package instance, is not select to the users own orgs list.

SELECT o.organization_id,
o.name,
o.legal_name,
o.reg_number,
o.notes,
ot.type as organization_type
FROM organizations o,
organization_types ot,
organization_type_map tm
WHERE
o.organization_id = tm.organization_id and
o.organization_id in (SELECT ow.organization_id,ow.package_id
FROM organization_owner ow
WHERE ow.user_id = :user_id) and
tm.organization_type_id = ot.organization_type_id and
o.package_id =
[template::list::filter_where_clauses -and -name "orgs"]
[template::list::orderby_clause -orderby -name orgs]
Collapse
9: Re: SQL queries 'n TCL (response to 8)
Posted by Iuri Sampaio on
It's done!
PAtrick and Malte,
It's really great the subselect you two mentioned on this thread.


SELECT o.organization_id,
o.name,
o.legal_name,
o.reg_number,
o.notes,
ot.type as organization_type
FROM organizations o,
organization_types ot,
organization_type_map tm
WHERE
o.organization_id = tm.organization_id and
o.organization_id in (SELECT ow.organization_id
FROM organization_owner ow
WHERE ow.user_id = :user_id) and
tm.organization_type_id = ot.organization_type_id and
o.package_id in (SELECT ow.package_id
FROM organization_owner ow
WHERE ow.organization_id = o.organization_id)

Thanks for the link to the docs. I didn't find before on PG website