postgresql - Query two tables with one to many relationship -
i'm using 2 postgres tables have 1 many relationship. primary table called users , other table called files.
the users table has following columns:
id serial primary key, email varchar(128) not null, username varchar(128) unique not null, password_hash varchar(128) not null
the files table has following columns:
id serial primary key, user_id integer references users(id), title varchar(128) not null, url varchar(128) not null
when log app, i'm querying files display doing
cur.execute('select * files')
and when want specific users files run
cur.execute('select * files user_id = %i' % user_id)
for query fetches files, i'd adjust username associated each file also. how should tailor execute statement make happen?
try following. know syntax work other dbms':
cur.execute('select f.*, u.username files f, users u u.id = f.user_id)
Comments
Post a Comment