[DB-SIG] how do I execute a query likte 'select * from mytable where id in (1, 2, 3)'?

Joerg Beyer j.beyer at web.de
Sat Dec 29 21:53:11 CET 2007


Harald Armin Massa schrieb:
> Joerg,
> 
>> maybe this is obvious, but I don't know how to execute a query like:
>> select * from mytable where id in (1,2,3)
>> from python. The difficult part is the set in the where clause, the "in
>> (a,b,c,d, ...)"
> 
> 
> WHICH database are you talking about?

I am using MySQL.

> 
> With Oracle, you are on your own, that is, you have to do stringmagic
> to create the SQL.
> 
> With PostgreSQL and PSYCOPG2 you can use lists:
> 
> cs.execute("select * from s2e where element = any (%(what)s)",
> dict(what=[1,2,3]))
> 
> using PostgreSQL arrays and the "any" function.

the MySQL function "in()" seems to be what "any()" is for PostgreSQL, see
http://dev.mysql.com/doc/refman/5.0/en/func-op-summary-ref.html
http://www.postgresql.org/docs/8.2/static/functions-aggregate.html

The missing trick was to use a python list as a an argument to the
query. It is obvious now.

	Thanks for the hint
	Joerg
> Best wishes,
> 
> Harald
> 
> 


-- 
skype: j.beyer, jabber: j.beyer at web.de
gpg: 2CFB B5F2 988E 8897 06DB  2D50 F128 D2D6 F1B2 2099


More information about the DB-SIG mailing list