How to select 5 records(including username, first_name, last_name, contact) for each user in the database?
table user( id, username, first_name, last_name )
contact( id id_user //id from user table name )
To select from these two tables you'll need to write an inner join query:
SELECT u.username, u.first_name, u.last_name, c.name FROM user u INNER JOIN contact c ON (u.id = c.id_user)
That will get you all records from the users table joined with all matching records from the contacts table.
Originally posted by eoghain To select from these two tables you'll need to write an inner join query: SELECT u.username, u.first_name, u.last_name, c.name FROM user u INNER JOIN contact c ON (u.id = c.id_user) That will get you all records from the users table joined with all matching records from the contacts table. [/B]
Originally posted by eoghain To select from these two tables you'll need to write an inner join query:
That will get you all records from the users table joined with all matching records from the contacts table. [/B]
How to get 5 records per user and order by first_name?
TH
You can't. You can get 5 total records, or you can get them all. You can't get 5 records for each specific user in a single query.